Efficient enough?

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

Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Efficient enough?

 
0
  #1
Oct 10th, 2008
I had to write a program that asks the user to guess a number. The program then tells if their guess is too high or too low depending on the difference between their guess and the random number. I finished the program and it works, but my teacher's very picky about efficiency. The chapter is about controlled structures. Can someone look at this and tell me if there's a better way to write it? Mine's kinda long and doesn't seem right :\

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. //declare the variables
  10. int num; //Variable to store the random number
  11. int guess; //Variable to store the number guessed by the user
  12. int isGuessed; //Boolean variable to control the loop
  13. int diff; //Variable to store the difference between the random number and guessed number
  14. int count; //Variable to count number of tries
  15.  
  16. num = (rand() + time(0)) % 100;
  17. isGuessed = false;
  18. count = 0;
  19.  
  20.  
  21. while (!isGuessed)
  22. {
  23. while (count < 5)
  24. {
  25. cout << "Enter an integer greater than or equal to 0 and less than 100. You have 5 guesses: ";
  26. cin >> guess;
  27. cout << endl;
  28.  
  29. diff = abs(num - guess); //Diff equals the difference between num and guess
  30.  
  31. if (diff == 0)
  32. {
  33. cout << "You guessed the correct number." << endl;
  34. isGuessed = true;
  35. count = 5;
  36. }
  37. else if (diff >= 50)
  38. {
  39. if (guess > num)
  40. cout << "Your guess is very high!" << endl;
  41. else
  42. cout << "Your guess is very low!" << endl;
  43. }
  44. else if (diff >= 30)
  45. {
  46. if (guess > num)
  47. cout << "Your guess is high!" << endl;
  48. else
  49. cout << "Your guess is low!" << endl;
  50. }
  51. else if (diff >= 15)
  52. {
  53. if (guess > num)
  54. cout << "Your guess is moderately high!" << endl;
  55. else
  56. cout << "Your guess is moderately low!" << endl;
  57. }
  58. else
  59. {
  60. if (guess > num)
  61. cout << "Your guess is somewhat high!" << endl;
  62. else
  63. cout << "Your guess is somewhat low!" << endl;
  64. }
  65. count++;
  66. } //end while
  67. cout << "\nGame over!" << endl;
  68. if (isGuessed)
  69. cout << "You win!" << endl;
  70. else
  71. cout << "You lose!" << endl;
  72. isGuessed = true;
  73. } //end while
  74.  
  75. return 0;
  76. }

Thanks for the help! =)
Last edited by ShadowOfBlood; Oct 10th, 2008 at 12:22 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: Efficient enough?

 
0
  #2
Oct 10th, 2008
Just browsing through your code. First thing, you don't need a nested while loop. Take out the outer while-loop, set the flag to true & break out of the loop when the answer is correct.

Also, I would use bool type for flag variables such as isGuessed.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Efficient enough?

 
0
  #3
Oct 10th, 2008
I think you can get rid of the outer while loop:

  1. while (!isGuessed)
  2. {
  3. // code
  4. isGuessed = true;
  5. } //end while

Since you assign isGuessed to true at the end, you're only going to go through the loop once, right? So it's not really a loop. Also, and it's not really an efficiency issue, but it might be helpful to, instead of saying they have 5 guesses each time, tell them how many guesses they have remaining. That's a small issue though.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: Efficient enough?

 
0
  #4
Oct 10th, 2008
Thanks, those tips helped a lot
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Efficient enough?

 
1
  #5
Oct 10th, 2008
>I finished the program and it works, but my teacher's very picky about efficiency.
Your program is I/O bound. The time it takes for a user to type a number and the time it takes to print the output will vastly overwhelm any processing efficiency. I recommend that you focus on clarity and correctness rather than efficiency.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Reply

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




Views: 393 | Replies: 4
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC