FILL IN THIS CODE THIS IS C++ //************************************************
ID: 3673290 • Letter: F
Question
FILL IN THIS CODE THIS IS C++ //************************************************************************************************** // FILE: lab05.cpp // // DESCRIPTION: Generates and displays a requested number of // random simple division problems for the user to answer. // // This lab project illustrates the following C++ concepts: // 1. Functions. Function calls. Pass parameters by-value. Return values. // 2. Writing loops. // 3. Writing if statements, if-else statements, and if-elseif-elseif... statements. // // AUTHORS: <your-name> (<your-email-address) // <your-partner's-name> (<your-partners-email-address>) // // COURSE: CSE100 Principles of Programming with C++, Spring 2015 // // LAB INFO: Lab Number 5; Date/Time: <your-lab-date-and-time>; TA: <your-lab-ta> // // ------------------------------------------------------------------------------------------------- #include <iostream> using namespace std; #include <cstdlib> // For rand() #include <iomanip> // For setprecision(), setw() #include <iostream> // For endl, fixed #include <string> // For string class //-------------------------------------------------------------------------------------------------- // FUNCTION: DisplayDivisionProblem() // // Displays a division problem with the given dividend and divisor for the user. // //-------------------------------------------------------------------------------------------------- void DisplayDivisionProblem(int dividend, int divisor) { cout << "What is " << dividend << " / " << divisor << " ? --->"; } //-------------------------------------------------------------------------------------------------- // FUNCTION: CalcGrossPay() // // Generates and returns a random integer which is between the minimum number (in // parameter min) and the maximum number (in parameter max). // Both the min value and the max vale are valid return values. // //-------------------------------------------------------------------------------------------------- int GetRandomInt(int min, int max) { int randomInt = rand() % (max - min + 1) + min; return randomInt; } int main() { // ask the user how many math problems they would like to answer // continue to generate and display new “random” math problems for the user // as long as they have not yet correctly answered the requested number of problems. // generate new math problem // display math problem // ask the user for the answer to the same current problem until they get the correct answer. // give user feedback on thier answer: "Correct", "Too High" or "Too Low" // display the average number of attempts that the user required to get the correct answer return 0; }
Explanation / Answer
//**************************************************************************************************
// FILE: lab05.cpp
//
// DESCRIPTION: Generates and displays a requested number of
// random simple division problems for the user to answer.
//
// This lab project illustrates the following C++ concepts:
// 1. Functions. Function calls. Pass parameters by-value. Return values.
// 2. Writing loops.
// 3. Writing if statements, if-else statements, and if-elseif-elseif... statements.
//
// AUTHORS: <your-name> (<your-email-address)
// <your-partner's-name> (<your-partners-email-address>)
//
// COURSE: CSE100 Principles of Programming with C++, Spring 2015
//
// LAB INFO: Lab Number 5; Date/Time: <your-lab-date-and-time>; TA: <your-lab-ta>
//
// -------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
#include <cstdlib> // For rand()
#include <iomanip> // For setprecision(), setw()
#include <iostream> // For endl, fixed
#include <string> // For string class
//--------------------------------------------------------------------------------------------------
// FUNCTION: DisplayDivisionProblem()
//
// Displays a division problem with the given dividend and divisor for the user.
//
//--------------------------------------------------------------------------------------------------
void DisplayDivisionProblem(int dividend, int divisor)
{
cout << "What is " << dividend << " / " << divisor << " ? (quotient only) --->";
}
//--------------------------------------------------------------------------------------------------
// FUNCTION: CalcGrossPay()
//
// Generates and returns a random integer which is between the minimum number (in
// parameter min) and the maximum number (in parameter max).
// Both the min value and the max vale are valid return values.
//
//--------------------------------------------------------------------------------------------------
int GetRandomInt(int min, int max)
{
int randomInt = rand() % (max - min + 1) + min;
return randomInt;
}
int main()
{
// ask the user how many math problems they would like to answer
int n;
int correct=0,divisor,dividend,answer,total_attempts=0;
cout<<"How many math problems you would like to answer? ";
cin>>n;
// continue to generate and display new “random” math problems for the user
// as long as they have not yet correctly answered the requested number of problems.
while(correct!=n)
{
// generate new math problem
dividend = GetRandomInt(50,100);
divisor = GetRandomInt(1,50);
// display math problem
DisplayDivisionProblem(dividend,divisor);
// ask the user for the answer to the same current problem until they get the correct answer.
cin>>answer;
while(answer!=(dividend/divisor))
{
total_attempts++;
if(answer<(dividend/divisor))
// give user feedback on thier answer: "Correct", "Too High" or "Too Low"
cout<<"Too low!! ";
else if(answer>(dividend/divisor))
cout<<"Too High!! ";
DisplayDivisionProblem(dividend,divisor);
cin>>answer;
}
cout<<"Correct ";
total_attempts++;
correct++;
}
// display the average number of attempts that the user required to get the correct answer
cout<<"Average no. of attempts = "<<total_attempts*1.0/n<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.