473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

multiplication test

my assingnment is to design an application that will generate two random operands that will formulate a question in the form:
5 x 2 =

Assuming 5 and 2 are the randomly generated operands.

The user at this point will enter the answer followed by the enter key. The entered value is compared with the computer calculated answer. If they are the same, a counter representing the correctly answered questions is incremented; along with a message indicating the answer was answered correctly. If the answer entered is incorrect, then a message indicating the answer was incorrectly answered should be displayed.

Each test comprises of 10 questions; The question number will be displayed as a prefix to the question displayed.

After the 10 questions have been answered, the score as a percentage is displayed. e.g. “You answered 70% of the questions correctly”

The user is then asked if they would like another go? Entering ‘Y’ will start the test again, after clearing the screen.


so far my code is
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int firstrandom;                            //first random number to be generated declared as an integer
  6. int secondrandom;                            //second random number to be generated declared as an integer
  7. int guess;                                
  8. int answer;
  9. int counter;
  10. int quesnum;
  11.  
  12. void main (void)
  13.  
  14. {
  15.     printf("What is the product of the following questions?\n\n");
  16.  
  17.  
  18.     srand (time(NULL));
  19.     firstrandom = rand() % 12 + 1;
  20.     secondrandom = rand() % 12 + 1;
  21.     answer = firstrandom * secondrandom;
  22.     printf("Q1\n\n");
  23.     printf("%d\n", firstrandom); 
  24.     printf("%d\n", secondrandom);
  25. }
  26.  
  27.  
i dont know how to make it check whether the answer given by the user is correct or not and make it loop 10 times for the test... also how to make the question numbers got one up
Nov 12 '11 #1

✓ answered by whodgson

You could put all this code plus the question "go again?" in a do while loop with the end condition while (response == 'y' ||response== 'Y') and i suppose it would not hurt to include at the end of this loop (and the last line):
if(response== 'n'||response == 'N')break;
good luck

5 2274
whodgson
542 512MB
If m and n are assumed to be the two random numbers....user provided 'ans' will be correct if it == m*n else it will be incorrect.
Nov 13 '11 #2
thanks for the multiplicattion...,
i have this code now:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int firstrandom;                            //first random number to be generated declared as an integer
  6. int secondrandom;                            //second random number to be generated declared as an integer
  7. int guess;                                
  8. int answer;
  9. int correctcounter;
  10. int qnum;
  11. int fnlprcnt;
  12.  
  13. void main (void)
  14.  
  15. {
  16.     printf("What is the product of the following questions?\n\n");
  17.     qnum = 0;
  18.     correctcounter = 0;
  19.  
  20.  
  21.     for(qnum=0; qnum<10;qnum)
  22.  
  23.         do 
  24.             {
  25.                     qnum++;
  26.                     printf("Q%d\n\n", qnum);
  27.                     srand (time(NULL));
  28.  
  29.                     firstrandom = rand() % 12 + 1;
  30.                     secondrandom = rand() % 12 + 1;
  31.                     printf("%d x %d\n", firstrandom, secondrandom);
  32.                     answer = firstrandom * secondrandom;
  33.  
  34.                     //printf("%d\n", firstrandom); 
  35.                     //printf("%d\n", secondrandom);
  36.  
  37.                     printf("%d\n", answer);
  38.  
  39.  
  40.  
  41.                     scanf("%d", &guess);
  42.  
  43.                     if (guess > answer)
  44.                         printf("incorrect\n");
  45.  
  46.                     if (guess < answer)
  47.                         printf("incorrect\n");
  48.  
  49.                     if(guess == answer)
  50.                         printf("Correct\n");
  51.                         correctcounter++;
  52.  
  53.             }
  54.  
  55.  
  56.         while (guess != answer);
  57.  
  58.             fnlprcnt = correctcounter / 10 * 100;
  59.             printf("Well Done, you have completed the test. Your scored %d percent\n", fnlprcnt);
  60.  
  61.  
  62.  
  63.  
  64. }
  65.  
  66.  
when i run the program it loops and stops at q10 if i get the last question right
if i get q10 wrong it carrys on... any ideas?
Nov 14 '11 #3
whodgson
542 512MB
Your while loop end condition is 'guess != answer' so it will continue until a correct answer arrives and i don`t think the for loop is doing anything(i.e. it has no enclosing braces {})
BTW incrementing the for loop in its body is not considered good practice.
Nov 15 '11 #4
thanks or the advice... i have changed the while statement to
Expand|Select|Wrap|Line Numbers
  1. while (qnum != 10);
  2.  
for it to cut off at the 10th question

the only part i am struggling on now is getting it to ask whether the user would like to try again and then run the code again.

e.g.
Expand|Select|Wrap|Line Numbers
  1. printf("would you like to try again? 1 for yes and 2 for no")
  2.  
its just i dont know where to start to make it scan for the input and whether i need to declare anymore variables

please help and thanks for the hep you have already given to me =)
Nov 15 '11 #5
whodgson
542 512MB
You could put all this code plus the question "go again?" in a do while loop with the end condition while (response == 'y' ||response== 'Y') and i suppose it would not hurt to include at the end of this loop (and the last line):
if(response== 'n'||response == 'N')break;
good luck
Nov 16 '11 #6

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

Similar topics

3
by: Mark Dickinson | last post by:
Can anyone either reproduce or explain the following apparently inconsistent behaviours of __add__ and __mul__? The class Gaussian provides a sub-bare-bones implementation of Gaussian integers (a...
4
by: christopher diggins | last post by:
Welcome to the first installment of the Diggins Public Domain Posts (PDP). There is a significant dearth of good public domain C++ code. All of it seems to come with one kind of a license or...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
5
by: Paul McGuire | last post by:
Does Python's run-time do any optimization of multiplication operations, like it does for boolean short-cutting? That is, for a product a*b, is there any shortcutting of (potentially expensive)...
8
by: starffly | last post by:
In my program, the caculated value is supposed to be no more than the constant named MAXINT,otherwise, overflow error will be informed.however, I cannot test if the value exceeds MAXINT within the...
11
by: Skybuck Flying | last post by:
Hello, type Tbig = array of byte; vAddTable : array of array of byte; // = value // = transport/carry vMulTable : array of array of byte;
2
by: helPlease | last post by:
I want to multiply two very long integers (say of 20 digits).I am storing them in character array.Then what mechanism should i follow to mutiply two such numbers.If anybody has any suggestions...
2
by: Germaris | last post by:
Hi there! Here is the whole script of a test document which doesn't work... Excuse me but I never used multiplication of variables and I confess how shameful I feel. I absolutely don't...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.