LOGO
General Discussion Undecided where to post - do it here.

Reply to Thread New Thread
Old 08-12-2006, 03:10 AM   #1
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default Could anyone help me with C
I have to write a program that:
ask the user to guess a number between 1 and 20. The computer has picked a number between 1 and 20. If the user picks the right number, then the computer prints out the ‘Congratulations’ and game ends, else the user has five chances, if the user can not find the right answer in five guesses, the computer prints the message, ‘You Loose’ and the program stops.

and so far i have:
{
int y,z;
y=1+rand()%20;
printf("Please Guess the Number between 1 and 20, You have five tries.\n");
while(z!=y){
scanf("%d", &z);
if (z==y){printf("Congratulations\n");}
}
}
which basically will ask them the number and if they get it right ends the program

but how would i limit this to 5 gusses, im guessing to use a for loop like

for(int x=1;x
LSDDSL is offline


Old 08-12-2006, 03:46 AM   #2
KahiroSamo

Join Date
Oct 2005
Posts
461
Senior Member
Default
pseudo code that may work...

Code:
Code
int number = 1+rand()%20; boolean done = false; int tries = 5; int guess;  while(!done && tries >0){     printf("Please Guess the Number between 1 and 20, You have %d tries remaining.\n", tries);     scanf("%d", &guess);     tries--;     if(guess==number){         printf(blah);         done = true;     }  }

You may want to put something in so that if a user enters something outside the range it doesn't count as one of their tries.

I feel like I've given too much away possibly!
KahiroSamo is offline


Old 08-12-2006, 03:47 AM   #3
h4z1XBI7

Join Date
Oct 2005
Posts
554
Senior Member
Default
Can you have two conditions in a while loop? If you can then you could have the loop add 1 to another variable for an incorrect guess then have the while loop stop when that integer equals 5 (ie go while its not equal to 5). You might be able to use something like:

while((z!=y)&&(w!=5))

Although I'm not sure if it'll work...worth a try anyway!


Edit: Meh, TomG's better...
h4z1XBI7 is offline


Old 08-12-2006, 05:25 AM   #4
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default
pseudo code that may work...

Code:
Code
int number = 1+rand()%20; boolean done = false; int tries = 5; int guess;  while(!done && tries >0){     printf("Please Guess the Number between 1 and 20, You have %d tries remaining.\n", tries);     scanf("%d", &guess);     tries--;     if(guess==number){         printf(blah);         done = true;     }  }

You may want to put something in so that if a user enters something outside the range it doesn't count as one of their tries.

I feel like I've given too much away possibly!
eh ive never learned any boolean commands yet so i think he would think i was cheating

weve basically learned functions, random numbers, for and while loops and other random crap

BUT! some of the code helped me get it right! thanks alot man!
LSDDSL is offline


Old 08-12-2006, 05:29 AM   #5
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default
could you also tell me how i could possibly generate 100 random numbers without having to type it all out :O
LSDDSL is offline


Old 08-12-2006, 05:30 AM   #6
KahiroSamo

Join Date
Oct 2005
Posts
461
Senior Member
Default
eh ive never learned any boolean commands yet so i think he would think i was cheating

weve basically learned functions, random numbers, for and while loops and other random crap

BUT! some of the code helped me get it right! thanks alot man!
Erm the condition for while and for loops must evaluate to a boolean.

You could replace boolean with int, true with 1 and false with 0 to get the same effect, though it seems silly if you have not been taught about all the basic types. It's very easy anyway.

Just wait till you get to start doing pointers and stuff with memory, you will probably see "Segmentation Fault" a lot!

Edit:
could you also tell me how i could possibly generate 100 random numbers without having to type it all out :O
You need to set up an int array of size 100 then iterate over it with a while loop. Inside the body of the loop generate a random number and assign it to the value in the array at that position and then increment the array index number. If you don't know how to use arrays - google!
KahiroSamo is offline


Old 08-12-2006, 06:17 AM   #7
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default
yeah i got the 100 random numbers generated no problem

sorry for the noobish questions, i have really bad teacher....

srand(time(0));
int x,y;
for(x=1;x
LSDDSL is offline


Old 08-12-2006, 06:22 AM   #8
GypeFeeshyTes

Join Date
Oct 2005
Posts
594
Senior Member
Default
srand(time(0));
int x,y;
for(x=1;x
GypeFeeshyTes is offline


Old 08-12-2006, 06:25 AM   #9
KahiroSamo

Join Date
Oct 2005
Posts
461
Senior Member
Default
that gets me the hundred random numbers, what i now need to do is have it only display the ones that are divisible by 2 and 3.....(into a whole number i believe )

im not sure on how to do this besides maybe set up a case-switch/if statement but not sure how would i would put it, but that might be the wrong direction....

and thanks for the help TomG
There are a couple of ways you could do it:

1. Make a function that takes a number and returns a boolean - true if it's divisible by 2 or 3 and false otherwise. Then have an if statement that only printf's it if that function returns true on it.

2. Make the same function as before then generate the random numbers and only store the divisible ones in the array. Then iterate over the array printf-ing them all.
KahiroSamo is offline


Old 09-11-2006, 08:44 AM   #10
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default
ahh thanks to you both! got it working

i dont know why he is testing us with things we havent even learned yet?

edit: ahh getting these programs betetr now, my teacher barely speaks english as well so yeah this helps alot

what does the %d mean? i always put it in the scanf functions but thats just because he told me to and it works, he never explains why
LSDDSL is offline


Old 09-11-2006, 09:14 AM   #11
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default
ah **** guys one more questions

i have to do this: 4- Write a program in C that has the following output:
Please make a choice:
1-grade calculation
2-end
Sample Input 1
Please input your first grade: 76
Please input your second grade: 84
Please input your third grade: 92
The average of your grades is: 84.0
This corresponds to a grade of: B
Do more calculations?(Y or N)
Sample Input: N
Good By


so far i have

void main()
{
int a,b,c,d,h,z,y;
while(a!=2){
printf("Please make a choice\n");
printf("1-Grade Calculation\n");
printf("2-END\n");
scanf("%d", &a);
if (a==1){ printf("Please enter your first grade:");
scanf("%d", &b);
printf("\n");
printf("Please enter your second grade:");
scanf("%d", &c);
printf("\n");
printf("Please enter your third grade:");
scanf("%d", &d);
printf("The average is:");
h=(b+c+d)/3;
printf("%d", h);
printf("\n");
if(h>=90 && h=80 && h=70 && h=60 && h
LSDDSL is offline


Old 09-11-2006, 09:54 AM   #12
PetraCromlich

Join Date
Oct 2005
Posts
467
Senior Member
Default
Do you not have a textbook?

If not this is an excellent C tutorial:
http://www.crasseux.com/books/ctutorial/
PetraCromlich is offline


Old 09-11-2006, 10:15 AM   #13
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default
yeah but he doesnt tell us to use it
and he doesnt teach along with it
and he never refers to it, or points out pages to use
and i go searching through and only find chunks of what he teaches at certain points


and he cant speak english :P

thanks for the guide ill take a look at it
LSDDSL is offline


Old 10-11-2006, 11:15 AM   #14
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default
bumP!!!!
LSDDSL is offline


Old 10-11-2006, 11:49 AM   #15
Manteiv

Join Date
Oct 2005
Posts
371
Senior Member
Default
Code: [code]int main() { char continue = 'y'; do { int *pGrades; int numGrades; int total = 0; float average; cout > numGrades; if( numGrades < 1 ) { cout
Manteiv is offline


Old 10-11-2006, 12:23 PM   #16
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default
lol how would i do it coding in c then?
LSDDSL is offline


Old 10-11-2006, 01:03 PM   #17
Manteiv

Join Date
Oct 2005
Posts
371
Senior Member
Default
lol how would i do it coding in c then?
Figure it out?
Manteiv is offline


Old 10-11-2006, 01:16 PM   #18
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default
Figure it out?
i'd love too....
LSDDSL is offline


Old 10-11-2006, 01:28 PM   #19
LSDDSL

Join Date
Oct 2005
Posts
619
Senior Member
Default
void main()
{

int a,b,c,d,h,z;
int y;
while(a!=2){
printf("Please make a choice\n");
printf("1-Grade Calculation\n");
printf("2-END\n");
scanf("%d", &a);
if (a==1){
printf("Please enter your first grade:");
scanf("%d", &b);
printf("\n");
printf("Please enter your second grade:");
scanf("%d", &c);
printf("\n");
printf("Please enter your third grade:");
scanf("%d", &d);
printf("The average is:");
h=(b+c+d)/3;
printf("%d", h);
printf("\n");
if(h>=90 && h=80 && h=70 && h=60 && h
LSDDSL is offline


Old 10-11-2006, 09:44 PM   #20
ValdisSeroff

Join Date
Oct 2005
Posts
566
Senior Member
Default
It looks good, you're almost there.

As for your question, to make the scanf() function accept letters instead of numbers, change the format parameter (1st parameter) to "%c", which means single character (%d means decimal integer). Then use an "if" statement to test if the character is a "y" or "n"

So, the code (with changes in bold) should look something like this:

Code: [code]void main() { int a,b,c,d,h,z; while(a!=2){ char y; printf("Please make a choice\n"); printf("1-Grade Calculation\n"); printf("2-END\n"); scanf("%d", &a); if (a==1){ printf("Please enter your first grade:"); scanf("%d", &b); printf("\n"); printf("Please enter your second grade:"); scanf("%d", &c); printf("\n"); printf("Please enter your third grade:"); scanf("%d", &d); printf("The average is:"); h=(b+c+d)/3; printf("%d", h); printf("\n"); <b>if(h>=90 && h=80 && h=70 && h=60 && h=0 && h
ValdisSeroff is offline



Reply to Thread New Thread

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

All times are GMT +1. The time now is 03:34 PM.
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.0 PL2
Design & Developed by Amodity.com
Copyright© Amodity