a) The use of computers in education is referred to as computer-assisted instruc
ID: 3727030 • Letter: A
Question
a) The use of computers in education is referred to as computer-assisted instruction (CAl). Write a program that will help an elementary school student learn multiplication. Use the rand function to produce two positive one-digit integers The program should then prompt the user with a question, such as How much is 6 times 7? The student then inputs the answer. Next, the program checks the student's answer. If it's correct, display the message "Very good!" and ask another multiplication question. If the answer is wrong, display the message "No. Please try again." and let the student try the same question repeatedly until the student finally gets it right. A separate function should be used to generate each new question This function should be called once when the application begins execution and each time the user answers the question correctly. b) One problem in CAl environments is student fatigue. This can be reduced biy varying the computer's responses to hold the student's attention. Modify the program of "part a" so that various comments are displayed for each answer as follows: Possible responses to a correct answer: Very good! Excellent! Nice work! Keep up the good work! Possible responses to an incorrect answer: No. Please try again Wrong. Try once more Don't give up! No. Keep trying Use random-number generation to choose a number from 1 to 4 that will be used to select one of the four appropriate responses to each correct or incorrect answer. Use a switch statement to issue the responses. Use two functions: one for responses to a correct answer and another for responses to an incorrect answer.Explanation / Answer
#include<iostream>
#include<string>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function to return random message for correct answer
string returnRightMessage()
{
// Stores the message for correct answer
string messageRight[] = {"Very good!", "Excellent!", "Nice Work!", "Keep up the good work"};
// Generates random number between 0 and 3
int no = rand() % 3;
// Returns message at no index position
return messageRight[no];
}// End of function
// Function to return random message for wrong answer
string returnWrongMessage()
{
// Stores the message for correct answer
string messageWrong[] = {"No. Please try again", "Wrong. Try one more", "Don't give up!", "No. Keep trying"};
// Generates random number between 0 and 3
int no = rand() % 3;
// Returns message at no index position
return messageWrong[no];
}// End of function
// Function to display menu for difficulty level, accept user choice for level and return it
int menuLevel()
{
int choice;
// Displays menu
cout<<" 1 for single digit.";
cout<<" 2 for double digit.";
cout<<" 3 for Exit.";
cout<<" Enter the level: ";
// Accepts choice
cin>>choice;
// Returns choice
return choice;
}// End of function
// Function to display menu for arithmetic operation choice, accept user choice and return it
int menuArithmatic()
{
int choice;
// Displays menu
cout<<" 1 for Addition.";
cout<<" 2 for Subtraction.";
cout<<" 3 for Multiplication.";
cout<<" 4 for Division.";
cout<<" 5 for Random mixture of all types.";
cout<<" 6 for Exit.";
cout<<" Enter your choice: ";
// Accepts choice
cin>>choice;
// Returns choice
return choice;
}// End of function
// Function to generate random number and returns it
int randomNumber(int level)
{
// Checks if the level is one
if(level == 1)
// Generate and return random number between 1 and 9
return ((rand() % 8) + 1);
// Checks if level is two
else if(level == 2)
// Generate and return random number between 1 and 99
return ((rand() % 98) + 1);
// Otherwise, for arithmetic operation option 5 selected for random arithmetic operation
else if(level == 5)
// Generate and return random number between 1 and 4
return ((rand() % 3) + 1);
}// End of function
// Function to calculate percentage and return it
float claculatePercentage(int correct)
{
return ((correct / 10.0f) * 100);
}// End of function
// Function to generate question
void generateQuestion(int no1, int no2, int type)
{
// Checks the type of question
switch(type)
{
case 1:
cout<<no1<<" add "<<no2<<"? ";
break;
case 2:
cout<<no1<<" subtract "<<no2<<"? ";
break;
case 3:
cout<<no1<<" times "<<no2<<"? ";
break;
case 4:
cout<<no1<<" divide "<<no2<<"? ";
break;
}// End of switch - case
}// End of function
// main function definition
int main()
{
// Local variable to store data
int c = 0, type;
int correctCount = 0, wrongCount = 0;
float percent, userResult, computerResult, no1, no2;
// Loops till user choice is not 3 or 6
do
{
// Calls the function to select difficulty level
switch(menuLevel())
{
case 1:
srand(c);
// Calls the function to generate random number for level one i.e., single digit
no1 = randomNumber(1);
no2 = randomNumber(1);
break;
case 2:
srand(c);
// Calls the function to generate random number for level two i.e., double digit
no1 = randomNumber(2);
no2 = randomNumber(2);
break;
case 3:
exit(0);
default:
cout<<" Invalid choice!";
}// End of inner switch case
// Calls the function to check type of arithmetic operation
switch(menuArithmatic())
{
case 1:
// Calls the function to generate question
generateQuestion(no1, no2, 1);
// Accept result from the user
cin>>userResult;
// Calculates the result
computerResult = no1 + no2;
break;
case 2:
// Calls the function to generate question
generateQuestion(no1, no2, 2);
cin>>userResult;
computerResult = no1 - no2;
break;
case 3:
// Calls the function to generate question
generateQuestion(no1, no2, 3);
// Accept result from the user
cin>>userResult;
// Calculates the result
computerResult = no1 * no2;
break;
case 4:
// Calls the function to generate question
generateQuestion(no1, no2, 4);
// Accept result from the user
cin>>userResult;
// Calculates the result
computerResult = no1 / (float)no2;
break;
case 5:
// Calls the function to generate type of arithmetic operation randomly
type = randomNumber(5);
// Calls the function to generate question
generateQuestion(no1, no2, type);
cin>>userResult;
// Checks the type of arithmetic operation generated randomly
switch(type)
{
case 1:
// Calculates the result
computerResult = no1 + no2;
break;
case 2:
// Calculates the result
computerResult = no1 - no2;
break;
case 3:
// Calculates the result
computerResult = no1 * no2;
break;
case 4:
// Calculates the result
computerResult = no1 / (float)no2;
break;
}// End of inner switch case
break;
case 6:
exit(0);
default:
cout<<" Invalid choice!";
}// End of outer switch case
// Checks the result of user and computer if both are same
if(userResult == computerResult)
{
// Increase the correct answer counter
correctCount++;
// Displays randomly generated correct answer message
cout<<returnRightMessage();
}// End of if condition
// Otherwise wrong answer given by the user
else
{
// Increase the wrong answer counter
wrongCount++;
// Displays randomly generated wrong answer message
cout<<returnWrongMessage();
}// End of else
// Increase the number of question generated
c++;
// Checks if number of question is 10
if(c == 10)
{
// Calculates the percentage
percent = claculatePercentage(correctCount);
// Checks if he percentage is less than 75% display message
if(percent < 75)
cout<<" Please ask your teacher for extra help.";
// Otherwise, percentage is greater than or equals to 75% display message
else
cout<<" Congratulations you are ready to go to the next level!";
// Reset the counters to zero
correctCount = wrongCount = c = 0;
}// End of if condition
}while(1); // End of do - while loop
}// End of main function
Sample Output:
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 1
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Random mixture of all types.
6 for Exit.
Enter your choice: 4
7 divide 8? 0.875
Excellent!
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 2
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Random mixture of all types.
6 for Exit.
Enter your choice: 1
42 add 44? 86
Excellent!
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 2
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Random mixture of all types.
6 for Exit.
Enter your choice: 3
46 times 13? 123
No. Please try again
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 2
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Random mixture of all types.
6 for Exit.
Enter your choice: 2
49 subtract 43? 6
Very good!
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 1
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Random mixture of all types.
6 for Exit.
Enter your choice: 1
4 add 2? 6
Very good!
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 2
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Random mixture of all types.
6 for Exit.
Enter your choice: 4
55 divide 78? 1.23
No. Please try again
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 1
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Random mixture of all types.
6 for Exit.
Enter your choice: 3
3 times 2? 6
Nice Work!
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 1
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Random mixture of all types.
6 for Exit.
Enter your choice: 2
6 subtract 7? -1
Nice Work!
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 2
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Random mixture of all types.
6 for Exit.
Enter your choice: 1
65 add 45? 110
Nice Work!
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 1
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Random mixture of all types.
6 for Exit.
Enter your choice: 3
5 times 8? 40
Excellent!
Congratulations you are ready to go to the next level!
1 for single digit.
2 for double digit.
3 for Exit.
Enter the level: 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.