Random Change Between

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 3
Reputation: ichigoSJ is an unknown quantity at this point 
Solved Threads: 0
ichigoSJ's Avatar
ichigoSJ ichigoSJ is offline Offline
Newbie Poster

Random Change Between

 
0
  #1
Aug 18th, 2008
Hi im trying to make a maths program and im having trouble with one of my sections where im currently just using '+' incorporated in to a do while loop until the score = 10 though i'm stuck on how to make it randomly change the question between + or - as so far i've only got +


  1. int choice ;
  2. cin >> choice;
  3. do {
  4. int r, r2, answerplus, answersubtract;
  5. r = rand() % 8 + 1;
  6. r2 = rand() % 8 + 1;
  7. answerplus = r + r2;
  8. out << "question number " << questionumber << endl;
  9. cout << r << "+ __ = " << answerplus << endl;
  10.  
  11. int responce;
  12. cin >> responce;
  13.  
  14. if (responce == r2) {cout << "correct!" << endl;
  15. score = score + 1; }
  16. else {cout << "wrong!" << endl; }
  17. questionumber = questionumber + 1;
  18. cout << "your score is " << score << endl;
  19. while (questionumber <= 10);

thankyou
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,471
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Random Change Between

 
0
  #2
Aug 18th, 2008
I think you need to describe what it is your making and what your is problem a little better. Simply saying i'm stuck isn't enough.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: Random Change Between

 
0
  #3
Aug 18th, 2008
That looks like a fun little program. To make it do both addition and subtraction, you need to determine what changes between the two paths. Edward sees that you need to display a different character for the equation--either '+' or '-' so the user knows how to figure the answer--and the answer itself needs to reflect the right operation. If you hoist the operator character out into a variable, you can localize the choice between addition and subtraction without changing the rest of the code:
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8.  
  9. srand(unsigned(time(0)));
  10.  
  11. while (true) {
  12. int lhs = rand() % 8 + 1;
  13. int rhs = rand() % 8 + 1;
  14. int answer;
  15. char op;
  16.  
  17. // Choose equally between + and -
  18. if (rand() % 2 == 0) {
  19. answer = lhs + rhs;
  20. op = '+';
  21. }
  22. else {
  23. answer = lhs - rhs;
  24. op = '-';
  25. }
  26.  
  27. cout << lhs << ' ' << op << " __ = " << answer << "\n>";
  28.  
  29. int choice;
  30.  
  31. if (!(cin >> choice))
  32. break;
  33.  
  34. if (choice == rhs)
  35. cout << "Correct!\n";
  36. else
  37. cout << "Incorrect, the answer is " << rhs << '\n';
  38. }
  39. }
The choice is made by going one way if the random value is even, and the other way if it's odd. That gives you a good 50/50 chance for either addition or subtraction. You can change that so one has more probability than the other. For example, if you change rand() % 2 to rand() % 3, addition will be twice as likely.

This approach to making random decisions can be applied in a lot of places and can be extended for more than two paths. It's a good trick to have under your belt.
Last edited by Radical Edward; Aug 18th, 2008 at 11:41 am.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC