[Show all top banners]

Robert Frost
Replies to this thread:

More by Robert Frost
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 C Program help

[Please view other pages to see the rest of the postings. Total posts: 38]
PAGE:   1 2 NEXT PAGE
[VIEWED 5671 TIMES]
SAVE! for ease of future access.
The postings in this thread span 2 pages, View Last 20 replies.
Posted on 10-17-04 5:09 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Hi all!
Anybody done a C Program for "guessing a secret number"?
 
Posted on 10-17-04 7:00 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

ohh shit..few days ago i just deleted'em from secure shell! damn!
i will try to see if my friend still has it!
 
Posted on 10-17-04 7:14 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

There you go..some tech people...

Anyway we should have a thread, which deals with programing problems and stuffs like that...I mean considering there are so many Nepali students in computer related field...

anyway I have a questions myself. I am newbie to PERL...I tried to install that think with Apache server in my home computer...but whenever I try to install something using PPM ..I get error message..like " bad protocol"..think it is not properly hooked up....I tried google with no success...I think you need to change something in enviroment variables....not sure...any help in this regard will be appreciated..
 
Posted on 10-17-04 7:45 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

dude, i found the one with guessing DICE, but not the guessing numbers. sorry

 
Posted on 10-17-04 9:40 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Could you post that one. Sounds like the similar process.

I got a problem, here is the code for a program that the assignment is based upon:

#include

void main()
{
const int NUM = 10;

int Guess ;

char answer;

for (Guess = NUM; (Guess >0) &&(answer != 'y'); --Guess)
{
printf( "I will guess your secret number if the number is between 0 and 100ý);

printf( "Is Your guess %d \n", Guess);

scanf(ý %cý, &answer);
}
if (answer =='y')
printf( "Wow my guess is right \n");
else
printf( "you Cheatedý);
}

Now I gotta do the same thing but here are some other criterias:
1) Extend this program with a while loop
2) Guess the number as long as the guess is incorrect.

I presume when you put the if statement code inside a while loop, it automatically goes back into putting a new value into it and asking the user if this is what he had guessed, if not, go back again and try it till he says yes.

Something like it. I have been trying hard, but since I donot have a clear cut basics on C-Programming, I am finding this simple program mighty headache.
Can somebody help please....



 
Posted on 10-17-04 10:21 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

RF, two things
a. Did you write down above code?
b. You mean insert the while loop in the middle of code without modifying the above code? (Like without modifying for-loop?) coz it seems easy if I take that for-loop out and just use the while loop. Dunno... Just curious...
 
Posted on 10-17-04 10:22 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

RF, two things
a. Did you write down above code?
b. You mean insert the while loop in the middle of code without modifying the above code? (Like without modifying for-loop?) coz it seems easy if I take that for-loop out and just use the while loop. Dunno... Just curious...
 
Posted on 10-17-04 10:32 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

This is the actual assignment:

a) Write a program to find a secret number assigned to a variable.
b) Extend the Guess of my number program that I posted above.
c) Use a while loop
d) Make sure to guess the number as long as the Guess is incorrect.

Thanks
 
Posted on 10-17-04 10:41 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

hey Robert...im not good in explaining so im asking me fren to explain...hope it helps..did a smiliar program in Java..but already deleted it..so guess cant pass it to u to help..anyways hope what me fren says...helps..he is a self acclaimed programming god ;o) well he is good..but not that great...shhhhhhh ;o)...


I dont really understand what the problem is. It would be more helpful if you posted the actual assignnment specifications. Anyways i'm assuming what you have to do is make a program such that the program asks the user to think of a number between a particular limit(eg. 1 to 100) and then tries to guess the number in a certain number of attempts
The best way to do this is using binary search method. I havent done C in a long time so i dont really remember the syntax but i can write the pseudocode for you


{
guess the middle number of the limit(eg. 1-100). lets call it G
ask the user if the number he/she thought of is less than, equal to or greater than G
if its equal then the computer's guess is correct and program terminates saying so.
If its less than G, then change the limit to lower limit(eg. 1) up to G-1
If its more than G, then change the limit to G+1 upto upper limit(eg. 100)
}
repeat this loop


have fun programming..its fun aint it?well it is when u can make it work hehe..

danny
 
Posted on 10-17-04 11:16 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Your fren is right. The problem I have here is with the coding and also I don't think you have to go that far in saying
if its greater then or less than.
because,
lets say the number the user guessed is 9,
if the number given by the computer is 9, he will hit 'y' for Yes, and the program will terminate
if the number is incorrect say 2, he will hit 'n' for No, and the program will loop back to providing user with another number. And this will continue until the computer finds the right number that is 9.
But in this program, if 9 is the number, that should find it in second time since the program continues to give a number start from 10 and then going down. So when the computer gives 10 as the first number, the user will hit 'n' and then the next number will be 9.
so on and so forth.

hyaaa tauko dukyo.....



 
Posted on 10-18-04 12:37 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

robert dude!!mero pani tauko dhukio reading what u posted!!!!!!!ARGHHHHHHHHHHHHHH!!!

plus im already getting a headache doin some programming assignments due tomorrow :oS...pretty huge :oS...dyam why does other ppl assignments seems more fun to do and easier..;oP...

anyways dun really understand C but looking at ur code ..it does say ur program willl guess a number between 0-100..am i wrong?..or is it 10?so any numbers btween 0-10?

