1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
//********************************
// This program will ask the user to answer a series of
//arithmetic questions and will report on how the user performs.
#include <iostream>
#include <ctime> // Will be used by the srand function for time
#include <cstdlib> // will allow for random numbers to be generated.
#include <iomanip>
using namespace std;
void getProbsPerSet(int& numProbs);
// The following functions will have inputs
void doOneSet( char problemType,int numProbs,int& correctCount);// Prototype
void getMaxNum(int &maxNum); // Will need inputs
void printHeader(char problemType);
void doOneProblem(char problemType, int& MaxNum, int & correctCount);
void generateOperands(int& firstNum, int& secondNum, int maxNum);
void calcCorrectAnsw(char problemType, int firstNumber, int secondNum, int& answer);
void checkAnswer (int correctAnsw,int answer, int&correctCount);
void printReport(int probsPerSet, int set1Correct, int set2Correct, int set3Correct);
int main()
{
int probsPerSet;
int set1Correct,set2Correct, set3Correct;
srand(time(0));
getProbsPerSet(probsPerSet);
doOneSet('+', probsPerSet, set1Correct);
doOneSet('-', probsPerSet, set2Correct);
doOneSet('*', probsPerSet, set3Correct);
printReport(probsPerSet,set1Correct, set2Correct, set3Correct); //a report is generated for the user
//telling them how they did
}
void getProbsPerSet(int& numProbs)
//Pre: numProbs is assigned
//Post: numProbs is received from the user and passed to the calling function
{
cout << "Enter problems per set: ";
cin >> numProbs;
}
void doOneSet( char problemType,int numProbs,int& correctCount)
//Pre: problemType,numProbs and correctCount are assigned
//Post: output one set of problems
{
int maxNum;
correctCount=0;
printHeader(problemType);
getMaxNum(maxNum);
for (int i = 1; i <= numProbs; i++)
{
doOneProblem(problemType, maxNum, correctCount);
}
}
//******************************************************************
// Function void printerHead definition
//prints out the header at the top of each set denoting the number
//******************************************************************
void printHeader(char problemType)
//Pre: problemType is assigned
//Post: the header is then printed
{
int setNumber;
if (problemType == '+')
setNumber = 1;
if (problemType == '-')
setNumber = 2;
if (problemType == '*')
setNumber = 3;
cout << endl << "Set #" << setNumber << endl << "----------" << endl;
}
//*************************************************************************************************
// Function void getMaxNum
// This function allows the user to enter the highest number that may b used in one of the problems
//*************************************************************************************************
void getMaxNum(int &maxNum)
{
cout << "What is the maximum number for this set? ";
cin >> maxNum;
}
//************************************************************************
// Function doOneProblem definition
//creates a single problem with two random numbers and the desired operand,
//collects the user's answer, checks it and passes the number that they
//got correct up to the main function
//************************************************************************
void doOneProblem(char problemType, int& MaxNum, int & correctCount) // delivers the amount of correct problems answer to the main function
{
int firstNum,secondNum;
int answer;
int correctAnswer;
generateOperands(firstNum, secondNum, MaxNum);
cout << firstNum << " " << problemType << " " << secondNum << " = ";
cin >> answer;
calcCorrectAnsw( problemType,firstNum, secondNum, correctAnswer);
checkAnswer(answer, correctAnswer, correctCount);
}
//prints out the problem for the user to solve
void generateOperands(int& firstNum, int& secondNum, int maxNum)
{
firstNum= rand() % maxNum + 1;
secondNum = rand() % maxNum + 1;
}
//*************************************************************
//Function void calcCorrectAnsw definition
//calculates what the correct answer for the problem should be
//*************************************************************
void calcCorrectAnsw(char problemType, int firstNumber, int secondNum, int& answer)
{
if (problemType == '+')
answer = firstNumber + secondNum;
if (problemType == '-')
answer = firstNumber - secondNum;
if (problemType == '*')
answer = firstNumber * secondNum;
}
//****************************************************************************
//Function void checkAnswer definition
// :checks the user's answer
// :prints out feedback and increases the count of correct answers for the
// report generated for the user.
//*****************************************************************************
void checkAnswer (int correctAnsw, int answer, int &correctCount) // This function Prototype checks the user's answers
//Pre: validates user answer
//Post: outputs whether or not the user entered the correct answer
{
if (answer == correctAnsw) // switch statement used to determine the output
// " correct" or "incorrect" when he user inputs their answer.
{
cout << "correct" << endl;
correctCount++;
}
else
cout << "incorrect" << endl;
}
//***************************************************************************************************************************************************************
//Pre: Determines how many answers the user input correctly prints out a report telling the user how many problems they got correct out of the total for each set
// Post:and the percentage correct that that equals, and also a total and percentage for all the problems
//****************************************************************************************************************************************************************
void printReport(int probsPerSet, int set1Correct, int m set2Correct, int set3Correct) // Prints a report containing percentages.
//Pre: Analyzes user answers
//Post: outputs a percentage per set and an overall
{
int totalCorrect = set1Correct + set2Correct + set3Correct;
int total = probsPerSet * 3.0; // Assumes three sets of problems are done
// multiplies the number problems chosen by the user by *3
cout.precision(0);
cout.setf(ios::fixed);
cout << endl << endl;
cout << "Set#1: You got " << set1Correct << " out of " << probsPerSet << " for "
<< ((set1Correct / probsPerSet) * 100 )<< "%" << endl;
cout << "Set#2: You got " << set2Correct << " out of " << probsPerSet << " for "
<< ((set2Correct / probsPerSet) * 100 )<< "%" << endl;
cout << "Set#3: You got " << set3Correct << " out of " << probsPerSet << " for "
<< ((set3Correct / probsPerSet) * 100 )<< "%" << endl;
cout << "Overall you got " << totalCorrect << " correct out of " << total << " for "
<< ((totalCorrect / total) * 100 )<< "%" << endl;
}
|