In: Computer Science
For this project, you will create a program in C that will test the user’s proficiency at solving different types of math problems. The program will be menu driven. The user will select either addition, subtraction, multiplication or division problems. The program will then display a problem, prompt the user for an answer and then check the answer displaying an appropriate message to the user whether their answer was correct or incorrect. The user will be allowed 3 tries at each problem. The program will also keep statistics on how many problems were answered correctly.
This week you will add functions to allow the user to select problems of varying degrees of difficulty and also keep statistics on number of correct answers vs. total number of problems attempted.
So now let’s add the code for the remaining functions:
We’ll start by inserting code to ask the user if they want to see problems that are easy, medium or difficult. We’ll do this by prompting them to enter ‘e’ for easy, ‘m’ for medium or ‘d’ for difficult and we’ll do this right after they have selected the type of problems they want. We will also add a validation loop that will check to see if they entered a correct response (e,m, or d).
Easy – problems using numbers 0 thru 10
Medium – problems 11 thru 100
Difficult – problems 100 – 1000
Here’s some code to get you started on the “difficulty level.”
if( difficultyLevel == 'E')
{
num1=rand()%10+1;
num2=rand()%10+1;
}
else
if (difficultyLevel == 'M')
{num1=rand()%100+1;
num2=rand()%100+1;
}
else
{ num1=rand()%1000+1;
num2=rand()%1000+1;}
Use an “if” statement to check the difficulty and generate the random numbers.
Statistics:
Create two variables ttlProblems and correctAnswers. Every time the user answers a problem add 1 to ttlProblems and every correct answer add 1 to correctAnswers. We’ll display the total number of problems and correct answers as well as the percent correct after the user has selected the exit option on the main menu.
Then, alter the code to allow for floating point numbers. This requires formatting the output.
IDE Used: Dev C++
Code : mathquiz.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> //for bool datatype
//function to get difficulty level
char dif_lev_func()
{
char dif_lev;
printf("\nSelect question difficulty level\n");
printf("e for easy, m for medium, d for
difficult\n");
printf("Your choice: ");
dif_lev=getch();
printf("%c",dif_lev);
return dif_lev;
}
//function to perform operations
bool ques_func(char op)
{
char dif_lev;
float first_num;
float sec_num;
float ans;
//for checking valid difficulty
bool valid=false;
//for counting number of turns
int turn=0;
//to check if answer is correct
bool if_correct=false;
//do this until difficulty is not valid
do
{
dif_lev= dif_lev_func();
if(dif_lev=='e')
{
first_num = rand()%10+1;
sec_num = rand()%10+1;
valid =true;
}
else if(dif_lev=='m')
{
first_num = rand()%100+1;
sec_num = rand()%100+1;
valid =true;
}
else if(dif_lev=='d')
{
first_num = rand()%1000+1;
sec_num = rand()%1000+1;
valid=true;
}
else
{
printf("\nInvalid option! Enter
again: ");
valid=false;
}
}while(valid==false);
if(op=='+')
{
//format to print two decimal point
printf("\nQues: %0.2f +
%0.2f",first_num,sec_num);
while(turn<3)
{
printf("\nAnswer: ");
scanf("%f",&ans);
if(ans==(first_num+sec_num))
{
printf("\nCorrect Answer!");
turn =3;
if_correct=true;
}
else
{
printf("\nIncorrect Try again");
turn++;
if(turn==3)
{
printf("\nTurn over!");
}
}
}
}
if(op=='-')
{
printf("\nQues: %0.2f -
%0.2f",first_num,sec_num);
while(turn<3)
{
printf("\nAnswer: ");
scanf("%f",&ans);
if(ans==(first_num-sec_num))
{
printf("\nCorrect Answer!");
turn =3;
if_correct=true;
}
else
{
printf("\nIncorrect Try again");
turn++;
if(turn==3)
{
printf("\nTurn over!");
}
}
}
}
if(op=='*')
{
printf("\nQues: %0.2f *
%0.2f",first_num,sec_num);
while(turn<3)
{
printf("\nAnswer: ");
scanf("%f",&ans);
if(ans==(first_num*sec_num))
{
printf("\nCorrect Answer!");
turn =3;
if_correct=true;
}
else
{
printf("\nIncorrect Try again");
turn++;
if(turn==3)
{
printf("\nTurn over!");
}
}
}
}
if(op=='/')
{
printf("\nQues: %f / %f",first_num,sec_num);
while(turn<3)
{
printf("\nAnswer: ");
scanf("%f",&ans);
if(ans==(first_num/sec_num))
{
printf("\nCorrect Answer!");
turn =3;
if_correct=true;
}
else
{
printf("\nIncorrect Try again");
turn++;
if(turn==3)
{
printf("\nTurn over!");
}
}
}
}
return if_correct;
}
//main function
int main()
{
int opt=0;
//to print total questions answered
int ttlproblems=0;
//to print total correct answers
int correctAnswers=0;
bool if_correct=false;
printf("Welcome to maths quiz\n");
//continue this until option is 5 i.e Exit
while(opt!=5)
{
printf("\nSelect your question type");
printf("\n1. Addition\n2. Subtraction");
printf("\n3. Multiplication \n4. Division");
printf("\n5. Exit ");
printf("\nChoice: ");
scanf("%d",&opt);
switch(opt)
{
case 1:
if_correct=ques_func('+');
ttlproblems++;
if(if_correct==true)
correctAnswers++;
break;
case 2:
if_correct=ques_func('-');
ttlproblems++;
if(if_correct==true)
correctAnswers++;
break;
case 3:
if_correct=ques_func('*');
ttlproblems++;
if(if_correct==true)
correctAnswers++;
break;
case 4:
if_correct=ques_func('/');
ttlproblems++;
if(if_correct==true)
correctAnswers++;
break;
case 5: printf("\nThanks for
answering");
//print total numbers of questions
answered
printf("\nTotal Question Answered:
%d",ttlproblems);
//print numbers of correct answered
printf("\nTotal Correct Answers:
%d",correctAnswers);
break;
}
}
return 0;
}
Code Snippets
Sample Output