printf( "I will guess your secret number if the number is between 0 and 100ý);
is there a typo here with the 100? cos if its between 0-10...pretty easy ;oP

for (Guess = NUM; (Guess >0) &&(answer != 'y'); --Guess)
since NUM was instantiated as 10..
this code does say the program starts guessing from 10..and goes down everytime the guess is wrong until u reach 1....if the user says the program didnt guess it right even when the gues was 1..well..the user cheated...

i think ur supposed to change the code and use while-loop instead of for-loop arent u?klanki bro help him...i think u know C hehe...

anyways heres roughly what i would do..



void main()
{

int Guess=10 ; //let guess start from 10
char answer;
printf( "I will guess your secret number if the number is between 0 and 10);

while (Guess > 0) //1st loop Guess equals 10
{
printf( "Is Your guess %d \n", Guess);
scanf( %c, &answer); //dunno if these 2 lines shud be placed here

if (answer != 'y'')
guess--; //1st loop number now goes down to 9&goes thru the loop again

else if (answer =='y')
printf( "Wow my guess is right \n");
}
printf( "you Cheatedý); //since u came out of the while loop witout getn a 'y'
//user cheated by not choosin a number btween 1-10
}



hope that gave u and idea..hope its right hehe...
dyam i gotta go back to me own work...ARGHHHH!!! anyone free to help in VB.NET?notes are shit!!!not helping..:oS...

im gone!!!woohoooo!!!
mero pani tauko esto ghumayko cha!!!hyaaaaaa!!


danny

 
Posted on 10-18-04 3:55 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

dananah le hasaayo... hehe. Mero ni scene gaayeb bho yaar. hehe.

RF bru, I think I know what you are talking about now...
Is the output going to be something like this? (I haven't worked on it)

****
Is your guess 10
answer: n
Is your guess 9
answer: y
Wow my guess is right

Want me to guess again?
new_answer: y

Is your guess 10
answer: n
Is your guess 9
answer: n
Is your guess 8
answer:y
Wow my guess is right

Want me to guess again?
new_answer: n

(quit the program)
*****

If thats how the output is going to look like, then you need to initialize another char variable, (new_answer).
Use the while loop right before the forloop.
code: while (new_answer!= 'n')
then put everything starting from forloop inside the while loop.
after that "cheated" part ask the user if he wants to play again. scan the value like you did before. then CLOSE the while loop...

Should give the output like I showed above... hehe... Otherwise we have to wake up sajha C++ gurus SM Sainju and echoes bro. :D:D:D Good luck.
 
Posted on 10-18-04 5:19 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

damn, you want the whole code? robert? lol
looks like danny and kalanka are helpin u out. kalanka ko mukha ma kalanka le ramro garos!
 
Posted on 10-18-04 9:23 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Here is the output for your code:

C:\C-Files\Module4-Secret>a
I will guess your secret number if the number is between 0 and 10.

Is Your guess 10? 'y' or 'n'? y
Wow my guess is rightIs Your guess 10? 'y' or 'n'? Is Your guess 9? 'y' or 'n'?
-----------------------------------------------------------------------------------------------------------
C:\C-Files\Module4-Secret>a
I will guess your secret number if the number is between 0 and 10.

Is Your guess 10? 'y' or 'n'? n
Is Your guess 9? 'y' or 'n'? Is Your guess 8? 'y' or 'n'? n
Is Your guess 7? 'y' or 'n'? Is Your guess 6? 'y' or 'n'? n
Is Your guess 5? 'y' or 'n'? Is Your guess 4? 'y' or 'n'? n
Is Your guess 3? 'y' or 'n'? Is Your guess 2? 'y' or 'n'? n
Is Your guess 1? 'y' or 'n'? you Cheated
-----------------------------------------------------------------------------------------------------------

Two problems:

a) Shouldn't the program terminate when I hit 'y' saying that the guess is right. Why its asking again??
b) Shouldn't the program ask one number at a time?? Why two numbers?

I think the program that you have given is in the right track. I haven't checked the code as yet. As I copied an ran the program to see if it worked.


 
Posted on 10-18-04 9:31 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

That 0-100 was a typo.

Kalanki,
Let me try what you have said.




 
Posted on 10-18-04 10:18 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

RF,
Write down
fflush(stdin);
right after the scanf(...); might fix the problem.
 
Posted on 10-18-04 12:23 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

hey robert...(said it like that hindi style hehehe ;oP)

u talking about me code there?it actually works?;oP....anyways
a) Shouldn't the program terminate when I hit 'y' saying that the guess is right. Why its asking again??>
u got to break out of the while-loop once the answer is "y"

 
else if (answer =='y')
{
printf( "Wow my guess is right \n");
break;
}


that should do the trick..hopefully hehe no idea of C syntax as i said...and hopefully for

b) Shouldn't the program ask one number at a time?? Why two numbers?

no idea of C syntax..so dunno what those few codes actually does...hope kalanki vors suggestion helps..he seem to have some idea whats goin hehe..small errors im sure u can debug hem..its fun debuggin aint it?;o)...

dyam aint there any helpful soul out there with good VB.NET Notes?hehehe
tauko still ghumming!!!!hyaaaaaa...:oS

laters...


danny

 
Posted on 10-18-04 1:17 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Here's something I just coded for you. It should meet all your requirements. Plus, it has a few extra features, such as, the numbers are guessed randomly (not in sequence, which is no fun!), the same number isn't guessed twice, and the user is able to play the game repeatedly.

I hope I just didn't do your entire homework for you!




 
Posted on 10-18-04 1:20 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

The program in action. Case 1:

 
Posted on 10-18-04 1:23 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 
 



PAGE:   1 2 NEXT PAGE
Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 7 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
whats wrong living with your parents ?
Now Trump is a convicted criminal .
Nims- एक उन्मत्त साँढे
cannot accept Visa candidates
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters