Question

In: Computer Science

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.)

1. for (int j = 8; j > 3; j -= 2)

        cout << setw(5) << j;

    Answer:     

2. int sum = 5;

   for (int k = -4; k <= 1; k++)

        sum = sum + k;

   cout << sum;

Answer:      

3.   for (int k = 2; k < 5; k++)

      {

            for (int j = 6; j < 9; j++)

                  cout << setw(4) << (k + j) << " ";

            cout << endl;

      }

    Answer:    

int sum = 0;

     int a = 3;

     while (a < 6)

     {

           for (int k = a; k < 6; k++)

                 sum = sum + k;

           a = a + 1;

     }

     cout << sum << endl;

     Answer:      

5. int k = 0;

   for (k = 1; k <= 1; k++)                   

   {

     for (int counter = 1; counter <= 1; counter--)

        cout << counter << endl;

     cout << " " << endl;

   }

    cout << k << endl;

Answer:       

int k = 0;

for (k = 10; k < 1; k++)                     

   {

     for (int counter = 1; counter <= 10; counter++)

       cout << setw(5) << counter;

     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 = 6;

    int number = 16;

    number = number + ++answer;

    cout << answer << " " << number;        

    

               

//Answer:         

b. int answer = 16;

   int number = 6;

   number = number + answer--;

   cout << answer << " " << number;

               

          

   //Answer:

          

Solutions

Expert Solution

ANSWER :

1. OUTPUT :

8 6 4

explaination :

Here setw() funtion in used to add width in the outptut. So setw(5) specifies the spaces between the output when it is printed

2. OUTPUT :

-4

explaination : In the for loop k is iterated from -4 to 1 untill condition is met it is incremented.

3. OUTPUT :

8 9 10

9 10 11

10 11 12

explaination : Here there are two for loops first loop runs from 2 to 5 and 2nd loop runs from 6 to 9. In the second for loop there is print statement which returns the width of 4 units and k + j value

4. OUTPUT :

26

explaination : Here there is a while loop where a=3 < 6 loop runs untill condition is met. and inside while loop there is for loop runs from k = a to 6 and returns sum of the loop

5. OUTPUT :

returns garbage value and the loop runs for infinite turns

6. OUTPUT :

10

explaination : Here the for loop is not compiled because the condition in the for loop doesnot satisfy so in the for loop k is assigned to 10 so the final output is 10

a. OUTPUT :

7 23

explaination : Here the answer = 6 and number = 16

number = number + ++answer

here ++answer is a preincrement so first the ++answer is incremented and the following statement is evaluated so solution becomes answer = 7 and number = 23

b. OUTPUT :

15 22

explaination : Here answer = 16 and number = 6

number = number + answer--

whre answer-- is a post drecement so the expression is evaluated first and answer value is decremented. So the solution becomes answer = 15 number = 22


Related Solutions

Find the error in each of the following code segments and explain how to correct it....
Find the error in each of the following code segments and explain how to correct it. x = 1; while ( x <= 10 ); x++; } for ( y = .1; y != 1.0; y += .1 System.out.println( y ); switch ( n ) { case 1: System.out.println( “The number is 1” ); case 2: System.out.println( “The number is 2” ); break; default: System.out.println( “The number is not 1 or 2” ); break; } The following code should print...
Find and correct at least two errors in each of the following segments of code. a)...
Find and correct at least two errors in each of the following segments of code. a) final int ARRAY_SIZE = 5; ARRAY_SIZE = 10; int[] table = new int[ARRAY_SIZE]; for (int x = 0; x <= ARRAY_SIZE; x++) table[x] = 99; b) String[] names = {“Mina” , “George”}; int totalLength = 0; for (int i = 0; i <= names.length() ; i++) totalLength += names[i].length; c) String[] words = “Hello”,“Goodbye”; System.out.println(words.toUpperCase()); d) public class MyClass { private int x; private...
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
C++ Questions What is the output of the following pseudocode code: ages = new List Append(ages,...
C++ Questions What is the output of the following pseudocode code: ages = new List Append(ages, 55) Append(ages, 88) Append(ages, 66) Print(ages) A. 55, 66, 88 B. 55, 88, 66 C. 55, because there is no loop D. 66, because there is no loop E. None of the above. QUESTION 2 Type the list after the given operations. Each question starts with an empty list. Type the list as: 5, 7, 9 Append(list, 3) Append(list, 2) Append(list, 1) Remove(list, 3)...
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
Show what is written by the following segments of code, given that item1, item 2 and...
Show what is written by the following segments of code, given that item1, item 2 and item 3 are int variables. StackType stack; item1 = 1; item2 = 0; item3 = 4; stack.Push(item2); stack.Push(item1); stack.Push(item1 + item3); item2 = stack.Top(); stack.Pop(); stack.Push(item3*item3); stack.Push(item2); stack.Push(3); item1 = stack.Top(); stack.Pop(); cout << item1 << endl <<item2 << endl << item 3 << endl; while (!stack.isEmpty()) { item1 =stack.Top(); stack.Pop(); cout << item1 << endl; }
The language is Assembly Language. Thank you! Assignment 3: Run the following code. Record what each...
The language is Assembly Language. Thank you! Assignment 3: Run the following code. Record what each of the flag changes are for each command. INCLUDE Irvine32.inc .data .code main PROC mov al,255 add al,1 call DumpRegs sub al,1 call DumpRegs sub al,1 call DumpRegs exit main ENDP END main
pleade provide the output and a brief explanation of the following C++ code: double dec1 =...
pleade provide the output and a brief explanation of the following C++ code: double dec1 = 2.5; double dec1 = 3.8; double *p, *q; p = &dec1; *p = dec2 - dec1; q = p; *q = 10.0; *p = 2 * dec1 + (*q); q =&dec2; dec1 = *p + *q; cout<<dec1<<" "<<dec2<< endl; cout<<*p<<" "<<*q<< endl; The following code should output the radius of th ebase, height , volume, and surface area of a cylinder. However, it fails...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT