Code in C++ Please code in more basic C++ (im still learning the basics) a) The
ID: 3732735 • Letter: C
Question
Code in C++ Please code in more basic C++ (im still learning the basics)
a) The use of computers in education is referred to as computer-assisted instruction (CAI). 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 CAI environments is student fatigue. This can be reduced by 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.
c) More sophisticated computer-assisted instruction systems monitor the student’s performance over a period of time. The decision to begin a new topic is often based on the student’s success with previous topics. Modify the program of "part b" to count the number of correct and incorrect responses typed by the student. After the student types 10 answers, your program should use a function to calculate the percentage that are correct. If the percentage is lower than 75%, display "Please ask your teacher for extra help.", then reset the program so another student can try it. If the percentage is 75% or higher, display "Congratulations, you are ready to go to the next level!", then reset the program so another student can try it.
d) Part c" developed a computer-assisted instruction program to help teach an elementary school student multiplication. Modify the function you create in "part a" to allow the user to enter a difficulty level. At a difficulty level of 1, the program should use only single-digit numbers in the problems; at a difficulty level of 2, numbers as large as two digits, and so on.
e) Modify the program of "part d" to allow the user to pick a type of arithmetic problem to study. An option of 1 means addition problems only, 2 means subtraction problems only, 3 means multiplication problems only, 4 means division problems only and 5 means a random mixture of all these types.
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: 1
7 add 8? 15
Excellent!
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
2 subtract 4? -3
Wrong. Try one more
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
6 times 1? 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: 4
1 divide 5? 1
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: 1
52 add 12? 64
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: 5
7 add 6? 13
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: 3
59 times 10? 590
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: 4
62 divide 77? 3
Don't give up!
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: 5
65 times 45? 700
Don't give up!
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: 5
69 subtract 76? 800
No. Please try again
Please ask your teacher for extra help.
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
7 add 8? 15
Excellent!
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
2 add 4? 6
Excellent!
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 1? 5
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: 3
1 times 5? 5
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: 1
52 add 12? 64
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: 3
55 times 78? 4290
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: 5
3 times 2? 6
Excellent!
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: 5
6 times 7? 42
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: 5
1 times 3? 3
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: 2
69 subtract 76? -7
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