Question

In: Computer Science

Please use c++ and follow the instruction, I really want to fully understand this question and...

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:

  • 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. 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;
}

Solutions

Expert Solution

#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


Related Solutions

please I don't really know how to start answering this question I really need to understand...
please I don't really know how to start answering this question I really need to understand it please show the work with a clear handwriting A collision in one dimension A mass m1 = 2 kg moving at v1i = 3 ms−1 collides with another mass m2 = 4 kg moving at v2i = −2 ms−1. After the collision the mass m1 moves at v1f = −3.66 ms−1. (a) Calculate the final velocity of the mass m2. (b) After the...
So for the following question I really want to understand how to approach the problem. I'm...
So for the following question I really want to understand how to approach the problem. I'm having a really hard time understanding the concept so a detailed answer (that's also dumbed down) will be much appreciated! If a drug has a half-life of 10 hours and is taken orally, how long will it take until the subject achieves a steady state drug concentration?
please do this in C++! I want to understand it, it must be done before the...
please do this in C++! I want to understand it, it must be done before the evening or nightime. Follow instructions exactly as it says. Please send a screenshot also with your code so I can see how it is supposed to be formatted. Since typing it a chegg answer, makes it look a bit messy. Your program will read in a file of commands. There are three types of commands: Warrior creates a new warrior with the specified name...
I want to understand how this can be solved in c++. Please send a screenshot also...
I want to understand how this can be solved in c++. Please send a screenshot also with your code so I can see how it is supposed to be formatted or indented. Instructions: Your program will read in a file of commands. There are three types of commands: Warrior creates a new warrior with the specified name and strength. Battle causes a battle to occur between two warriors. Status lists all warriors, alive or dead, and their strengths. A sample...
HI, I hope you are doing well. I really don't understand this question and don't know...
HI, I hope you are doing well. I really don't understand this question and don't know how to solve it at all because I am completely new to this c++ programming. can you please explain each line of code with long and clear comments? please think of me as someone who doesn't know to code at all. and I want this code to be written in c++ thank you very much and I will make sure to leave thumbs up....
I really do not understand how to explain aggregate willingness to pay. This question on my...
I really do not understand how to explain aggregate willingness to pay. This question on my homework is throwing me through a loop. Suppose you want to determine the aggregate willingness to pay among students at your school or employees at your employer for increasing recycling at the school or workplace. How might you do this? Can someone explain aggregate willingness to pay Then how to relate this back to the homework questions.
I really don't understand what the following question is trying to ask. In regards to medical...
I really don't understand what the following question is trying to ask. In regards to medical marijuana was there a trade-off between science and politics?
PLEASE ANSWER IN PSEUDOCODE USING BUBBLE FULLY --- trying to understand steps for a test Question...
PLEASE ANSWER IN PSEUDOCODE USING BUBBLE FULLY --- trying to understand steps for a test Question 4) Sorting: Sort {5 4 3 2 1} into ascending order using the sorting algorithm of your choice (Bubble, Exchange, Insertion or Selection). You must show each movement of the sort to get any credit (no need to write down the algorithm) (15 points)
please use computer to do this question ( I do not understand hand writing) 4. Determine...
please use computer to do this question ( I do not understand hand writing) 4. Determine if the following production technologies exhibit IRS, DRS, or CRS, and explain why. (1 point each)       a. q=2KL       b. q=5KL       c. q=       d. q=       e. q=K+L       f. q=3K+3L       g.q=8(K+L)
No sloppy writing, please! I want to be able to see and understand each step :)...
No sloppy writing, please! I want to be able to see and understand each step :) The three polarizing filters are bundled so that the polarizing axes of the second and third filters are 33 ° and 71 °, respectively, relative to the polarizing axis of the first filter. Intensity of unpolarized light hitting the bundle after passing through the bundle is 53 W / cm^2. If the intensity of the incoming light is kept constant, what is the intensity...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT