Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LAB 1.3 Run-time Errors A run-time error occurs when a program instruction tells the program to do something it is unable to correctly do. This

LAB 1.3 Run-time Errors

A run-time error occurs when a program instruction tells the program to do something it is unable to correctly do. This type of error cannot be detected by a compiler because no syntax rules have been broken. So the program will compile but, as the name suggests, something will go wrong when the program is run. In some cases a run-time error causes the program to abort; in others it continues running, but produces incorrect results. Lets take a look at a common run-time error, attempting to divide by zero.

Step 1: Add the average.cpp program from your Lab1 folder to the project. Here is a copy of the source code.

1 // average.cpp 2 // This program finds the average of two numbers. 3 // It contains two errors that must be fixed. 4 #include 5 using namespace std; 6 7 int main () 8 { 9 int size = 0; // The number of values to be averaged 10 double num1, 11 num2, 12 average; // Average of num1 and num2 13 14 // Get the two numbers 15 cout << "Enter two numbers separated by one or more spaces: "; 16 cin >> num1 >> num2; 17 18 // Calculate the average 19 average = num1 + num2 / size; 20 21 // Display the average 22 cout << "The average of the two numbers is: " << average << endl; 23 24 return 0; 25 }

Step 2: Read through the source code to see if you can spot any errors in the program. Two lines contain errors, but even if you can find them, do not fix them yet.

Step 3: Compile the program. Since it contains no syntax errors, the compiler should not detect any errors.

Step 4: Run the program and, at the prompt, enter 10 5. Did the run-time error cause the program to abort or to produce incorrect results? If it ran without aborting it probably displayed something like this:

Enter two numbers separated by one or more spaces: 10 5

The average of the two numbers is: 1.#INF

Step 5: The error occurs on line 19 when a quantity is divided by size. Notice that size is set in line 9 to 0. Correct the error by changing line 9 of the source code to set size equal to 2 so that the quantity on line 19 will be divided by 2. Then recompile and rerun the program, again entering 10 and 5 for the two numbers. The output should now look like this:

Enter two numbers separated by one or more spaces: 10 5

The average of the two numbers is: 12.5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions