6. Given the following function definition void calc (int a, int &b) { int c; c
ID: 3837510 • Letter: 6
Question
6. Given the following function definition
void calc (int a, int &b)
{
int c;
c = a + 5;
a = a * 2;
b = c + a + 2;
}
What is the output of the following code fragment that invokes calc? (3 points)
int x = 10;
int y = 3;
int z = 15;
calc (x, y); cout << x << “ “ << y << “ “ << z << endl;
7. a What is the output from the following code segment? 2.5 points
b. If there is no output, a runtime error, compile error or an infinite loop, please identify and explain. 2.5 points
int point = 1;
while ( point ! = 13)
{
cout << point << “, ”; point +=3; }
8.Evaluate the following expressions, assume the following declarations:
int x = 4;
int y = 9 / 2;
int num = 6;
num *= x + y; What is the value of the num after expression is evaluated?
9.Evaluate the following expression: (y > 35), where y (data-type int) has the value ……
10. What will be the following code display?
int num = 0; int a = 0, b = 0, c = 0, d = 0, x = 0; if (num > 1 && num <= 5) { a = a + 1; } else if (num == 8) { b = b + 1; x = 8; } else if (num > 8 && num < 10) { c = c * 2; } else { d += 5; }
11.Rewrite the following conditional expressions as if/else statements: cout << (( (x % 2) == 0 ...
12.What is the output of the following code fragment? int x, y; x = 10; y = 2; while (x > y) { y ++; x -= 2; cout << “x = “ << x <<” ” << “y = “ << y << endl; }
13. Indicate if the following condition is True or False Assume the variables x=5, y=6 and z=8. 7 <= x && z > 4 True False
14. Write a function prototype and a function definition called sum that receives an array of type integer, and the number of elements in the array. It returns the sum of the numbers in the array.
15.
15.Determine all the output from the following segment of program as it would appear on the screen.
a. Lines 1, 2, and 3 are called function ____________. The function __________ are in lines 17-31.
b. Explain what int =5 means in line 2.
c. Explain the significance of the & in line 3. Explain what the word static means in line 23.
d. Explain the difference between line 10 and line 13.
int f_1();
void f_2(int = 3);
float f_3(int &);
int main()
{
int x = 10, y;
float z; y = f_1();
cout << "y: " << y << endl;
f_2(); z = f_3(x);
cout << "z: " << z << endl;
f_2(x); cout << "x: " << x << endl;
return 0;
}
int f_1()
{
int x = 12; x += 8; return x; }
void f_2(int y) { static int x = 6; x -= y; cout << "x:" << x * x << endl;
}
float f_3(int & x) { x += 3;
cout << "x:" << x << endl;
return 103.3333;
}
16. Using a looping structure, prompt user for negative integer values from the keyboard. Ignore all positive values and terminate the loop once a value 0 is entered. After the loop ends, print the total of the read integers. Example:
17. Indicate if the following expression is True or False Assume the variables int a = - 3, b = 6, c = 6; a >= -1 || c <= (b + a / 2);
18. Evaluating Sorts Given the following array: 41, 32, 5, 8, 7, 50, 11 Show what the array looks like after the first swap of a Selection Sort in ascending order.
19. Evaluate the following expression (y>35), where y (data type int) has the value 15
20. Write a function called expensive books which receive a double array of book prices and the number of books
21. Evaluate the following expression (3% 5-7/6 )
Explanation / Answer
Solution:
6. Output:
10 37 15
Explanation:
calc (x, y); - we call calc with two arguments x and y
In function definition:
void calc (int a, int &b) // x is accepted as a and y's address is accepted (referenece of y is passed)
{
int c;
c = a + 5; // here value of c is c = 10 + 5 =15
a = a * 2; // here a = a * 2 = 10 * 2 = 20 ( no change reflected in x, as x is passed by vlaue and not as reference)
b = c + a + 2; // here b = c + a + 2 = 15 + 20 + 2 = 37 ( Value of y is changed to 37, as it was passed as reference(i.e. address of y is accepted)
}
Thus we get the output:
10 37 15
7.a. The output is:
1, 4, 7, 10,
Explanation:
int point = 1; // point initialized to 1
while ( point != 13) // Loops executed as long as point != 13
{
cout << point << ", "; // prints 1
point +=3; }
point +=3 - here point gets incremented by 3 thus point =4
while ( point != 13) - condition is true
cout << point << ", "; - prints 4
point +=3 - here point gets incremented by 3 thus point =7
while ( point != 13) - condition is true
cout << point << ", "; - prints 7
point +=3 - here point gets incremented by 3 thus point =10
while ( point != 13) - condition is true
cout << point << ", "; - prints 10
point +=3 - here point gets incremented by 3 thus point =13
while ( point != 13) - condition is false, so loop stops and we get the output:
1, 4, 7, 10,
b. No error at all, code gets executed with proper output.
8. Given,
int x = 4;
int y = 9 / 2;
int num = 6;
num *= x + y;
The value of num = 48
Explanation:
x= 4,
y = 9/2 = 4 ( as y as integer variable , it ignores the fractional part of the division)
Then , num is initialized to 6,
the expression num *= x + y, evaluates to,
num = num * (x + y) = 6 *( 4 + 4) = 6 * 8 = 48
9. (y > 35), is evaluated as false, because y will be initialized to 0.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.