3.35 Use rand
to produce x times y. Check answer. Repeat until correct.
3.36 Use random # generator to vary responses. Use switch to issue responses.
3.37 Count correct/incorrect; calculate %. Print string if <75%.
*/
#include <iostream>
using
std::cout;
using std::cin;
using std::endl;
#include <cstdlib>
#include <ctime>
void
good( void ); // response
randomizers
void bad( void );
int main()
{
int n,
x,
y,
z = 0,
correct = 0,
incorrect = 0;
srand( time (0) ); // random
number generator
cout << "How many questions? ";
cin >> n;
for ( int
i = 1; i <= n; )
{
x = ( 3 + rand()
% 10 );
y = ( 3 + rand()
% 10 );
cout << "How
much is " << x << " x " << y
<< "? ";
cin >> z;
while
( z != x * y ) // repeats until correct
{
++incorrect;
bad();
cout
<< x << " x " << y << " = ";
cin
>> z;
i++;
}
if
( z == x * y )
{
++correct;
good();
i++;
}
}
cout << "Your score is " <<
( correct * 100 ) / ( correct + incorrect ) <<
"%." << endl;
if ( correct / 7.5
< incorrect / 2.5 ) // passing grade is 75%
cout << "Please ask your instructor
for extra help." << endl;
return 0;
}
void
good( void )
{
switch ( rand() %
4 )
{
case 0: cout <<
"Very good!";
break;
case 1: cout <<
"Excellent!";
break;
case 2: cout <<
"Nice work!";
break;
case 3: cout <<
"Keep up the good work!";
break;
}
cout << endl;
}
void
bad( void )
{
switch ( rand()
% 4 )
{
case 0: cout <<
"No. Please try again.";
break;
case 1: cout <<
"Wrong. Try once more.";
break;
case 2: cout <<
"Don't give up!";
break;
case 3: cout <<
"No. Keep trying.";
break;
}
cout << endl;
}