Convert to Absolute Values

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Convert to Absolute Values

 
0
  #1
Feb 12th, 2007
I have this program to write thats a guessing game. the user specifies the amount of rounds and only 5 attempts per round is allowed a round is over when the user guesses correctly or the number of attempts are up. A Message is displayed to inform the user whether he/she is cold, hot or warm based on a certain criteria:

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. /*Funtion Prototypes*/
  6.  
  7. int genRandom(void);
  8.  
  9.  
  10. int main(void)
  11.  
  12. {
  13. /*Declare variables*/
  14.  
  15. int rounds;
  16. int ranNum;
  17. int guess;
  18. int attempts=5;
  19. int score;
  20.  
  21.  
  22. printf("\nEnter the desired number of rounds\n");
  23. scanf("%d",&rounds);
  24.  
  25.  
  26. do{
  27. ranNum=genRandom();/*Call random function*/
  28.  
  29. printf("Your rannum is %d\n", ranNum);
  30.  
  31. do{
  32. printf("Guess the value of the random number\n");
  33. scanf("%d",&guess);
  34.  
  35. if(guess>100)
  36. {
  37. printf("Invalid!!!Your source base must be less than 100\n\n");
  38.  
  39. }
  40.  
  41. attempts=attempts-1;
  42.  
  43.  
  44. if (ranNum==guess)
  45. {
  46. printf("Congratulations,You are correct!\n");
  47.  
  48. }
  49.  
  50.  
  51. if ((ranNum-guess) >40)
  52. {
  53. printf("You are Cold\a\n");
  54. }
  55. else if (10<=(ranNum-guess)<=40)
  56. {
  57. printf("You are Warm\a\n");
  58.  
  59. }
  60. else if ((ranNum-guess) < 10)
  61. {
  62. printf("You are Hot!\a\n");
  63.  
  64. }
  65. }while(attempts!=0);
  66.  
  67. }while(rounds!=0);
  68.  
  69.  
  70. score=5*(5-attempts);
  71.  
  72. printf("Your score is %d\n", score);
  73.  
  74. }/* end of main*/
  75.  
  76. int genRandom(void)
  77. {
  78. return rand()%100;
  79. }
1. How do I work with only absolute or positive values for my criteria (say number is less than guess)
2. I want to break out of the loop when the guess is correct or when the attempts are up.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Convert to Absolute Values

 
0
  #2
Feb 13th, 2007
Some points:
  • To guarantee that a particular integer will always be postive (absolute), declare it as an unsigned int.
  • You check to see if the value the user entered was larger than 100, but you never checked if it was smaller than 0.
  • You can use break to jump out of a loop if you need to, although a better way would be simply to keep checking the value of a boolean variable which represents if it's time to exit the loop.
  • This statement makes no sense:
    1. if (10<=(ranNum-guess)<=40)
    Since you already checked that the difference is smaller than 40, just check that the difference isn't smaller than 10.
  • Proper code indentation is a code habit to practice. Many editors have a built-in feature, so you don't even have to think about it. And it makes code much easier to read and understand.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Convert to Absolute Values

 
0
  #3
Feb 13th, 2007
Originally Posted by joeprogrammer View Post
Some points:
  • To guarantee that a particular integer will always be postive (absolute), declare it as an unsigned int.
[LIST]

what if I typecast it
  1. ((unsigned int)(ranNum-guess) >40)
  • You can use break to jump out of a loop if you need to, although a better way would be simply to keep checking the value of a boolean variable which represents if it's time to exit the loop.
  • Do you mean like if (attempts>0=1&&rounds>0=1) ?

  • Proper code indentation is a code habit to practice. Many editors have a built-in feature, so you don't even have to think about it. And it makes code much easier to read and understand.
I been trying to work on my indentation and I thought I was getting better can you suggest a good code editor that would help?

Thanks.
Last edited by boujibabe; Feb 13th, 2007 at 10:53 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Convert to Absolute Values

 
0
  #4
Feb 13th, 2007
here is the revised code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*Funtion Prototypes*/
  5. int genRandom(void);
  6.  
  7. int main(void)
  8.  
  9. {
  10. /*Declare variables*/
  11.  
  12. int rounds;
  13. int ranNum;
  14. int guess;
  15. int attempts=5;
  16. int score;
  17.  
  18.  
  19. printf("\nEnter the desired number of rounds\n");
  20. scanf("%d",&rounds);
  21. do{
  22. ranNum=genRandom();/*Call random function*/
  23. --rounds;
  24. do{
  25. printf("Guess the value of the random number\n");
  26. scanf("%d",&guess);
  27. --attempts;
  28.  
  29. if((guess>100)||(guess<0))
  30. {
  31. printf("Invalid!!!your number must less than 100 or more than 0\n\n");
  32. continue;
  33. }
  34.  
  35. if (ranNum==guess)
  36. {
  37. printf("Congratulations,You are correct!\n");
  38. break;
  39. }
  40.  
  41. if ((unsigned int)(ranNum-guess) >40)
  42. {
  43. printf("You are Cold\n");
  44. }
  45. else if (10<=(unsigned int)(ranNum-guess))
  46. {
  47. printf("You are Warm\n");
  48. }
  49. else if ((unsigned int)(ranNum-guess) < 10)
  50. {
  51. printf("You are Hot!\n");
  52. }
  53. }while(attempts!=0);
  54. }while(rounds!=0);
  55.  
  56. score=5*(5-attempts);
  57.  
  58. printf("Your score is %d\n", score);
  59.  
  60. }/* end of main*/
  61.  
  62. int genRandom(void)
  63. {
  64. return rand()%100;
  65. }

