What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.)
1. for (int j = 25; j > 16; j -= 3)
cout << setw(5) << j;
Answer:
2. int sum = 0;
for (int k = -2; k <= 2; k++)
sum = sum + k;
cout << sum;
Answer:
for (int k = 2; k < 5; k++)
{
for (int j = 3; j < 6; j++)
cout << setw(4) << (k + j) << " ";
cout << endl;
}
Answer:
int sum = 0;
int a = 6;
while(a < 9)
{
for (int k = a; k < 8; k++)
sum = sum + k;
a = a + 1;
}
cout << sum << endl;
Answer:
int k = 0;
for (k = 4; k < 1; k++)
{
for (int counter = 1; counter <=10; counter++)
cout << setw(5) << counter;
cout << endl;
}
cout << k << endl;
Answer:
6. int k = 0;
for (k = 1; k <= 10; k++)
{
for (int counter = 1; counter <= 10; counter--)
cout << counter << endl;
cout << " " << endl;
}
cout << k << endl;
Answer:
Assuming all variables have been properly declared and all source code has been properly setup in a compiler, what is the output from each of the following fragments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. (2 points each)
a. int answer = 5;
int number = 8;
number = number + ++answer;
cout << answer << " " << number;
//Answer:
b. int answer = 5;
int number = 8;
number = number + answer--;
cout << answer << " " << number;
//Answer:
|
1. for (int j = 25; j > 16; j -= 3) cout << setw(5) << j; Answer: Explanation Prints number in the width of 5 |
|
2. int sum = 0; for (int k = -2; k <= 2; k++) sum = sum + k; cout << sum; Answer: 0 Explanation -2 -1 + 0 + 1 + 2 = 0 |
|
for (int k = 2; k < 5; k++) { for (int j = 3; j < 6; j++) cout << setw(4) << (k + j) << " "; cout << endl; } Answer:
|
|
int sum = 0; int a = 6; while(a < 9) { for (int k = a; k < 8; k++) sum = sum + k; a = a + 1; }
cout << sum << endl; Answer: 20 |
// Note: 4 sub parts at a time please -- Policy of Chegg
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) for (int k = 2; k < 5; k++) { for (int j = 3; j < 6; j++) cout << setw(4) << (k + j) << " "; cout << endl; }
A. What is the output of the following C++ code fragment? (all variables are of type int) int count-1; int y-100; while (count 3) y=y-1 ; count+t cout << y << endl; cout<< count <<endl What is the value of x after control leaves the following while loop? (all variables are of type int) B. int x0 while (x < 20) sum- sum+x cout << sum<< endl;
points): Show the output of the code below as it would appear on the monitor. int main cout <<" <<endl: int wildcat 2: while (wildcat > 5) cout << wildcat <<endl; wildcat++ cout <K*<< endl; int jayhavk 5i do cout << jayhawk <s endl: while (jayhawk0) cout <<*" << endl; int wolverine l: while (wolverine 0 &&wolverine < 10) cout << wolverine <<endl: wolverine2: cout <<" <<endl: for (int zag-4; zag>0; zag_) cout <<****" << endl; for (int i-10; i<-30;...
2. What is the output of the following code fragment? n = 1; while (n <= 5) { n++; cout << n << ' '; a.1 2 3 4 5 b. 1 2 3 4 c. 1 1 1 forever d. 2 3 4 5 e. 2 3 4 5 6 3. What is the termination condition for the following While loop? while (beta > 0 && beta < 10) { cout << beta << endl; cin >> beta; }...
Class: C++ Final Exam Dates Multiple Choice Identify the choice thar best completes the statement or answer the question N 1. In s tructures, the computer repeats particular statements a certain number of times depending on some condition(s) a. looping C. selection b. branching d. sequence 2. What is the output of the following CH code? count = 1; num - 25; while (count < 25) numnum - 1: count++; cout << count << " " << num << endl;...
What is the output of the following code snippet? (If there is some kind of syntax error indicate this by marking "error" in your answer choice) 1. int laps = 8; if (laps++ > --laps) laps += 2; else if (--laps > (laps - 1)) laps += 4; else if (++laps) laps -= 3; cout << laps << endl; 2. What is the output of the following code snippet? int j = 47; int i = 8; j =...
12. What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; *p = 35; *q = 98; *p = *q; cout << x << " " << y << endl; cout << *p << " " << *q << endl; 13. What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; x = 35; y = 46; p...
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;...
1. (TCO 1) What is the output of the following C++ code? int list[5] = {0, 5, 10, 15, 20}; int j; for (j = 0; j < 5; j++) cout << list[j]; (Points : 4) 0 1 2 3 4 0 5 10 15 0 5 10 15 20 5 10 15 20 Question 2.2. (TCO 1) What is stored in alpha after the following code executes? int alpha[5] = {0}; int j; for (j = 0; j...
Consider the following code segment, what is the output? //you please explain your answer with all the steps?? int j, k; for (k=0;k<10; k++) { for (j=0; j<5;j++) cout << "*"; } cout << endl;