Create a program that displays five random addition problems, one at a time, on
ID: 3689460 • Letter: C
Question
Create a program that displays five random addition problems, one at a time, on the computer screen. Each problem should be displayed as a question, like this: What is the sum of x + y?. The x and y in the question represent random numbers from 1 to 10, inclusive. After displaying the question, the program should allow the user to enter the answer. It should then compare the user's answer with the correct answer. If the user's answer matches the correct answer, the program should display the "Correct!" message. Otherwise, it should display the "Sorry, the answer is'' message followed by the correct answer and a period. If necessary, create a new project named Advanced24 Project, and save it in the Cpp8Chap09 folder. Enter your C++ instructions into a source file named Advanced24.cpp. Also enter appropriate comments and any additional instructions required by the compiler. Test the program appropriately. In this exercise, you will create a program that displays the amount of a cable bill. The amount is based on the type of customer, as shown in Figure 9-43. For a residential customer, the user will need to enter the number of premium channels only For a business customer, the user will need to enter the number of connections and the number of premium channels. Use a separate program-defined function for each customer type.Explanation / Answer
24)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void checkAnswer(int answer, int userAnswer);
int generateQuestion();
int x;
int y;
int main(int argc, char *argv[])
{
srand (time(NULL));
for(int i=0;i<5;i++)
{
int answer;
int userAnswer;
answer = generateQuestion();
cin>>userAnswer;
checkAnswer(answer,userAnswer);
}
return 0;
}
int generateQuestion()
{
int answer;
x = 1 + rand()%10;
y = 1 + rand()%10;
answer=x+y;
cout<<"What is the sum of "<<x <<"+"<< y<<"?";
return answer;
}
void checkAnswer(int answer, int userAnswer)
{
if(answer == userAnswer)
{
cout<<"Correct! ";
}
else if(answer != userAnswer)
{
cout<<"Sorry, the answer is "<<answer<<". ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.