Guess the number game missing a loop - c

I was trying to do a "Guess the number" game that would use a verification to see if the number is low, high or equal to the random number. I want to do a loop verification that will say if the number is to high and to try again!
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <cs50.h>
int
main(void)
{
srand(time(NULL));
int skittles = rand() % 1024;
printf("%d\n", skittles);
printf("choose a number betwen 1 to 1024\n");
int numpessoa = GetInt();
printf("%d\n", numpessoa);
if (numpessoa < skittles)
{
printf("numpessoa < skittles");
numpessoa = GetInt();
}
if (numpessoa > skittles)
{
printf("numpessoa > skittles");
numpessoa = GetInt();
}
if (numpessoa == skittles)
{
printf("numpessoa == skittles");
}
return 0;
}

If this is an homework,you shouldn't ask its solution directly.
I only can provide some pseudocode.
loop forever
get user's guess
check guess against answer
guess differs answer ,then provide some hints
guess equals answer,tell user he/she is right and break out of this loop
And there is an another bug in your code.
The 1024 modulo of an random integer is between 0 and 1023.

Related

Trying to use random number generator in a coin flip program in C

Hello i'm trying to create a program where it counts the number of heads and tails in a coin flip simulator based on the number of coin flips the user inputs. The issue with this is that when i'm trying to count the number of heads and tails then increment them, it gives me just a random number and not within the range of flips given.
This is a sample output of my error
Coin Flip Simulator
How many times do you want to flip the coin? 10
Number of heads: 1804289393
Number of tails: 846930886
Instead I want it to count it like this:
Coin Flip Simulator
How many times do you want to flip the coin? 10
Number of heads: 7
Number of tails: 3
Here's the program :
#include <stdio.h>
#include "getdouble.h"
#include <time.h>
#include <stdlib.h>
int main(void) {
printf("Coin Flip Simulator\n");
printf("How many times do you want to flip the coin? ");
int numFlip = getdouble();
if(numFlip == 0 || numFlip < 0) {
printf("Error: Please enter an integer greater than or equal to 1.\n");
}
int i = 0;
int numHead = rand();
int numTails = rand();
for (i = 0; i < numFlip; i++) {
if(numFlip%2 == 0) {
numHead++;
}
else {
numTails++;
}
}
printf("Number of heads: %d\n", numHead);
printf("Number of tails: %d\n", numTails);
return 0;
}
Thanks for the suggestions, it's my first time trying to use the random number generator.
I suggest you use an unsigned integer type for numFlip. The main issue is that you need to initialize numHead (sic) and numTails. You want to use srand() to seed the random number generate otherwise the result is deterministic. As you only have two options just record number of heads and determine the tails after the fact:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(0));
printf("Coin Flip Simulator\n");
printf("How many times do you want to flip the coin? ");
unsigned numFlip;
if(scanf("%u", &numFlip) != 1) {
printf("numFlip failed\n");
return 1;
}
unsigned numHeads = 0;
for(unsigned i = 0; i < numFlip; i++)
numHeads += (rand() % 2); // removed ! when #Fe2O3 wasn't looking :-)
unsigned numTails = numFlip - numHeads;
printf("Number of heads: %u\n", numHeads);
printf("Number of tails: %u\n", numTails);
}
and example output:
Coin Flip Simulator
How many times do you want to flip the coin? 10
Number of heads: 2
Number of tails: 8

How to quit loop when requirement is met in C

I have made a while loop and it works partly. I want the code to stop when the values entered are under the parameter, but it keeps going regardless of the output. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main()
{
// defining variables until "till here" comment
int i;
int rollDice;
int firInp;
int secInp;
srand (time(NULL)); // seeding rand so that we get different values every time
// till here
while(rollDice > 0)
{
printf("Enter the amount of faces you want your dice to have (MAX=24, MIN=1): "); // prints the message
scanf("%d", &firInp); // user input stored into firInp
printf("Enter the amount of throws you want(MAX=499, MIN=1): "); // this message is printed after the users first input
scanf("%d", &secInp); // user input stored into secInp
if (((firInp < 25)&&(firInp > 1))&&((secInp < 500)&&(secInp > 1))){ // if statement to check parameters
for(i = 0; i < secInp; i++){
rollDice = (rand()%firInp) + 1;
printf("%d \n", rollDice);
}
}
else{
printf("Sorry, these numbers don't meet the parameters\nPlease enter a number in the right parameters.\n");
}
}
return 0;
}
I'm new to C btw.
edit: I want the loop to continue if the user input is more than 24, 499 respectively.
What you're doing is wrong. Variable rollDice is for storing the values of the outcomes rather than doing a condition check. It will have random values and since the values on the dice can't be negative or zero it may not exit the while loop. I don't know what will rand() will produce so I'm just assuming.
The range for rand() is [0,RAND_MAX), including zero and excluding RAND_MAX. But because of this expression (rand()%firInp) + 1 , you're adding one to it. So it will never become Zero.
You can use a flag variable and set it to 1. When the if conditions are met, you can set the flag to 0. It will exit the while loop.
Corrected code :-
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main()
{
// defining variables until "till here" comment
int i;
int rollDice;
int firInp;
int secInp;
int flag = 1;
srand (time(NULL)); // seeding rand so that we get different values every time
// till here
while(flag)
{
printf("Enter the amount of faces you want your dice to have (MAX=24, MIN=1): "); // prints the message
scanf("%d", &firInp); // user input stored into firInp
printf("Enter the amount of throws you want(MAX=499, MIN=1): "); // this message is printed after the users first input
scanf("%d", &secInp); // user input stored into secInp
if (((firInp < 25)&&(firInp > 1))&&((secInp < 500)&&(secInp > 1))){ // if statement to check parameters
for(i = 0; i < secInp; i++){
rollDice = ((rand() + 1)%firInp);
printf("%d \n", rollDice);
}
flag = 0;
}
else{
printf("Sorry, these numbers don't meet the parameters\nPlease enter a number in the right parameters.\n");
}
}
return 0;
}
EDIT :-
Also, division with 0 is undefined. rand() can attain value 0. You should add 1 to rand() rather than adding to whole modulus. It can create an error if the rand() will give 0 as an output.

find sum of infinite series.Why does it issue a zero amount?

You need to find the sums of an infinite series with a given accuracy.(the picture with the task is given as a link)
the program constantly counts the zero amount. I don't understand what my mistake is
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int i;
int n =1;
double E,x,q;
double sum = 0;
scanf ("%f",&E) ;
scanf ("%f",&x) ;
q=pow(x,3)/6;
while(fabs(q)>=E){
if (n/2==0) {
sum=sum+q;
}
else {
sum=sum-q;
}
q=(q*pow(x,2))/(n+3);
n=n+1;
}
printf("%f",sum);
return 0;
}
It will never going to enter to that if statement. You are letting n=1 and it will be always 1, and u are doing n=n+1 in the else.

Checking for a prime number through user input in a given range in C

#include <stdio.h>
#include <stdlib.h>
int main()
{
int n=0;
int x;
printf("Please enter a number:");
scanf("%d", &n);
(x<=1)&&(x>=1000)&&(x=(!(n)));
if((n/1)&&(n/n)&&(!(n/x)))
{
printf("P\n");
}
else
{
printf("C\n");
}
system("pause");
return 0;
}
My code keeps outputting "P" from the if statement when run, can anyone explain why? Is there a way to make the code work with just if and else functions? If so please help
I believe the intent of this if((n/1)&&(n/n)&&(!(n/x))) statement is saying "if n is divisible by one and if n is divisible by n and if n is not divisible by x" however this is not how we express these ideas in C. Look at the % or modulo operator and see if you can figure out how we can express these ideas using logic

Random number generator game in C

I have started C recently and am having trouble make the computer think of a random number.
This is the code so far. I need help!
#include <stdio.h>
#include <stdlib.h>
int main ()
{
time_t t;
int userin;
printf("Guess a number from 1 to 10\n");
scanf("%d", userin);
int r = rand() % 11;
if (r == userin)
{
printf ("you are right");
}
else
{
printf("Try again");
}
return 0;
}
Thx a lot guys it worked out!!!!
In your code, r will be a random number from 0 to 10. For a random number between 1 and 10, do this:
int r = rand() % 10 + 1;
Also, you should call
srand(time(NULL));
at the beginning of main to seed the random number generator. If you don't seed the generator, it always generates the same sequence.
There is issue in your scanf statement as well.
You should use
scanf("%d", &userin);
instead of
scanf("%d", userin); /* wrong - you need to use &userin */
scanf needs the address of variables at which it will store the value. For a variable, this is given by the prefexing the variable with &, as in &userin.
There are few issues in your code.
not reading into the address & of your variable using scanf
not considering "legitimate" values of input, result of rand()%11 can also be 0
not checking against "illegal" input values, which can "alias" the result.
not properly initializing seed of the pseudo-random rand() function, so it always returns the same result.
Using printf for debugging your code, as in the following example, based on your code can help a lot:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DEBG 1
int main (void)
{
time_t t;
int userin;
printf("Guess a number from 1 to 10\n");
if(scanf("%d", &userin) != 1){ // read into the variable's address
printf("Conversion failure or EOF\n");
return 1;
}
if(userin < 1 || userin > 10){ // check against "illegal" input
printf("Offscale, try again\n");
return 1;
}
srand(time(NULL)); // initialize the seed value
int r = 1 + rand() % 10; // revise the formula
if (DEBG) printf("%d\t%d\t", r, userin); //debug print
if (r==userin){
printf ("you are right\n");
}else{
printf("Try again\n");
}
return 0;
}
Please, also consult this SO post.
Problems :
scanf("%d", userin); //you are sending variable
This is not right as you need to send address of the variable as argument to the scanf() not the variable
so instead change it to :
scanf("%d", &userin); //ypu need to send the address instead
and rand()%11 would produce any number from 0 to 10 but not from 1 to 10
as other answer suggests, use :
(rand()%10)+1 //to produce number from 1 to 10
Solution :
And also include time.h function to use srand(time(NULL));
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int userin;
printf("Guess a number from 1 to 10\n");
scanf("%d", &userin);
int r = (rand() % 10)+1;
if (r==userin)
{
printf ("you are right");
}
else
{
printf("Try again");
}
return 0;
}
Why use srand(time(NULL)) ?
rand() isn't random at all, it's just a function which produces a sequence of numbers which are superficially random and repeat themselves over a period.
The only thing you can change is the seed, which changes your start position in the sequence.
and, srand(time(NULL)) is used for this very reason
This should work
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int userIn = 0; //I like to initialize
printf("Guess a number from 1 to 10\n");
scanf("%d", &userIn);
srand(time(NULL)); //seed your randum number with # of seconds since the Linux Epoch
int r = (rand()%10)+1; //rand%11 gives values 0-10 not 1-10. rand%10 gives 0-9, +1 makes sure it's 1-10
if (r == userIn)
{
printf ("you are right\n");
}
else
{
printf("Try again\n");
}
return 0;
}
Edit: You may want to implement code to verify that the user input is in fact an integer.

Resources