By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,408 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,408 IT Pros & Developers. It's quick & easy.

Generate multiplication tables

P: 24
Can somebody please help me with the following problem. I need to submit the problem on Monday.

A program is required which could be used to help a child practice their multiplication tables. The program should start by asking the user how many multiplication problems they would like to try, and read this value from the keyboard. Then, the program should present to the user that many multiplication problems and allow the user to enter an answer for each.
After each answer is input, the program should print a message indicating that the answer is correct, or if it is not correct, display the correct answer. When all the problems are done, the program should print the percentage of problems answered correctly.

Below is a sample run of such a program and you can model your output on it if you wish. The user input is shown in boldface.
How many problems would you like? 3
Question 1: what is 6 x 8? 48
Correct!

Question 2: what is 7 x 3? 28
No, 7 x 3 is 21

Question 3: what is 4 x 5? 20
Correct!

Your score is 66.6%
The numbers used in the problems must all be in the range 1..12 and must be randomly generated. The expression below will generate a random integer in the range 1..12:
srand(time(NULL));
int j=1+(int) (12.0*rand()/(RAND_MAX+1.0));

where j is a random number between 1 and 12.

I wrote the solution for the problem but the problem is that my solution doesn't show the score. And secondly, my solution generates the same question repeatedly.

Please, please help me.

Thanxxxxxx in advance.

My code:

Expand|Select|Wrap|Line Numbers
  1. #include<cstdlib.h>
  2. #include<iostream.h>
  3.  
  4. int main()
  5. {
  6.     int user=0,i, num1, num2, count=0; //variable declaration
  7.     double answer, score=0, totalPercentage; //variable declaration
  8.  
  9.     cout<<"How many problems would you like? ";
  10.     cin>>user;
  11.  
  12.     srand(time(NULL)); //calling srand function
  13.  
  14.     for(i=1;i<=user;i++)
  15.     {
  16.           num1=1+(int) (12.0*rand()/(RAND_MAX+1.0));//generates first random number
  17.  
  18.           num2=1+(int) (12.0*rand()/(RAND_MAX+1.0));//generates second random number
  19.  
  20.           cout<<"Question "<<numOfQuestion<<":"<<" what is "<<num1<<" X "<<num2<<"? ";
  21.  
  22.     cin>>answer;
  23.  
  24.     if (answer==(num1*num2))
  25.     {
  26.         cout<<"Correct!"<<endl;
  27.         count++;
  28.     }
  29.     else
  30.       {
  31.         cout<<"No, "<<num1<<" X "<<num2<<" is "<<num1*num2<<endl;
  32.     }
  33.     }
  34.     totalPercentage=(count*100)/user;
  35.     cout<<"Your score is "<<totalPercentage<<"%"<<endl;
  36.     return 0;
  37. }
Feb 13 '07 #1
Share this Question
Share on Google+
5 Replies


hirak1984
100+
P: 316
for repetition of numbers for multiplication,you may first generate all the random numbers and keep it in array.

e.g : for 3 question array size will be3 or 6 depending upon you use one array or two.
while storing random numbers check if the new number generated already exists in the array or not.
then output questions using numbers from the array.

and for the percentage problem,is it giving error or not printing the correct percentage?
Feb 13 '07 #2

P: 96
Sample code is in c....try to implement in c++ once after understanding the c code...and also implement the average result.....

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. void main()
  5. {
  6.     int i,j,k,input,res,count;
  7.     clrscr();
  8.     printf("Enter How many u have to work out :");
  9.     scanf("%d",&i);
  10.     count=0;
  11.     do
  12.     {
  13.         j=(rand()%10);
  14.         k=(rand()%10);
  15.         res=j*k;
  16.         printf("What %d * %d is :",j,k);
  17.         scanf("%d",&input);
  18.         if(res==input)
  19.         {
  20.             printf("Right\n");
  21.         }
  22.         else
  23.         {
  24.             printf("Wrong the right answer is : %d\n",res);
  25.         }
  26.         count++;
  27.     }while(count<=i);
  28.     getch();
  29.     return 0;
  30. }
Feb 13 '07 #3

hirak1984
100+
P: 316
this code can also repeate questions(logically).

one have to precheck if randomly generated numbers are repeated or not.I think repeatition was the main problem not the logic.

Sample code is in c....try to implement in c++ once after understanding the c code...and also implement the average result.....

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i,j,k,input,res,count;
clrscr();
printf("Enter How many u have to work out :");
scanf("%d",&i);
count=0;
do
{
j=(rand()%10);
k=(rand()%10);
res=j*k;
printf("What %d * %d is :",j,k);
scanf("%d",&input);
if(res==input)
{
printf("Right\n");
}
else
{
printf("Wrong the right answer is : %d\n",res);
}
count++;
}while(count<=i);
getch();
return 0;
}
Feb 13 '07 #4

P: 24
Thank you very much I have found out the solution of the problem.
Feb 13 '07 #5

P: 96
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include <time.h>
  5. void main()
  6. {
  7.     int i,j[10],j_j[10],k[10],k_k[10],input,res,count,buf;
  8.     time_t t;
  9.  
  10.     clrscr();
  11.     printf("Enter How many u have to work out :");
  12.     buf=0;
  13.     scanf("%d",&i);
  14.  
  15.     srand((unsigned) time(&t));
  16.     for(count=0;count<10;count++)
  17.     {
  18.         j[count]=(rand()%10);
  19.         k[count]=(rand()%10);
  20.     }
  21.     for(count=0;count<10;count++)
  22.     {
  23.         do
  24.         {
  25.             j[count]=(rand()%10);
  26.             k[count]=(rand()%10);
  27.             if(j[count] == j_j[count])
  28.             {
  29.                 if(k[count] == k_k[count])
  30.                 {
  31.                     buf=1;
  32.                     j[count]=6;
  33.                     k[count]=7;
  34.                 }
  35.                 else
  36.                 {
  37.                     buf=0;
  38.                 }
  39.             }
  40.         }
  41.         while(buf);
  42.     }
  43.     count=0;
  44.     do
  45.     {
  46.         res=(j[count]*k[count]);
  47.         printf("What %d * %d is :",j[count],k[count]);
  48.         scanf("%d",&input);
  49.         if(res==input)
  50.         {
  51.             printf("Right\n");
  52.         }
  53.         else
  54.         {
  55.             printf("Wrong the right answer is : %d\n",res);
  56.         }
  57.         count++;
  58.     }while(count<=(i-1));
  59.  
  60.     for(count=0;count<10;count++)
  61.     {
  62.         k_k[count]=k[count];
  63.         j_j[count]=j[count];
  64.     }
  65.     printf("Press any key to continue...");
  66.     getch();
  67.     return 0;
  68.  
  69.  
  70. }
Above code will reduce the repetion but not completely....
Feb 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.