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

1. Determine all the output from the following segment of program as it would ap

ID: 3557116 • Letter: 1

Question

1. Determine all the output from the following segment of program as it would appear on the screen.

2. Explain each line of output

int a ( );

void b (int = 5);

float c (int&);

int main()

{

int y, x = 2;

float z;

y=a();

cout << x << " "<< y << endl;

b();

z = c(x);

cout << x << " "<< y << " " << z <<endl;

b(x);

cout << x << " " << y << endl;

return 0;

}

int a ( )

{

int x = 1;

x += 3;

return x;

}

void b(int y)

{

static int x = 4;

x = x + y;

cout << x << endl;

}

float c(int & x)

{

x *= 2;

cout << x << endl;

return 3.0;

}

2) Evaluate the following expression: (x > 25), where x (data-type int) has the value 25

3) Write a loop to read integers from the keyboard. When a negative integer is entered, the loop ends.

After the loop ends, print the total of the non-negative integers.

Example:

Please enter an integer: 4

Please enter an integer: 8

Please enter an integer: -3

The total is 12

4)Evaluate the following expression.(25 % 3)

5) Evaluate the following expression.(3 % 5) Blank 1

6) Evaluate the following expressions, assume

int a = 3, b = 4, c = 6;

a >= -1 || a <= b

True or False.

7) Evaluate the following expressions, assume the following declaration:

int a = 2;

What is displayed by the following statement:

cout << a++; Blank 1

8) Evaluate the following expressions, assume

int a=3, b=4, c=6;

!(a > 3)

True or False.

9) Local variables declared inside a function will retain their value between function calls if they are declared as

10) Rewrite this else- if code segment into a switch statement

if (num >= 1 && num < 5)

a = a - 1;

else if (num == 7)

{

b = b + 1;

x = 6;

}

else if (num > 8 && num <= 11)

c = c - 1;

else

d = d + 1;

11) Evaluate the following expressions, assume

int a = 3, b = 4, c = 6;

a >= -1 || a <= b

True or False.

12)

Evaluating Sorts Given the following array:

40, 12, 5, 8, 3, 45, 10

Show what the array looks like after the first swap of a Selection Sort in ascending order.

13) Evaluate the following expressions: (20.0 * (9/5.0)) + 12.0

14) What is the value of the num _____ after expression is evaluated?

15) In order for a binary search to work correctly, the values in the array must be

16)

Evaluating Output.

What is the output of the following code fragment?

Explain why you get this output?

If there is no output, a runtime error, compile error or an infinite loop, please identify and explain.

int x, y;

x = -1;

y = 0;

while (x < 3)

{

y += 2;

x += 1;

}

cout <<

Explanation / Answer

1)

#include "stdafx.h"

#include<iostream>

using namespace std;

int a ( );

//method declaration for a

void b (int = 5);

//method declaration for b with integer 5 as parameter

float c (int&);

//method declaration for c with address value as parameter(pass by reference)

int main()

//program execution begins here

{

       int y, x = 2;

       //integer variables dclaration and initialization of x to 2

       float z;

       //floating point variable declaration

       y=a();

       //call the method a and store the return value in y

       cout << x << " "<< y << endl;//output line 1

       //display x and y

       b();

       //function call to method b

       z = c(x);

       //call the method c and store the return value in z

       cout << x << " "<< y << " " << z <<endl;//output line 4

       //display x,y and z

       b(x);

       //call the method b

       cout << x << " " << y << endl;//output line 6

       //display the values of x and y

       system("pause");

       //return 0 to main method

       return 0;

}

//function definition for method a

int a ( )

{

int x = 1;

//declare and iniialize variable x to 1

x += 3;

//increment z by 3

return x;

//retun the value of x to calling method

}

//function definition for method b with 5 as parameter as declared

void b(int y)

{

//declare and initialize x to 4

static int x = 4;

//add the value of y(5) to x

x = x + y;

//display the final value of x and move curson to next line

cout << x << endl;//output line 2 and 5

}

//function definition for method c with x as parameter called by reference

float c(int & x)

{

//double the value of x

//here x is 4 since i is decalred as static in method b

x *= 2;

//display the final value of x

cout << x << endl;//output line 3

//return 3.0 to calling method

return 3.0;

}

Output:

2 4

//displayed as a result of output line 1

9

//displayed as a result of output line 2

4

//displayed as a result of output line 3

4 4 3

//displayed as a result of output line 4

13

//Displayed as a result of output line 5

4 4

// Displayed as a result of output line 6

Press any key to continue . . .

2)

(x > 25), where x (data-type int) has the value 25

False

3)

do

{

cin>>x[i];

}while(x>0);

3)

#include "stdafx.h"

#include<iostream>

using namespace std;

int main()

{

       int x;

       int tot=0;

       int i=0;

       do

       {

       cout<<"Please enter an integer:";

       cin>>x;

       if(x>0)

              tot+=x;

       }while(x>0);

       cout<<"Total is: "<<tot;

       system("pause");

       return 0;

}

Output:

Please enter an integer:4

Please enter an integer:8

Please enter an integer:-3

Total is: 12Press any key to continue . . .

4)

25%3=1

Since the calculation leads to a value greater than 1, the answer is TRUE

5)

3%5=3

Since the calculation leads to a value greater than 1, the answer is TRUE

6)

int a = 3, b = 4, c = 6;

a >= -1 || a <= b

3>-1 || 3<4

Both are true

So, True

7)

cout << a++;

Ans: 2

Reason: a is incremented after printing

8)

int a=3, b=4, c=6;

!(a > 3)

Since a is equal to 3, the statement that it a is not greater than 3 is true

Ans: True

9) Static