C++ Help-Fix Common Loop Errors

Sep 22nd, 2015
Anonymous
Category:
Engineering
Price: $25 USD

Question description

I need help with this assignment that deals with fixing common loop errors. Please follow the instructions carefully. Also, please make sure the fixes work and the code runs before posting it, just to save time and trouble. Please ask if you have any questions, I am quick to reply.

---------------------------------------------------------

Instructions: 

This 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.

The program contains three sections with infinite loops. Some infinite loops will cause a message to keep scrolling on your screen. For others, the cursor will just be blinking and nothing will appear (at least immediately) on your screen. Clicking on the X in the upper right of console window will break out of an infinite loop. You might want to challenge yourself to identify the infinite loops before you try to run the program. But running the program to see where the infinite loops occur is also acceptable.


*    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 << "Section I" << endl;

    int sum;  // Accumulates the total 
    int 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 << "Section II" << endl;

    double total;     // Accumulates total
    double price;    // Gets next price from user
    int num_items;     // Number of items
    int counter = 1;  // Loop control counter

    cout << "How many items do you have? ";
    cin >> num_items;

    while (counter < num_items) {
        total = 0; 
        cout << "Enter the price of item " << counter << ": ";
        cin >> price;
        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 << "Section III" << endl;

    char reply;  // loop control variable 
    do {
        cout << "Enter the price of the next item: ";
        cin >> price;
        total += price;

        cout << "Do you have more items? (y or n) ";
        cin >> reply;
    } while (reply == 'y' && reply == 'Y');
    
    cout << "The total price is: " << total << endl;

// SECTION IV: update comment below on how you fixed this section's code, and tests run 
// FIX = 
// TESTS: 

    cout << endl << "Section IV" << 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);

// SECTION V: update comment below on how you fixed this section's code, and tests run 
// FIX = 
// TESTS: 

    cout << endl << "Section V" << 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 VI: update comment below on how you fixed this section's code, and tests run 
// FIX = 
// TESTS: 

    cout << endl << "Section VI" << 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;

// SECTION VII: update comment below on how you fixed this section's code, and tests run 
// FIX = 
// TESTS: 

    cout << endl << "Section VII" << endl;

    cout << "I can calculate the sum of 1 to 5 using a for loop" << endl; 
    sum=0;
    for (counter=0; counter<= 5; counter++); 
       sum += counter;

    cout << "The sum of numbers from 1 to 5 (inclusive) is: " << sum << endl;
  
// SECTION VIII: update comment below on how you fixed this section's code, and tests run 
// FIX = 
// TESTS: 

    cout << endl << "Section VIII" << endl;

    cout << "I can print all the odd numbers from 1 to 9" << endl;

    for (counter=1; counter != 9; counter=counter+2)
        cout << counter << endl;

    return 0;
}

Tutor Answer

(Top Tutor) Studypool Tutor
School: Purdue University
PREMIUM TUTOR

Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1820 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors