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; }
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***********************
Find and fix the errors in this C++ code: * This program illustrates a variety of...
What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...
I need help finding the several errors and several loop errors for I get the final output. C++ cLion #include <iostream> using namespace std; int main() { cout << "Welcome to Loop World" << endl; cout << endl; cout << "******************" << endl; cout << "Section I" << endl; cout << "******************" << endl; int sum; // Accumulates the total int i; // Used as loop control variable int numIter; // The number of times to iterate cout << "Enter...
C++ class 3.2.4: If-else statement: Fix errors. Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if (userNum > 0) cout << "Positive." << endl; else cout << "Not positive, converting to 1." << endl; Answer #include using namespace std; int main() { int userNum; cin >> userNum; /* Your solution goes here */ return 0; userNum = 1; cout << "Final: " << userNum << endl;
Please answer in C++ ONLY, and please fill all comments elaboratively. Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are required for this question. Run and provide a single screenshot showing the output. Complete the missing comments to explain the action performed by each statement highlighted in red. The first comment is given as an example. Copy and paste the source code with the comments filled out in your...
NEED HELP WITH THIS!! Open with Drive N // Test1B-Debug.cpp .This is a program with 6 errors in it Find and fix each error. // Simple Container made from circular paper // Test-1B Debug Program #include #include
C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream> using namespace std; int main() { //Part A int numA numA = 10; cout << numA << end; /* Part B */*/ int numB = numA; cin >> usrInput; int usrInput = 0; numB = numB + usrInput; cout << numB <"/n"; /* Part C */ int numC = 10000000000; cout << numC << endl; return 0; }
I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs can be found at compile time, while others are found at run time. Access the UserMenu.cpp code file (in the zip file in Start Here) and use debugging practices and the debugger in Visual Studio to find each bug. Fix the bug and write a comment in the code giving a description of how you found the...
A) Fix any errors to get the following program to run in your environment. B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...
12) 8 pts. Find the errors in the following code fragment and correct them. #i nclude <iostream> using namespace std; double fudge (double s) f return s 2.3; int mainO cout >> "Your original estimate" double estimate; cin >> estimate; cout << endl; for (int 1 = 0;1c3;i++); cout << "EStimate' < fudge(estimate) <<endl Hint: There are 4 syntax errors. There is one error that is syntactically correct but causes the output to not be what you would expect. Once...
The C++ code below will compile but has a variety of runtime issues. Identify a runtime issue with the program and describe how you might fix the problem. #include <iostream> #include <vector> using namespace std; //------------------------------------------------------------------------------ int main() { vector<double> temps; // temperatures double temp = 0; double sum = 0; double high_temp = 0; double low_temp = 0; while (cin >> temp) // read and put into temps temps.push_back(temp); for (int i = 0; i < temps.size(); ++i) {...