So i'm having trouble making a program that asks the user to enter a number and then using that number I must increase the value of the pointer towards two_count and three_count. These are counter the factors of two's and three's in the number entered.
For example if the user input 2, then the program should spit out
"There have been 1 factor of 2 and 0 factors of 3"
Then the user can input 0 to exit program
What I have so far is
include <stdio.h>
void main()
{
int* two_count;
int* three_count;
int num;
while(two_count >= 0 || three_count >= 0)
{
printf("Enter a number: \n");
scanf("%d", &num);
if(num % 2)
{
two_count++;
}
else if(num % 3)
{
three_count++;
}
else if(num == 0)
{
printf("Thank you for playing, enjoy your day!\n");
break;
}
printf("So far, there have been %d factors of 2 and %d factors of 3\n", two_count, three_count);
}
}
Thanks!
If you want to use pointers, you can do it like this
int two_count = 0;
int* two_count_ptr = &two_count;
int three_count = 0;
int* three_count_ptr = &three_count;
Then, for retrieving value and incrementing you would need to dereference the pointer
while(*two_count_ptr >= 0 || *three_count_ptr >= 0)
(*two_count_ptr)++;
Hope this helps.
Related
Number guessing game which a user guess a number from 0-20 and i want it to display how many tries are left for the user for example the maximum tries is 5 and if the user got it wrong on the first try it will display something like "tries left : 4", how do i implement that in my code?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
time_t t;
srand((unsigned)time(&t));
int randomNumber = rand() % 21;
int num;
int guess = 0;
printf("\nThis is a guessing game.");
printf("\nGuess 5 times only\n");
printf("\nEnter your guess:");
scanf("%d",&num);
while (num != randomNumber && guess < 5){ //checks if num is equal or not to randomnumber and count is less than 5 or limit 5
if(num > randomNumber){ // checks if num is greater than randomNumber
printf("Too high! try again:");
scanf("%d",&num);
guess++;
}
if(num < randomNumber){ //checks if num is less than randomNumber
printf("Too low! try again:");
scanf("%d",&num);
guess++;
}
if(num == randomNumber){ //checks if num is equal to randomNumber
printf("You got it right!\n");
return 0;
}
if(guess == 5) //checks if tries is 5 then exits program.
{
printf("your out guess of guesses!\n");
return 0;
}
}
return 0;
}
You can add this inside your while loop (at the end)
// For total no of guesses = 5, if that is n then n - 1 - guess
printf("tries left %d\n", 4 - guess);
If you’re trying to print a value in a loop something along the lines of printf(“You have %d guesses left\n”,5-guess); might work
You already have an answer, but I would advise you using symbolic constants #define GUESS_NUM 4 instead of actual numbers in your statements and your program in general, in programs like this it makes no difference, but you can get confused very easily in larger projects.
Also, your loop should end when guess reaches 4 because you already have one guess before the loop. You could write something like this:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define GUESS_NUM 4
int main()
{
time_t t;
srand((unsigned)time(&t));
int randomNumber = rand() % 21;
int num;
int guess = 0;
printf("\nThis is a guessing game.");
printf("\nGuess 5 times only\n");
printf("\nEnter your guess:");
scanf("%d",&num);
while (num != randomNumber && guess < GUESS_NUM){ //checks if num is equal or not to randomnumber and count is less than 5 or limit 5
if(num > randomNumber){ // checks if num is greater than randomNumber
printf("tries left : %d\n", GUESS_NUM-guess);
printf("Too high! try again:");
scanf("%d",&num);
guess++;
}
if(num < randomNumber){ //checks if num is less than randomNumber
printf("tries left : %d\n", GUESS_NUM-guess);
printf("Too low! try again:");
scanf("%d",&num);
guess++;
}
if(num == randomNumber){ //checks if num is equal to randomNumber
printf("You got it right!\n");
return 0;
}
if(guess == GUESS_NUM) //checks if tries is 5 then exits program.
{
printf("your out guess of guesses!\n");
return 0;
}
}
return 0;
}
as you saw up there the problem is that the loop does not continue after the users has entered his code, i am wondering why this is and if you have a better purpose for me. I am new to the C language help is much appreciated!!!!!!
#include <stdlib.h>
#include <stdio.h>
int main()
{
int randomNumber = 11;
int usersGuess;
int i;
do {
printf("You need to guess a number between 0 and 20! Good Luck! \n");
for (i = 5; i > 0; i--) {
printf("You have got %d amount of tries, Guess The random number: ", i);
scanf_s("%d", usersGuess);
if (usersGuess == randomNumber) {
printf("You won");
break;
} else if (usersGuess > randomNumber) {
printf("That is wrong, random number is less than that");
} else if (usersGuess < randomNumber) {
printf("that is wrong, the random number is higher than that");
} else if (usersGuess > 20) {
printf("please guess again cause the random number is between 0 and 20");
}
}
} while(i > 0);
return 0;
}
Your code has Undefined Behaviour, which means it's buggy and anything can happen. The problem is that you're passing an integer to scanf_s where it want a pointer. Do this:
scanf_s("%d", &usersGuess);
The reason is that you want the function to write into the variable usersGuess. In C, all parameters are passed by value, so if you want an output parameter, you have to make it a pointer.
My assignment is to check if a number is prime, but I have to use three sections to do it. The first is the main body of code and that is followed by two functions. The first checks if the number is even, and the second checks if it is prime. I know this is a rather tedious way to check if a number is prime but it is meant to get us introduced to functions and function calls!
UPDATE
I have gotten it all to work besides printing the smallest divisor of a non prime number. I thought using i from the second function would work but it will not output. I have copied by code below -- please help if you have any suggestions!
#include <stdio.h>
#include <math.h>
int even (int);
int find_div (int);
int main() {
int num, resultEven, resultPrime, i;
printf("Enter a number that you think is a prime number (between 2 and 1000)> \n");
scanf("%d", &num);
while (num < 2 || num > 1000) {
if (num < 2) {
printf("Error: number too small. The smallest prime is 2.\n");
printf("Please reenter the number > \n");
scanf("%d", &num);
}
else if (num > 1000) {
printf("Error: largest number accepted is 1000.\n");
printf("Please reenter the number > \n");
scanf("%d", &num);
}
else {
}
}
resultEven = even(num);
resultPrime = find_div(num);
if (resultEven == 1) {
printf("2 is the smallest divisor of %d. Number not prime\n", num);
}
else if (resultPrime == 1) {
printf("%d is the smallest divisor of %d. Number not prime\n", i, num);
}
else {
printf("%d is a prime number.\n", num);
}
return 0;
}
int even(int num) {
if (num % 2 == 0) {
return 1;
}
else {
return 0;
}
}
int find_div(int num) {
int i;
for (i = 2; i <= (num/2); i++) {
if (num % i == 0) {
return 1;
}
if (num == i) {
return 0;
}
}
return i;
}
I would create a function for Wilsons theorem (p-1)! = 1 (mod p) iff p is prime, first off, to make the functions nice and easy you will only need the one. for numbers less than 1000 it should work fine.
something like,
//it will return 1 iff p is prime
int wilson(int p)
{
int i, result = 1;
for (i = 0; i < p; i++)
{
result *= i;
result = result % p;
}
return result;
}
however if your not printing check that you have included, at the top of your file
#include <stdio.h>
your
resultEven = even(num)
needs a ; at the end but that was mentioned in the comments, besides that your methodology though odd is correct, also you do not need the empy else, that can simply be removed and your good
UPDATE:
//if return value == 1 its prime, else not prime, and
//return value = smallest divisor
int findDiv(int p)
{
int i= 0;
for (; i <= n/2; i++)
{
//you number is a multiple of i
if (num % i == 0)
{
//this is your divisor
return num;
}
}
//1 is the largest divisor besides p itself/smallest/only other
return 1;
}
your function call is correct but you need a semi colon (;) at the end of:
resultEven = even(num)
otherwise this program effectively checks for evenness. To check for prime one way is to ensure the number has no factors other than one and itself. This is done by finding the div of every whole number from 2 to half of the number being tested using a for loop. If a number produces a div of 0 then it is not prime because t has a factor other than 1 and itself.
I am taking an online C class, but the professor refuses to answer emails and I needed some help.
Anyways, our assignment was to write a program that takes an integer from the user and find the largest even digit and how many times the digit occurs in the given integer.
#include <stdio.h>
void extract(int);
void menu(void);
int main() {
menu();
}
void menu() {
int userOption;
int myValue;
int extractDigit;
do {
printf("\nMENU"
"\n1. Test the function"
"\n2. Quit");
scanf("%d", &userOption);
switch (userOption) {
case 1:
printf("Please enter an int: ");
scanf("%d", &myValue);
extractDigit = digitExtract(myValue);
break;
case 2:
printf("\nExiting . . . ");
break;
default:
printf("\nPlease enter a valid option!");
}
} while (userOption != 2);
}
void digitExtract(int userValue) {
int tempValue;
int x;
int myArr[10] = { 0 };
tempValue = (userValue < 0) ? -userValue : userValue;
do {
myArr[tempValue % 10]++;
tempValue /= 10;
} while (tempValue != 0);
printf("\nFor %d:\n", userValue);
for (x = 0; x < 10; x++) {
printf("\n%d occurence(s) of %d",myArr[x], x);
}
}
I have gotten the program to display both odd & even digit and it's occurrences.
The only part that I am stuck on is having the program to display ONLY the largest even digit and it's occurrence. Everything I've tried has either broken the program's logic or produces some weird output.
Any hints or ideas on how I should proceed?
Thanks ahead of time.
Run a loop from the largest even digit to smallest even digit.
for (x = 8; x >=0; x-=2)
{
if(myArr[x]>0) //if myArr[x]=0 then x does not exist
{
printf("%d occurs %d times",x,myArr[x]);
break; //we have found our maximum even digit. No need to proceed further
}
}
Note:To optimize you should count and store occurrences of only even digits.
Why do you even use the extra loop? To find the largest even digit in an integer and the number of its occurences, a modification to the first loop would suffice.
Consider the following (untested, but I hope you get the idea):
int tempValue;
int x;
int myArr[10] = { 0 };
int maxNum = 0;
tempValue = (userValue < 0) ? -userValue : userValue;
do {
int currNum = tempValue % 10;
myArr[currNum]++;
tempValue /= 10;
if (currNum % 2 == 0 && currNum > maxNum)
maxNum = currNum;
} while (tempValue != 0);
After this, maxNum should contain the largest even digit, and myArr[maxNum] should be the number of its occurences.
I've created a program to determine largest number, but my lecturer says it isn't perfect, can anybody make it perfect?
#include <stdio.h>
int main () {
double a,b=0,n, i;
printf("limit of n input: ");
scanf ("%lf",&n);
for (i=1;i<=n;i++) {
scanf("%lf",&a);
if (a>b) b=a;
}
printf("%.2lf", b);
return 0;
}
If by "not perfect" she meant "doesn't properly handle negative numbers or an empty set", then you'd want to
Treat n<1 as a special case (why should 0 be the largest of an empty set?)
Read the first number outside of the loop, so you're not making as assumption as to the smallest possible number
I would do it that way, sorry for the mass of text. I think it is coming from the typical Objective-C style programming with long words:
#include <stdio.h>
int clean_stdin() {
while (getchar()!='\n');
return 1;
}
int main(int argc, char *argv[]) {
char c;
signed int count = 0; // number of numbers to scan
unsigned int fireErrorMessage = 0;
do {
if (fireErrorMessage == 1) {
printf("You entered not a positive natural number. Please enter a number >0 Examples: 1 22 4012\n"); // output for the user
}
if (fireErrorMessage == 0) {
fireErrorMessage = 1;
}
printf("How many integers do you want to insert (Inser a number >0)? ");
} while (((scanf("%d%c", &count, &c) != 2 || c != '\n') && clean_stdin()) || count < 1);
signed int indexOfNumber; // for index, declared outside because of output at the end
signed int highestNumberIndex;
double highestNumber; // saving the highest value in a helper variable
fireErrorMessage = 0;
for (indexOfNumber = 1; indexOfNumber <= count; indexOfNumber++) {
double scannedNumber;
do {
if (fireErrorMessage == 1) {
printf("You entered not a number. Please enter a number. Examples: 3.0 -1 14\n"); // output for the user
}
if (fireErrorMessage == 0) {
fireErrorMessage = 1;
}
printf("Input number %d: ", indexOfNumber); // output for the user
} while (((scanf("%lf%c", &scannedNumber, &c) != 2 || c != '\n') && clean_stdin()));
fireErrorMessage = 0;
if (indexOfNumber == 1 || scannedNumber > highestNumber) {
highestNumberIndex = indexOfNumber;
highestNumber = scannedNumber;
}
}
printf("Highest input number on index %d, the value is about %.2lf\n", highestNumberIndex, highestNumber);
return 0;
}
Output
How many integers do you want to insert (Inser a number >0)? aa5
You entered not a positive natural number. Please enter a number >0 Examples: 1 22 4012
How many integers do you want to insert (Inser a number >0)? -3
You entered not a positive natural number. Please enter a number >0 Examples: 1 22 4012
How many integers do you want to insert (Inser a number >0)? 3
Input number 1: aa
You entered not a number. Please enter a number. Examples: 3.0 -1 14
Input number 1: -50.0001
Input number 2: 51a
You entered not a number. Please enter a number. Examples: 3.0 -1 14
Input number 2: -1.00
Input number 3: -0.1
Highest input number on index 3, the value is about -0.10
This code caters for negative, not a number input for the loop index as well as negative and not a number inputs inside the loop. Thanks
#include <stdio.h>
#include <math.h>
int main () {
int n, i;
double a,b=0;
printf("limit of n input: ");
scanf ("%lf",&n);
if(n < 0){
printf("value of n cannot be negative");
return 0;
}
else if (n == 0)
return 0;
else if (isnan(n))
return 0;
else{
for (i=1;i<=n;i++)
{
scanf("%lf",&a);
if(!isnan(a) && a > 0)
{
if (a>b) b=a;
}
}
printf("%.2lf", b);
return 0;
}
}