Complete the following problems. 1. What is the output from the following C++ co
ID: 3627423 • Letter: C
Question
Complete the following problems.1. What is the output from the following C++ code fragment?
int count = 1;
int y = 100;
while(count < 100)
{
y = y - 1;
count++;
}
cout << "y = " << y << " and count = " << count << end;
2. What is the output from the following C++ code fragment?
int num = 1;
while(num < 10)
{
cout << num << " ";
num += 2;
}
cout << end;
3. What is the output from the following C++ code fragment if the following values are the inputs to cin?
38 35 71 14 -10
int sum, num;
cin >> sum;
for(int j = 1; j <= 3; j++)
{
cin >> num;
sum += num;
}
cout << "Sum = " << sum << endl;
4. What is the output from the following C++ code fragment if the following values are the inputs to cin?
38 35 71 14 -1
int sum, num;
sum = 0;
cin >> num;
while(num != -1)
{
sum += num;
cin >> num;
}
cout << "Sum = " << sum << endl;
5. Write a do - while loop to get 20 numbers from the user and sum them together. Output the sum after the loop. Declare any variables you need.
6. What is the output from the following C++ code fragment if the following values are the inputs to cin?
58 23 75 176 145 -999
int num;
cin >> num;
while(num != -999)
{
cout << num % 25 << " ";
cin >> num;
}
cout << endl;
7. What is the output from the following C++ code fragment?
int count = 10;
while(count-- > 0)
{
cout << count << " ";
}
cout << endl;
8. What is the output from the following C++ code fragment?
int count = 1;
do
{
cout << count * (count - 1) << " ";
}while(++count <=5);
cout << endl;
9. Given the following C++ code fragment, answer the questions that follow.
int s = 0;
for(int i = 0; i < 5; i++)
{
s = 2 * s + i;
}
a. What is the final value of s?
b. If the expression i++ was replaced with i += 2 what would be the final value of s?
c. If a semi-colon is added following the right parenthesis of the for statement, what would be the final value of s?
10. Write a for statement to add all the multiples of three between 1 and 100. (ie. 3, 6, 9, … 99).
11. How many times will the loop bodies execute in the following loops?
a. int x = 5, y = 50;
do
{
x += 10;
}while(x < y);
b. int x = 25, y = 5;
while(x >= y)
{
x -= 5;
}
c. int y = 0;
for(int x = 5; x < 100; x += 5)
{
y++;
}
d. int x = 10, y = 1000;
while(x <= y);
{
x *= 10;
}
Explanation / Answer
please rate - thanks
1. What is the output from the following C++ code fragment?
int count = 1;
int y = 100;
while(count < 100) //will loop count = 1-99 axits when count=100
{
y = y - 1; //so goes through loop 99 times y=100-99=1
count++;
}
cout << "y = " << y << " and count = " << count << endl;
y=1 and count = 100
2. What is the output from the following C++ code fragment?
int num = 1;
while(num < 10)
{
cout << num << " "; //loop 1 to 9 adding 2 each time
num += 2;
}
cout << endl;
1 3 5 7 9
3. What is the output from the following C++ code fragment if the following values are the inputs to cin?
38 35 71 14 -10
int sum, num;
cin >> sum; //enters 1st number into sum
for(int j = 1; j <= 3; j++) //does the loop 3 times
{
cin >> num; //enters numbes 2-4 numbers and add them
sum += num;
}
cout << "Sum = " << sum << endl; //so adding 1st 4 numbers
sum = 158
4. What is the output from the following C++ code fragment if the following values are the inputs to cin?
38 35 71 14 -1
int sum, num;
sum = 0;
cin >> num; //same as above but stops becase -1 entered
while(num != -1)
{
sum += num;
cin >> num;
}
cout << "Sum = " << sum << endl;
sum = 158
5. Write a do - while loop to get 20 numbers from the user and sum them together. Output the sum after the loop. Declare any variables you need.
int sum, num,i;
sum = 0;
i=0;
while(i<20)
{cin >> num;
sum += num;
i++;
}
cout << "Sum = " << sum << endl;
6. What is the output from the following C++ code fragment if the following values are the inputs to cin?
58 23 75 176 145 -999
int num;
cin >> num;
while(num != -999)
{
cout << num % 25 << " "; //output the remanider of num/25
cin >> num;
}
cout << endl;
8 23 0 16 20
7. What is the output from the following C++ code fragment?
int count = 10;
while(count-- > 0) //subtract 1 from count until gets to 0
{
cout << count << " "; //and output it
}
cout << endl;
9 8 7 6 5 4 3 2 1 0
8. What is the output from the following C++ code fragment?
int count = 1;
do
{
cout << count * (count - 1) << " ";
}while(++count <=5);
cout << endl;
0 2 6 12 20
9. Given the following C++ code fragment, answer the questions that follow.
int s = 0;
for(int i = 0; i < 5; i++)
{
s = 2 * s + i;
}
a. What is the final value of s? 26
b. If the expression i++ was replaced with i += 2 what would be the final value of s? 8
c. If a semi-colon is added following the right parenthesis of the for statement, what would be the final value of s? 5
10. Write a for statement to add all the multiples of three between 1 and 100. (ie. 3, 6, 9, … 99).
sum=0;
for(i=1;i<=100;i++)
if(i%3==0)
sum+=i;
11. How many times will the loop bodies execute in the following loops?
a. int x = 5, y = 50;
do
{
x += 10;
}while(x < y);
when x=5,15,25,35,45 5 times
b. int x = 25, y = 5;
while(x >= y)
{
x -= 5;
}
when x=25,20,15,10,5 5 times
c. int y = 0;
for(int x = 5; x < 100; x += 5)
{
y++;
}
when x=5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95 19 times
d. int x = 10, y = 1000;
while(x <= y);
{
x *= 10;
}
when x=10,100,1000 3 times
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.