Question
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started