A programmer friend of yours has written a program. The goal of the program is t
ID: 3580479 • Letter: A
Question
A programmer friend of yours has written a program. The goal of the program is to compute and display the average of two numbers, which have been input by the user. The only problem is his code does not produce the correct output. He heard that you are taking a class in programming and asked you to help him 'debug' his code. Identify at least five run-time errors that will cause the program to produce the incorrect result in the following program (specify the line # and what is incorrect about the line). Note that Line 6 does not have a run time error on it. Even though x1 is NOT initialized, that will NOT effect the outcome of the program. The first error has been identified:
1. #include
2. using namespace std;
3. int main()
4. {
5. int total;
6. int x1;
7. cout << “Please enter a number: “;
8. cin >> x1;
9. total *= x1;
10. int x2;
11. cout << “Please enter another number: “;
12. cin >> x2;
13. total = total + x1;
14. double average = total / 2;
15. cout << “The average is “ << total << endl;
16. return 0;
17. }
I have uncovered the first error and it is below.
Line 5: variable 'total' not initialized. It should be initialized to 0 (zero).
Use the same format in identifying the remaining errors.
Explanation / Answer
Errors :
Line 1: iostream was not included.if not included then input ouput statements can not perform IO operations
( correct statment is : #include<iostream>)
Line 5: variable 'total' not initialized. It should be initialized to 0 (zero).
Line 7 : String quotes “ “ are not correct .( correct statment is : cout << "Please enter a number: ";)
Line 11 : String quotes “ “ are not correct .( correct statment is : cout << "Please enter another number: ";)
Line 13 : toatl is not on two numbers x1,x2 .( correct statment is : total = x2 + x1;)
Line 15 : String quotes “ “ are not correct .( correct statment is : cout << "The average is " ;)
Line 15 :Printing total value insted of average .( correct statment is : cout << "The average is " << average << endl;)
Correct Programme :
#include<iostream>
using namespace std;
int main()
{
int total=0;
int x1;
cout << "Please enter a number: ";
cin >> x1;
total *= x1;
int x2;
cout << "Please enter another number: ";
cin >> x2;
total = x2 + x1;
double average = total / 2;
cout << "The average is " << average << endl;
return 0;
}
Note :still unnecessary code is there to sum of two numbers and find avg.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.