Debugging (1 question, 14 points) Circle all syntax errors and logic errors in f
ID: 3795175 • Letter: D
Question
Debugging (1 question, 14 points) Circle all syntax errors and logic errors in following code, and correct it. There will be more than 14 errors. One correction earns one point until all 14 points are rewarded. However, if the student mark a correct place as wrong and change it, one point will be reduced.
include
using namespace std
int Main()
{
int n;
sum = 0;
cout >> "Enter a positive integer n:";
cin << n; while(n <= 0)
{
cout << "wrong input. Please enter a positive integer n: " cin >> n;
}
int i = 1;
// initialization while(i <=n)
//check condition
{
sum += i; i+;
//increase i by 1
}
switch(n)
{
case 1:
cout << "The sum is" << sum << endl;
case 2:
cout << "The sum of 1+"
<n << "=" << sum <<endl;
break; case 3
cout << "The sume of 1+2+" <<n <<"=" << sum <<endl;
cout << "The sum of 1+2+...+" <<n << "=" << sum <<endl; break
system(pause);
return 0; }
Explanation / Answer
Found total 18 Error.
// 1 include
#include <iostream>
// 2 using namespace std
using namespace std;
// 3 int Main()
int main()
{
int n;
// 4 sum = 0;
int sum = 0;
int i = 1;
// 5 cout >> "Enter a positive integer n:";
cout << "Enter a positive integer n:";
// 6 cin << n; while(n <= 0)
cin >> n;
while(n <= 0)
{
// 7 cout << "wrong input. Please enter a positive integer n: " cin >> n;
cout << "wrong input. Please enter a positive integer n: ";
cin >> n;
}
// 8 int i = 1 not allowed here;
// 9 initialization while(i <=n)
// initialization
while(i <=n)
//check condition
{
// 10 sum += i; i+;
sum += i;
i++;
//increase i by 1
}
switch(n)
{
case 1:
cout << "The sum is" << sum << endl;
// 18 break not present;
break;
case 2:
// 12 cout << "The sum of 1+"
cout << "The sum of 1+" << n << "=" << sum <<endl;
// 13 <n << "=" << sum <<endl;
break;
// 13 case 3
case 3:
cout << "The sume of 1+2+" <<n <<"=" << sum <<endl;
break;
// 14 Default:
default :
cout << "The sum of 1+2+...+" <<n << "=" << sum <<endl;
// 15 break
break;
// 16 system(pause);
return 0; }
// 17 end bracket not present
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.