The score is noncumulative and is suppsed to show at the end of each round
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Convert to Absolute Values

 
0
  #5
Feb 13th, 2007
You are getting incorrect output since you fail to reset the attempts value to 5 again i.e. at the end of the nested do while loop, set the value of attempts again to 5.

Also it is better to decrement the value of attempts at the end of the second do while loop, only after the successful completion of the loop since you seem to be using continue.

And btw, read this thread for getting a good IDE.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Convert to Absolute Values

 
0
  #6
Feb 13th, 2007
Thanks a bundle. It works now except when I'm correct I can't break out of the loop and print the score, also It isn't starting another round after the failed attempts.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Convert to Absolute Values

 
0
  #7
Feb 13th, 2007
Originally Posted by boujibabe View Post
Thanks a bundle. It works now except when I'm correct I can't break out of the loop and print the score, also It isn't starting another round after the failed attempts.
Hmm... the "break" statement works fine for me, except that you aren't calculating the score correctly. You should calculate the score right after the round finishes, and before you reset the attempts.

Like this:
  1. }while(attempts!=0);
  2. score+=5*(5-attempts);
Notice that I put += instead of just =, since the score is supposed to be accumlative. Which means you're going to have to initalize score at the beginning of your program.

You may want to print a "you failed" message before the round ends so that the user knows that he/she ran out of attempts, and additionally, print out the number of attempts remaining after each guess.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Convert to Absolute Values

 
0
  #8
Feb 14th, 2007
Originally Posted by boujibabe View Post
I been trying to work on my indentation and I thought I was getting better
Here's a tutorial about code formatting. Concentrate on whitespace as well as indentation. A differnt IDE won't do it all for you, and knowing how to format is extremely important.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Convert to Absolute Values

 
0
  #9
Feb 14th, 2007
Originally Posted by boujibabe View Post
Thanks a bundle. It works now except when I'm correct I can't break out of the loop and print the score, also It isn't starting another round after the failed attempts.
Post the updated code so I can see what you have come up with...
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Convert to Absolute Values

 
0
  #10
Feb 15th, 2007
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*Funtion Prototypes*/
  5. int genRandom(void);
  6.  
  7. int main(void)
  8.  
  9. {
  10. /*Declare variables*/
  11.  
  12. int rounds;
  13. int ranNum;
  14. int guess;
  15. int attempts=5;
  16. int score;
  17.  
  18.  
  19. printf("\nEnter the desired number of rounds\n");
  20. scanf("%d",&rounds);
  21. do{
  22. ranNum=genRandom();/*Call random function*/
  23. --rounds;
  24. do{
  25. printf("Guess the value of the random number\n");
  26. scanf("%d",&guess);
  27.  
  28.  
  29. if((guess>100)||(guess<0))
  30. {
  31. printf("Invalid!!!your number must less than 100 or more than 0\n\n");
  32. continue;
  33. }
  34.  
  35. if (ranNum==guess)
  36. {
  37. printf("Congratulations,You are correct!\n");
  38. break;
  39. }
  40.  
  41. if ((unsigned int)(ranNum-guess) >40)
  42. {
  43. printf("You are Cold\n");
  44. }
  45. else if (10<=(unsigned int)(ranNum-guess))
  46. {
  47. printf("You are Warm\n");
  48. }
  49. else if ((unsigned int)(ranNum-guess) < 10)
  50. {
  51. printf("You are Hot!\n");
  52. }
  53. --attempts;
  54. }while(attempts!=0);
  55.  
  56. score+=5*(5-attempts);
  57. attempts=5;
  58.  
  59. }while(rounds!=0);
  60.  
  61. printf("Your score is %d\n", score);
  62.  
  63. }/* end of main*/
  64.  
  65. int genRandom(void)
  66. {
  67. return rand()%100;
  68. }

Now I'm going on infinitely instead of breaking after five attempts. A noncumulative score has to be displayed at the end of each round but once the rounds are up a cumulative score is then displayed.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum


Views: 5883 | Replies: 31
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC