Consider the following C++ program. It prompts the user to enter sequence of values ending with "-1" and then calculates

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
correctanswer
Posts: 43759
Joined: Sat Aug 07, 2021 7:38 am

Consider the following C++ program. It prompts the user to enter sequence of values ending with "-1" and then calculates

Post by correctanswer »

Consider the following C++ program. It prompts the user to enter
sequence of values ending with "-1" and then calculates the average
of these inputs.
Consider The Following C Program It Prompts The User To Enter Sequence Of Values Ending With 1 And Then Calculates 1
Consider The Following C Program It Prompts The User To Enter Sequence Of Values Ending With 1 And Then Calculates 1 (108.19 KiB) Viewed 7 times
Instructions: Consider the following C++ program. It prompts the user to enter sequence of values ending with "-1" and then calculates the average of these inputs. #include using namespace std; int main() { // Declare and initialize variables int InputValue = 0; int TotalValue = 0; int CountValue = 0; // Read input value cout << "Enter input value (or -1 to stop): \n"; cin >> InputValue; // While loop to total up the daily sales while (InputValue != -1) { // Update total and count (FIX) // Read input value cout << "Enter input value (or -1 to stop): \n"; cin >> InputValue; } // Calculate and print average value (FIX) int AverageValue = 0; cout << "Average: << AverageValue << endl; return 0; }
Step 1: Copy this program into your C++ program editor, and compile it. You may see some warnings about unused variables, but hopefully no error messages. Step 2: Run your program type in a sequence of three numbers followed by a "-1" to see what the program outputs. The answer is clearly wrong, so your task is to correctly complete this program. Step 3: Edit the program, and insert the necessary code to update the total and count variables. This trick of adding (FIX) to the comments is a common way to help the programmer quickly find the parts of the program that are under construction. Step 4: Compile and test your program. You should see the compiler warnings go away, but the average will still be incorrect. Step 5: Edit your program again, and correct the formula for AverageValue. Compile and test your program with several input values to verify your formula. For example, if the user types "10 20 -1" the average should be 15. Step 6: One test that many people forget is to consider the "empty case" where the user types in "-1" as their first value. What does your program do in this case? Can you see the line of code that is the problem? Step 7: Edit your program and change all of the "int" variables into "float" variables and compile and run your program again. You should notice that the program prints an average of "nan", which stands for "not a number". In C++ "nan" values are used to identify undefined or non-representable values for floating-point elements, such as the square root of negative numbers or the result of 0/0. Step 8: Instead of relying on C++ nan values, we really should check that the division is valid before we calculate the average. Edit your program and add "if (CountValue > 0)" to your code to prevent the division by zero. When you are finished, test the empty case to verify that nothing is output. Step 9: Once you think your program is working correctly, upload your final program into the auto grader by following the instructions below. Please do not upload programs with infinite loops!!!
Register for solutions, replies, and use board search function. Answer Happy Forum is an archive of questions covering all technical subjects across the Internet.
Post Reply