Question
Loop Errors To gain more practice with loops, as well as provide experience to read/fix someone else's code. You will learn how the flow of
Loop Errors
To gain more practice with loops, as well as provide experience to read/fix someone else's code.
You will learn how the flow of control can be altered with different types of looping statements. You'll see the effect of braces in creating blocks of code, consider how complex boolean expressions are evaluated, and witness common pitfalls in programming conditional looping statements.
Your task for this lab is to fix a program that has several loops and several errors.
#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
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