Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C++ ONLY - The LoopErrors_main.cpp program, as it currently stands, contains a number of errors related to the learning objectives stated. Your task is

IN C++ ONLY - The LoopErrors_main.cpp program, as it currently stands, contains a number of errors related to the learning objectives stated. Your task is to correct the program, describe the type of error(s) you found, and list all the different values you need to input to test all possible cases in each section of the program. You should write this information (errors found/corrected and test values) as a comment at the top of each program section.

Compile and run the program. It is obviously not correct!

For each section, you must correct the error or errors. To identify the errors, you may need to do some calculations. For example, if the program says it is calculating the sum of the numbers from 1 to 5 (inclusive), the result should be 15.

 *CSCI 261: Fix Loop Errors * * Author: XXXX (_INSERT_YOUR_NAME_HERE_) * * This program illustrates a variety of common loop errors. * Fix the errors in each section. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include using namespace std; int main() { cout << "Welcome to Loop World" << endl; // SECTION I: update comment below on how you fixed this section's code, and tests run // that show loop is working properly // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section I" << endl; cout << "******************" << endl; short sum; // Accumulates the total short i; // Used as loop control variable for (i = 1; i < 5; ++i) { sum += i; } cout << "The sum of the numbers from 1 to 5 (inclusive) is: " << sum << endl; // SECTION II: update comment below on how you fixed this section's code, and tests run // that show loop is working properly // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section II" << endl; cout << "******************" << endl; double total; // Accumulates total double price; // Gets next price from user short num_items; // Number of items short counter = 1; // Loop control counter cout << "How many items do you have? "; cin >> num_items; cout << endl; while (counter <= num_items) { total = 0; cout << "Enter the price of item " << counter << ": "; cin >> price; cout << endl; total += price; counter++; } cout << "The total price is: " << total << endl; // SECTION III: update comment below on how you fixed this section's code, and tests run // that show loop is working properly // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section III" << endl; cout << "******************" << endl; cout << "I will now calculate "; cout << "the sum of numbers from 1 to 4 (inclusive)" << endl; sum=0; counter = 1; do { sum += counter; cout << "Sum so far: " << sum << endl; } while (counter <= sum); cout << endl << "Section III Recap" << endl; cout << "I calculated the sum of numbers from 1 to 4 (inclusive) as " << sum << endl; // SECTION IV: update comment below on how you fixed this section's code, and tests run // that show loop is working properly // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section IV" << endl; cout << "******************" << endl; cout << "I will now calculate "; cout << "the sum of squares from 1 to 4 (inclusive)" << endl; sum = 0; for (i=4; i>0; i++) { sum += i*i; } cout << "The sum of squares from 1 to 4 is: " << sum << endl; // SECTION V: update comment below on how you fixed this section's code, and tests run // that show loop is working properly // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section V" << endl; cout << "******************" << endl; cout << "I will now calculate "; cout << "the sum of cubes from 1 to 4 (inclusive)" << endl; sum = 0; counter = 1; while (counter < 10) { sum += (counter * counter * counter); } counter++; cout << "The sum of cubes from 1 to 4 is: " << sum << endl; cout << endl; cout << "******************" << endl; cout << "Section Done" << endl; cout << "******************" << endl; cout << endl << "Congrats! You fixed them all (hopefully correctly!)" << endl << endl << "Goodbye" << endl << endl; return 0; }

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

Question

5. If yes, then why?

Answered: 1 week ago

Question

3. What changes should I be making?

Answered: 1 week ago