This is in C languange and arrays are not allowed to be used. Also, please comme
ID: 670189 • Letter: T
Question
This is in C languange and arrays are not allowed to be used.
Also, please comment on the steps used, thank you.
This program will quiz the user on the basic arithmetic problems:
when the program runs, the user will be given a menu
of the following choices:
1. Give me an addition problem.
2. Give me a subtraction problem.
3. Give me a multiplication problem.
4. Quit.
The user's choice is specified with the corresponding integer.
(Note: I've left out division so we can just work with
integers.) If the user gives an integer outside of 1..4, the
program prints an error message and reprints the menu.
For options 13, your program will generate a problem for the
user to solve. For example an addition problem might look like:
Complete: 12 + 22 =
After displaying the problem, the program reads an answer from
the user and reports if it is correct or not.
If the answer is correct, the program congratulates the user and
reprints the menu and awaits the user’s next choice.
If the answer is incorrect the program will ask the user to try
again, but only up to a maximum of 5 attempts. If the user gives
5 incorrect answers, the program reports the correct answer with
an appropriate message and the program reprints the menu and
awaits user input.
Your program will use nonnegative numbers in the range [0..100]
(inclusive) for the problems given to users. To generate such
numbers at "random" you can use the rand() and srand() library
functions. The rand() function returns an integer. To get an
integer in the specified range, you can use the mod operator:
a = rand() % 101; // % 101 gives a number in 0..100
The rand() function produces a "pseudorandom" number. To "seed"
the generator so it doesn't produce the same sequence each time
the program is run, add this magic code to the top of main:
srand( (unsigned)time(NULL));
You will need to include the time.h header file as well:
#include <time.h>
Explanation / Answer
# include <stdio.h>
# include <conio.h>
# include <time.h>
int main(){ //main loop
int ans,ans2,i;
srand( (unsigned)time(NULL)); //srand function that will not repeat same numbers
int a = rand() % 101; //first random number
int b = rand() % 101;//second random number
do{ //while loop
printf(" 1. Give me an addition problem");
printf(" 2. Give me substraction problem");
printf(" 3. Give me multiplication problem");
printf(" 4. Exit ");
scanf(" %d",&ans);
switch(ans){ //switch case
case 1 :
printf(" %d + %d =",a,b);
scanf("%d",&ans2);
if(ans2==a+b){
printf(" You enter correct :");
}else{
printf(" Please try again :");
for(i=5;i>0;i--){
scanf(" %d",&ans2);
if(ans2==a+b){
printf(" You enter correct :");
break;
}else{
printf(" Please try again in %d times: ",i-1);
}
}
printf(" Answer is %d ",a+b);
}
a=0;
b=0;
break;
case 2:
printf(" %d - %d = ",a,b);
scanf("%d",&ans2);
if(ans2==a-b){
printf(" You are right");
}else{
printf(" Please try again : ");
for(i=5;i>0;i--){
scanf(" %d",&ans2);
if(ans2==a-b){
printf(" You are right :");
break;
}else{
printf(" Please try again in %d times: ",i-1);
}
}
printf(" Answer is %d ",a-b);
}
a=0;
b=0;
break;
case 3:
printf(" %d * %d");
scanf("%d",&ans2);
if(ans2==a*b){
printf(" you are right ");
}else{
printf("Please try again ");
for(i=5;i>0;i--){
scanf(" %d",&ans2);
if(ans2==a*b){
printf(" You are right :");
break;
}else{
printf(" Please try again in %d times: ",i-1);
}
}
printf(" Answer is %d ",a*b);
}
a=0;
b=0;
break;
case 4:
break;
default :
printf(" You enterd invalid choice, please try again: ");
break;
}
}while(ans!=4);
}
Expected output :
1. Give me an addition problem
2. Give me substraction problem
3. Give me multiplication problem
4. Exit 1
30 + 33 =63
You enter correct : 1. Give me an addition problem
2. Give me substraction problem
3. Give me multiplication problem
4. Exit 1
0 + 0 =5
Please try again :
5
Please try again in 4 times: 5
Please try again in 3 times: 5
Please try again in 2 times: 5
Please try again in 1 times: 5
Please try again in 0 times:
Answer is 0
1. Give me an addition problem
2. Give me substraction problem
3. Give me multiplication problem
4. Exit 4
--------------------------------
Process exited with return value 4
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.