Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Find and fix the errors in this C++ code: * This program illustrates a variety o

ID: 3925925 • Letter: F

Question

 Find and fix the errors in this C++ code:  *    This program illustrates a variety of common loop errors. *    Fix the errors in each section.  */  #include <iostream> 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 // 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 // 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 // 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 // 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 // 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; } 

Explanation / Answer

Source code after correcting error is given below,

Source code :

#include <iostream>
using namespace std;

int main() {
cout << "Welcome to Loop World" << endl;

// SECTION I: There is no error in section 1. it is giving correct output.

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: There is no error in section 1. it is giving correct output.

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: Here the error is inside Do while loop you didn't give a statement to increment counter value by 1 in // each iteration. you have to give counter++ inside Do while loop. otherwise the loop will not stop it will work // infinitely.
// we need to stop the loop when the counter value becomes 5. because we need to calculate sum of // 1 to 4(inclusive) integers.

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;
counter++;
} while (counter <= 4);

cout << endl << "Section III Recap" << endl;

cout << "I calculated the sum of numbers from 1 to 4 (inclusive) as " << sum << endl;


// SECTION IV: here the error is inside for loop you have given i++ instead of i--. you have to decrement i value by //1,because you have started i value from 4.so you have to decrement i value by 1 in each iteration and we have to //reach at a value 1.therefor give i--.

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: here the error is you have given a value 10 to compare with counter value in while loop condition //statement. we are calculating sum of cubes only from the value 1 to 4(inclusive.). so you should give '4' instead of
// '10'.

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 <= 4) {
sum += (counter * 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;
}

*************************END*******************PLS GIVE ME GOOD RATING***********************

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote