hey guys i need help with this example for some reason its not clicking for me.

Write a program that keeps generating two random numbers between 2 and 9 and asks the user for the product of the two numbers, e.g.: "What is 4 x 6?". If the user answers correctly, the program responds with "Right!"; otherwise, it displays: Wrong! 4 x 6 = 24.

Generate 5 such pairs of numbers and get the answers from the user. If at any time, both numbers are the same as last time, generate two new numbers. Continue generating 2 new numbers until at least one is different from last time.

After presenting 5 pairs of numbers and getting the answers, display how many the user got right; e.g.: You got 4 of 5 right. Then, ask if he or she wants to play again, like so: "Do you want to play again? [y/n]". If the user answers with 'y' or 'Y', it generates another 5 pairs of numbers and asks for their products. If not, it quits generating numbers.

If the user gets less than 25% of the last round of 5 questions, the program displays the multiplication table before terminating. Use a nested for loop to display the table. A bunch of cout statements will not be acceptable. You must also use a loop for any part that calls for repetition such as generating 5 pairs of numbers.

The following is a sample interaction between the user and the program:

What is 3 x 9? 27
Right!

What is 2 x 7? 14
Right!

What is 8 x 9? 63
Wrong! 8 x 9 = 72

What is 6 x 3? 21
Wrong! 6 x 3 = 18

What is 2 x 9? 18
Right!

You got 3 out of 5 right which is 60%.

Play agian? [y/n] n

(If the user had answered with y or Y the program would generate another 5 pairs of numbers and would again display the score and ask to do it again or not. But, because the user answered with n (or N), it quits, but before quitting, it prints the multiplication table since the score was less than 75%).

You need to study the multiplication table:
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

Press any key to continue.

Upload both cpp and exe files using the Browse and Upload buttons below and click Finish once.







***** PLEASE if someone can help me with this.
We're not going to do the whole assignment for you. What specifically is giving you trouble?

Larger projects seem more complicated than they are. Break it up into smaller parts.

For example... the first thing the assignment tells you to do is "generate two random numbers between 2 and 9". Can you do that?

Once you do that, move on to the next step: "ask the user for the product of the two numbers, e.g.: "What is 4 x 6?""

etc


Just take it one step at a time.


EDIT: also, give us more than 10 minutes to respond. We aren't just sitting on the boards 24/7 waiting to answer posts. Sometimes it takes us a while to see them and respond.
Last edited on
im new to c++ and trying to learn.Please someone help me :]
Last edited on
well im stuck on that because rand() % 10 gives u #s from 1-9 when i need 2-9.. and i just need help on the outer loop and the first inner loop.
rand() % 10 gives u #s from 1-9 when i need 2-9


rand() % 10 actually gives you numbers from 0 to 9 (not 1 to 9).

But changing this is simple. Just think about it. If rand()%10 gives you [0..9], then what does (rand()%10)+1 give you?
(rand() % 10) +1 would give you 1-10 #s?
soory .. Disch ,but i still do not get how do we get 2 - 9 ..
It's simple pattern recognition, guys!

(rand() % x) results in [0..x-1]
(rand() % x) + y results in [y..x+y-1]

Fill in your numbers for your desired range.

You already know y=2 (that's your desired minimum), so what does that make x?
thank you (rand()%8)+ 2.
Thanks Disch .. crystal clear...
int num,num2,user;
int answer1,count;

srand(unsigned(time(0)));
count=0;
for(int i=1 ; i<=5 ; i++)
{
num = (rand()%8)+ 2;
num2= (rand()%8)+ 2;
answer1=num*num2;
cout<<"What is "<<num<<" X "<<num2<<endl;
cin>>user;


if(answer1==user)
cout<<"Correct!\n";
count++;
if(answer1!=user)
cout<<"Wrong!\n";

}


cout<<"You got "<<count<<" out "<<"5 right!\n";



I need help now on how to filter out the random #s from being the same just as the direction say.
Also,need help on the part where I ask the user to enter "yes or no" to re-enter
into the loop.
Last edited on
heymrjack10, I know this isn't part of your question at all but if you're just learning c++ now or something, I would highly recommend getting into the habit of always initializing your variables to 0 or NULL;
so instead of
1
2
int num,num2,user; 
int answer1,count;


you should write it like this
1
2
3
4
5
int num = 0;
int num2 = 0;
int user = 0;
int answer1 = 0;
int count = 0;


this can save you massive headaches when debugging, and I don't just mean for ints, I mean for just about everything, ever. Don't just declare it, initialize it!
Thanks alot ceruleus makes sense! And would you know how to filter out the random #s from being the same as said in the directions.

Thanks!
someone please help me lol
use seed from the system clock e.g.
1
2
3
#include<ctime>
unsigned seed = time(NULL);
srand(seed);

for srand() and rand() you also need to include header <cstdlib>
Last edited on
#include <ctime>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{

int num = 0;
int num2 = 0;
int num3 = 0;
int num4 =0;
int user = 0;
int answer1 = 0;
float count = 0;
float percent;
char ch;
srand(unsigned(time(0)));

for(int i=1 ; i<=5 ; i++)
{
{
num = (rand()%8)+ 2;
num2= (rand()%8)+ 2;
answer1=num*num2;
cout<<"\nWhat is "<<num<<" x "<<num2<<endl;
cin>>user;

if(answer1==user)
cout<<"Right!\n";
count++;
if(answer1!=user)
cout<<"Wrong! -> "<<num<<" x "<<num2<<" = "<<answer1;
}


percent=(count / 5)*100;
cout<<"\nYou got "<<count<<" out "<<"5 right, which is "<< percent <<"%\n\n";

count<<"\nPlay agian? [y/n]:";
cin>>ch;
while( ch== 'y' || ch== 'Y');

}








if(percent<=25)
{
cout<<endl;
for(int a=1;a<=9;a++)
cout<<a<<' ';

cout<<endl;
for(int b=2;b<=18;b++)
if(b%2==0)
cout<<b<<' ';

cout<<endl;
for(int c=3;c<=27;c++)
if(c%3==0)
cout<<c<<' ';
cout<<endl;
for(int d=4;d<=36;d++)
if(d%4==0)
cout<<d<<' ';
cout<<endl;
for(int e=5;e<=45;e++)
if(e%5==0)
cout<<e<<' ';
cout<<endl;
for(int a2=6;a2<=54;a2++)
if(a2%6==0)
cout<<a2<<' ';
cout<<endl;
for(int b2=7;b2<=63;b2++)
if(b2%7==0)
cout<<b2<<' ';
cout<<endl;

for(int c2=8;c2<=72;c2++)
if(c2%8==0)
cout<<c2<<' ';
cout<<endl;
for(int d2=9;d2<=81;d2++)
if(d2%9==0)
cout<<d2<<' ';
cout<<endl;

}




system("pause");
return 0;
}
i neeed help with the inner loop.. and the a config to filter out the same #s... someone help :*(
Topic archived. No new replies allowed.