Question

In: Computer Science

For this project, you will create a program in C that will test the user’s proficiency...

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.

Solutions

Expert Solution

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


Related Solutions

********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
Create a Python program that will calculate the user’s net pay based on the tax bracket...
Create a Python program that will calculate the user’s net pay based on the tax bracket he/she is in. Your program will prompt the user for their first name, last name, their monthly gross pay, and the number of dependents. The number of dependents will determine which tax bracket the user ends up in. The tax bracket is as followed: 0 – 1 Dependents : Tax = 20% 2 – 3 Dependents : Tax = 15% 4+ Dependents :    Tax...
Create a Java application called ValidatePassword to validate a user’s password. Write a program that asks...
Create a Java application called ValidatePassword to validate a user’s password. Write a program that asks for a password, then asks again to confirm it. If the passwords don’t match or the rules are not fulfilled, prompt again. Your program should include a method that checks whether a password is valid. From that method, call a different method to validate the uppercase, lowercase, and digit requirements for a valid password. Your program should contain at least four methods in addition...
Create a C++ program that will ask the user for how many test scores will be...
Create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. (no fstream) The data needs to include the student’s first name, student number test score the fields should be displayed with a total width of 15. The prompt should be printed with a header in the file explaining what each is: ex. First Name student number Test Score 1) mike 6456464   98 2) phill...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
C++ Program -------------------- Project 5-1: Monthly Sales Create a program that reads the sales for 12...
C++ Program -------------------- Project 5-1: Monthly Sales Create a program that reads the sales for 12 months from a file and calculates the total yearly sales as well as the average monthly sales. Console Monthly Sales COMMAND MENU m - View monthly sales y - View yearly summary x - Exit program Command: m MONTHLY SALES Jan        14317.41 Feb         3903.32 Mar         1073.01 Apr         3463.28 May         2429.52 Jun         4324.70 Jul         9762.31 Aug        25578.39 Sep         2437.95 Oct         6735.63 Nov          288.11 Dec         2497.49...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program that performs the following:  Define a function called “Find_Min” that takes an array, and array size and return the minimum value on the array.  Define a function called “Find_Max” that takes an array, and array size and return the maximum value on the array.  Define a function called “Count_Mark” that takes an array, array size, and an integer value (mark) and...
Create a C++ project in visual studio. You can use the C++ project that I uploaded...
Create a C++ project in visual studio. You can use the C++ project that I uploaded to complete this project. 1. Write a function that will accept two integer matrices A and B by reference parameters, and two integers i and j as a value parameter. The function will return an integer m, which is the (i,j)-th coefficient of matrix denoted by A*B (multiplication of A and B). For example, if M = A*B, the function will return m, which...
Q1) Please create a C++ program that will ask the user for how many test scores...
Q1) Please create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. The data will include the student’s first name, Wayne State Access ID, midterm score and their favorite team (i.e. – Doug, hg1702, 92, Wolverines * - John, hv2201, 99, Warriors). Print out the completed test scores to a file (midTermScores.txt) . Adjust the output as follows: (15 points) a. Scores with the...
× course.content.assessment.attempt.menu Test Finance Proficiency Quiz 1 Finance Proficiency Quiz 1 Test Content Question 1 10...
× course.content.assessment.attempt.menu Test Finance Proficiency Quiz 1 Finance Proficiency Quiz 1 Test Content Question 1 10 Points Net sales are $10,000,000, beginning total assets are $2,000,000, and the asset turnover is 4.0 times. What is the ending total asset balance? $2,000,000 $2,500,000 $3,000,000 $4,000,000 Question 2 10 Points Which one of the following is a component of Liquidity analysis? Asset Turnover Current Ratio Return on Assets Debt to Total Assets Question 3 10 Points Darius, Inc. has the following income...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT