In: Computer Science
Please use c++ and follow the instruction, I really want to fully understand this question and I will use your code to check where I made mistake (Do not skip steps).
I have written my own code which has tons of errors, and I am so confused about this lab. I need help.
Lab 6.4 – C++ and Functions – Math Test
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 <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:
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. Copy and paste both the source code and the output.
here is my work
#include<iostream>
#include<string>
#include <cstdlib>
#include <ctime>
using namespace std;
void displayInfo(int right, string studentName);
void generateNumbers(int& number1, int& number2);
string inputName();
int main()
{
//Declare local variables
int counter;
string studentName;
int right = 0;
int number1;
int number2;
int answer;
srand(static_cast<unsigned int>(time(0)));
// to generate random sequence each time
studentName = inputName();
//Loop to run program again
for (counter = 1; counter <= 5; counter++)
{
generateNumbers(int& number1,
int& number2);
answer = getAnswer(number1,
number2);
if answer == number1 +
number2
cout << "Right";
right = right + 1
else
cout <<
"Wrong";
}
void displayInfo(int right, string studentName);
//definition 1 input name
string inputName()
{
string studentName;
cout<< "Enter student name";
cin>> name;
return name;
}
//MODULE 2 generateNumbers
void generateNumbers(int& number1, int& number2)
{
number1 = rand() % 500 + 1;
number2 = rand() % 500 + 1;
}
//definition 3 getAnswer
int getAnswer(int number1, int number2)
{
int answer;
cout << "Equation is", number1, "+",
number2;
cout << "Enter the sum:";
answer=answer1 + answer2;
return answer
}
//MODULE 4 displayInfo
void displayInfo(int right, string studentName)
{
cout << "Information for" <<
studentName;
cout << "The number right" << right;
}
#include<iostream>
#include<string>
#include <cstdlib>
#include <ctime>
using namespace std;
//definition 1 input name
string inputName(){
string name;
cout<<"Enter student name: ";
cin>>name;
return name;
}
//MODULE 2 generateNumbers
void generateNumbers(int &number1, int &number2){
number1 = rand() % 500 + 1;
number2 = rand() % 500 + 1;
}
//definition 3 getAnswer
int getAnswer(int number1, int number2){
int answer;
cout<<"Equation is "<< number1 << " + "<< number2<<"\n";
cout<<"Enter the sum: ";
cin>>answer;
return answer;
}
//MODULE 4 displayInfo
void displayInfo(int right, string studentName){
cout<<"Information for "<< studentName <<"\n";
cout<<"The number right: "<< right <<"\n";
}
// Main module
int main(){
//Declare local variables
int counter;
string studentName;
int right = 0;
int number1;
int number2;
int answer;
// to generate random sequence each time
srand(static_cast<unsigned int>(time(0)));
studentName = inputName();
//Loop to run program again
for(counter = 1; counter <= 5 ; counter++){
generateNumbers(number1, number2);
answer = getAnswer(number1, number2);
if(answer == number1 + number2){
cout<<"Right\n";
right += 1;
}
else{
cout<<"Wrong\n";
}
}
displayInfo(right, studentName);
return 0;
}
Sample Output:
To compile: g++ <filename> -o game
To execute: ./game