I don\'t get this question at all Run the following program, and explain the out
ID: 670316 • Letter: I
Question
I don't get this question at all
Run the following program, and explain the output you get.
#include using namespace std; int main() { int total; cout > x1; cout > x2; total = total + x2; cout << "total = " << total << " "; total = total / 2; cout << "total = " << total << " "; cout << "The average is " << total << " "; return 0; }
Note the trace messages that are inserted to show the current contents of the total variable. Then fix up the program, run it with the trace messages in place to verify it works correctly, and remove the trace messages.
Explanation / Answer
You didnot define x1 and x2 and didnot initalized the total.
Assuming you are taking x1 and x2 as inputs from user and initalizing total =0
You are adding x2 to the total, later halving the total and printing it as total and average.
#include <iostream>
using namespace std;
int main()
{
int total = 0;
int x1 = 2;
int x2 = 5;
cout << "x1 = "<< x1 <<endl;
cout << "x2="<<x2 <<endl;
total = total + x2;
cout << " total = " << total << " ";
total = total / 2;
cout << "total = " << total << " ";
cout << "The average is " << total << " ";
}
x1 = 2
x2=5
total = 5
total = 2
But you want to calculat avg and total of two numbers then the programme should be like below
#include <iostream>
using namespace std;
int main()
{
int total = 0;
int x1 = 2;
int x2 = 5;
//read variables from user like below
//cin >> x1
//cin >> x2
cout << "x1 = "<< x1 <<endl;
cout << "x2="<<x2 <<endl;
total = x1 + x2;
cout << " total = " << total << " ";
float avg = (float) total / 2;
cout << "total = " << total << " ";
cout << "The average is " << avg << " ";
}
The average is 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.