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

C++ List any syntax or logic errors in the following program segments. a) int nu

ID: 3811583 • Letter: C

Question

C++ List any syntax or logic errors in the following program segments.

a)
int num1 = 0, num2 = 10, result;
num = num + 1;
result = ++(num1 + num2);
cout << num1 << “ “ << num2 << “ “ << result;


b)
int num1, num2;
char again;
while ((again == ‘y’) || (again == ‘Y’))
cout << “Enter two numbers: “;
cin >> num1 >> num2;
cout << “Their sum is “ << (num1 + num2) << endl;
cout << “Do you want to do this again? “;
cin >> again;


c)
int num, bigNum, power, count;
cout << “Enter an integer: “;
cin >> num;
cout << “What power do you want it raised to? “;
cin >> power;
bigNum = num;
while (count++ < power);
bigNum *= num;
cout << “The result is “ << bigNum << endl;


d)
int numCount, total;
double average;
cout << “How many numbers do you want to average? “;
cin >> numCount;
for (int count = 0; count < numCount; count++)
{
int num;
cout << “Enter a number: “;
cin >> num;
total = total + num;
count = count + 1;
}
average = total / numCount;
cout << “The average is << average << endl;

Explanation / Answer

//a)
int num1 = 0, num2 = 10, result;
// ERROR: num is not declared
int num = num + 1;
// ERROR: lvalue required as increment operand
result = (num1 + num2);
cout << num1 << " " << num2 << " " << result;
// output: 0 10 10


//b)
int num1, num2;
// char again value not declared to be used in while loop
char again = 'y';
// while loop brakcets not given
while ((again == 'y') || (again == 'Y'))
{
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Their sum is " << (num1 + num2) << endl;
cout << "Do you want to do this again? ";
cin >> again;
}
/*
output:
Enter two numbers: 2 3
Their sum is 5
Do you want to do this again? y
Enter two numbers: 4 5
Their sum is 9
Do you want to do this again? n

*/


//c)
// count not initialized
int num, bigNum, power, count = 0;
cout << "Enter an integer: ";
cin >> num;
cout << "What power do you want it raised to? ";
cin >> power;
bigNum = num;
// while loop terminated after one statement because of which it will not iterate
// count iterate two power -1 to get the coorect power
while (count++ < power-1)
bigNum *= num;
cout << "The result is " << bigNum << endl;
/*
output:

Enter an integer: 6
What power do you want it raised to? 3
The result is 216

*/


//d)
// total not initialized to 0
int numCount, total = 0;
double average;
cout << "How many numbers do you want to average? ";
cin >> numCount;
for (int count = 0; count < numCount; count++)
{
int num;
cout << "Enter a number: ";
cin >> num;
total = total + num;
// no need to increment count as it is already incremented in for loop
//count = count + 1;
}
// type cast to double to get the result in double
average = (total*1.0) / numCount;
// terminating charater " is missing
cout << "The average is " << average << endl;

/*
output:
How many numbers do you want to average? 4
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
The average is 2.5
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote