943,910 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 462
  • C++ RSS
Oct 10th, 2008
0

Efficient enough?

Expand Post »
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 :\

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 10th, 2008
0

Re: Efficient enough?

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.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008
Oct 10th, 2008
0

Re: Efficient enough?

I think you can get rid of the outer while loop:

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 10th, 2008
0

Re: Efficient enough?

Thanks, those tips helped a lot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 10th, 2008
1

Re: Efficient enough?

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: UML Solving...help pls
Next Thread in C++ Forum Timeline: how to connect C++ with Vb???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC