Part II – C++ and Functions – Math Test MUST BE IN C++ Critical Review A value-returning function is a function that returns a value back to the part of the program that called it. In C++, you can use value-returning functions and those that do not. y = sqrt(x); // value returning function printBonus(stAmount, empAmount);// function returns no value Standard Library Functions C++ comes with a standard library of functions that have already been written for you. These functions, known as library functions, make a programmer’s job easier because they perform many of the tasks that programmers commonly need to perform. The rand Function In order to use the random function in C++, you must include library. You also should provide a random seed in order to get a random sequence every time you run the program by using library. To do this, simply add the following line to the top of your code: #include #include Add the following statement in main before using the rand() function: srand(static_cast(time(0))); The rand() function typically returns a number between 0 and 32767. The following is how you would get a random number between 1 and 6. value = rand() % 6 + 1; Write a program that will allow a student to enter their name and then ask them to solve 5 mathematical equations. The program should display two random numbers that are to be added, such as: 247 + 129 The program should allow the student to enter the answer. The program should then display whether the answer was right or wrong and accumulate the correct values. After the 5 questions are asked, display the student name and the number correct. In addition to any library functions you may use, you might consider the following functions: A function that allows the student to enter their name. A function that generates two random numbers, anywhere from 1 to 500. A function that displays the equation and asks the user to enter their answer. A function that calculates the results. A function that displays the student name and the number right. Your sample output might look as follows (random numbers will be different): Enter student name: Katie Equation is 424 + 28 Enter the sum: 472 Wrong Equation is 163 + 233 Enter the sum: 396 Right Etc…(through 5 iterations) Information for Katie The number right: 3 The Pseudocode Module main() //Declare local variables Declare Integer counter Declare String studentName Declare Integer right = 0 Declare Integer number1 Declare Integer number2 Declare Integer answer Call srand(time(0)) // to generate random sequence each time Set studentName = inputName() //Loop to run program again For counter = 1 to 5 //calls functions Call generateNumbers(number1, number2) Set answer = getAnswer(number1, number2) If answer == number1 + number2 then Display “Right” Set right = right + 1 Else Display “Wrong” End If End For Call displayInfo(right, studentName) End Module Function String inputName() Declare String name Display “Enter student name: ” Input name Return name End Function Module generateNumbers(Integer Ref number1, Integer Ref number2) Set number1 = rand() % 500 + 1 Set number2 = rand() % 500 + 1 End Module Function Integer getAnswer(Integer number1, Integer number2) Declare Integer answer Display “Equation is ”, number1, “+”, number2 Display “Enter the sum: ” Input answer Return answer End Function Module displayInfo(Integer right, String studentName) Display “Information for ”, studentName Display “The number right:”, right End Module When your code is complete and runs properly, capture the output.
Part II – C++ and Functions – Math Test
MUST BE IN C++
Critical Review
A value-returning function is a function that returns a value back to the part of the y = sqrt(x); // value returning function printBonus(stAmount, empAmount);// function returns no value
Standard Library Functions C++ comes with a standard library of functions that have already been written for you. These functions, known as library functions, make a programmer’s job easier because they perform many of the tasks that programmers commonly need to perform.
The rand Function In order to use the random function in C++, you must include <cstdlib> library. You also should provide a random seed in order to get a random sequence every time you run the program by using <ctime> library. To do this, simply add the following line to the top of your code: #include <cstdlib>#include <ctime>
Add the following statement in main before using the rand() function: srand(static_cast<unsigned int>(time(0)));
The rand() function typically returns a number between 0 and 32767. The following is how you would get a random number between 1 and 6. value = rand() % 6 + 1;
|
Write a program that will allow a student to enter their name and then ask them to solve 5 mathematical equations. The program should display two random numbers that are to be added, such as:
247 + 129
The program should allow the student to enter the answer. The program should then display whether the answer was right or wrong and accumulate the correct values. After the 5 questions are asked, display the student name and the number correct.
In addition to any library functions you may use, you might consider the following functions:
- A function that allows the student to enter their name.
- A function that generates two random numbers, anywhere from 1 to 500.
- A function that displays the equation and asks the user to enter their answer.
- A function that calculates the results.
- A function that displays the student name and the number right.
Your sample output might look as follows (random numbers will be different):
Enter student name: Katie<Enter>
Equation is 424 + 28
Enter the sum: 472<Enter>
Wrong
Equation is 163 + 233
Enter the sum: 396<Enter>
Right
Etc…(through 5 iterations)
Information for Katie
The number right: 3
The Pseudocode
Module main()
//Declare local variables
Declare Integer counter
Declare String studentName
Declare Integer right = 0
Declare Integer number1
Declare Integer number2
Declare Integer answer
Call srand(time(0)) // to generate random sequence each time
Set studentName = inputName()
//Loop to run program again
For counter = 1 to 5
//calls functions
Call generateNumbers(number1, number2)
Set answer = getAnswer(number1, number2)
If answer == number1 + number2 then
Display “Right”
Set right = right + 1
Else
Display “Wrong”
End If
End For
Call displayInfo(right, studentName)
End Module
Function String inputName()
Declare String name
Display “Enter student name: ”
Input name
Return name
End Function
Module generateNumbers(Integer Ref number1, Integer Ref number2)
Set number1 = rand() % 500 + 1
Set number2 = rand() % 500 + 1
End Module
Function Integer getAnswer(Integer number1, Integer number2)
Declare Integer answer
Display “Equation is ”, number1, “+”, number2
Display “Enter the sum: ”
Input answer
Return answer
End Function
Module displayInfo(Integer right, String studentName)
Display “Information for ”, studentName
Display “The number right:”, right
End Module
When your code is complete and runs properly, capture the output.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images