Question
1. What is the output of the following C++ code? int count = 4;double sum = 0;while(count > 0){ sum = sum + pow(count, 2.0);
1. What is the output of the following C++ code?
int count = 4;double sum = 0;while(count > 0){
sum = sum + pow(count, 2.0);
cout << sum << ' ';
count--;}cout << sum << endl;
Output:
2. Suppose that the input is 4 2 7 13 -1. What is the output of the following code?
int num;int sum;cin >> sum;num = sum;while (num != -1){
cin >> num;
sum = sum + num;}cout << "Sum = " << sum << endl;
Output:
3. Correct the following code so that it reads in 6 numbers and computes their sum:
int count = 0;int sum = 0;cin >> num;while(count <= 6);{
cin >> num;
count++;
sum = sum + count;}
Your code:
4. What is the output of the following program segment?
int count = 5;while(--count > 0)cout << count << " ";cout << endl;
Output:
5. What is the output of the following program segment?
int num = 0;int count;int y = 1;for (count = 0; count < 5; ++count){
num = 2 * (count - 2) + y;
cout << num << " ";}cout << count << " " << endl;
Output:
6. Write a for statement to compute the sum of all the multiples of 3 between 1 and 100 (i.e., 3+6+9+99).
int ___ = ___;
int ___ = ___;for(___;___;___){
___;
}
7. What is the output of the following C++ program segment? Assume all variables are properly declared.
for (j = 0; j < 8; j++){
cout << j * 25 << " - ";
if (j != 7)
cout << (j + 1) * 25 - 1 << endl;
else
cout << (j + 1) * 25 << endl;
}
Output:
8. How many times will each of the following loops execute?
Case one:
int x = 5; int y = 50;
do
x = x + 10;
while (x < y);
Answer:
Case two:
int x = 4; int y = 70;
do
x = x * 2;
while (x < y);
Answer:
Case three:
int x = 3; int y = 10;
do
x = x + 2;
while (x >= y);
Answer:
9. Write a while loop and a do...while loop that both have the same output as the following code segment:
int limit = 5;int total = 1;int j;for(j = 1; j <= limit; j++){
cout << total * j << endl;
total = total 1 + j;
}
cout << endl;
10. To learn how nested for loops work, do a walk-through of the following program segments and determine the exact output in each case.
Case one:
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = (i + 1); j <= 5; j++)
cout << setw(5) << j;
cout << endl;
}
Output:
Case two:
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
cout << setw(3) << j;
cout << endl;
}
Output:
Case three:
const int M = 5;
const int N = 5;
int i, j;
for (i = 1; i <= M; i++)
{
for (j = 1; j <= N; j++)
cout << setw(3) << M * (i - 1) + j;
cout << endl;
}
Output:
Case four:
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= (5 - i); j++)
cout << " ";
for (j = 1; j <= i; j++)
cout << setw(1) << j;
for (j = (i - 1); j >= 1; j--)
cout << setw(1) << j;
cout << endl;
}
Output:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started