943,108 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 367
  • C++ RSS
Feb 8th, 2010
0

What two numbers exit this programme?

Expand Post »
I wrote this programme but what two numbers you choose exit it?
can you guess just by looking? Because I can't

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. bool aHo = true;
  10. int a = 0;
  11. int b = 0;
  12.  
  13. while(aHo)
  14. {
  15. cout << "Enter Number one: ";
  16. cin >> a;
  17. cout << endl;
  18. cout << "Enter NUmber two: ";
  19. cin >> b;
  20. cout << endl;
  21.  
  22. for (int i = 0; i< 2; i++)
  23. for (int j = 0; j < 2; j++)
  24. for (int z = 0; z < 2; z++)
  25. for (int v = 0; v < 2; v++)
  26. cout << a++ << " " << ++b << " " << ++a << " " << b++ << endl;
  27. a++;
  28. if (a == 30 && b == 34)
  29. aHo = false;
  30. }
  31.  
  32.  
  33. system("PAUSE");
  34. }
Reputation Points: 10
Solved Threads: 0
Light Poster
invisi is offline Offline
27 posts
since Aug 2009
Feb 8th, 2010
0
Re: What two numbers exit this programme?
If you type in 0 and 0, what are the value of a&b at the end of the while loop? That should help you figure out the two numbers to enter.
Moderator
Reputation Points: 3275
Solved Threads: 886
Posting Sage
WaltP is offline Offline
7,699 posts
since May 2006
Feb 8th, 2010
0
Re: What two numbers exit this programme?
As can be observed, the cout statement is exected 2*2*2*2 = 16 times. Each time both a and b gets incremented twice.
Therefore they are incremented 32 times. a gets incremented once more. That makes a increment 33 times and b 32 times.
You need to enter such 'a' tht it becomes 30 after 33 increments and such a 'b' tht it becomes 34 after 32 increments.
Reputation Points: 11
Solved Threads: 1
Newbie Poster
masterovpuppetz is offline Offline
5 posts
since Oct 2008
Feb 8th, 2010
0
Re: What two numbers exit this programme?
Ah so to find how many times it goes through a loop you multiply all of them, cool thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
invisi is offline Offline
27 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Program function Help
Next Thread in C++ Forum Timeline: checking where the space is entered





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


Follow us on Twitter


© 2011 DaniWeb® LLC