Consider the following C++ function, where variable A is an array of length n, which is a positive integer. int test(int n, int* A){ int x=0; for( int i=0; i1)? n* factorial(n-1) : 1; } e. int factorial(int n){ return (n>1)? factorial(n)* factorial(n-1) : 1; } Consider the following program segment, where a is an integer constant and b is an integer variable that holds a positive value. int r = 0; int n = b; while (n != 0){ r += a; n--; } Which of the following is a loop invariant for the while loop? Question options: a. r = a(b-1) b. r= b(a-1) c. r = a + b d. r = ab - 1 e. r = ab
Consider the following C++ function, where variable A is an array of length n, which is a positive integer.
int test(int n, int* A){
int x=0;
for( int i=0; i<n; i++){
if (i%2==1){ x += A[i];}
}
return x;
}
What value does test function return?
|
a. The sum of the values in the array A |
|
b. The largest odd value in the array A |
|
c. The number of odd values in the array A |
|
d. The sum of the odd values in the array A |
|
e. The sum of the values in odd-numbered index in the array A |
Which of following recursive C++ functions correctly computes n! (the factorial of n, assuming n is a non-negative integer) ?
|
a. int factorial(int n){ return factorial(n)* factorial(n-1); } |
|
b. int factorial(int n){ return n* factorial(n-1); } |
|
c. int factorial(int n){ return factorial(n)* (n-1); } |
|
d. int factorial(int n){ return (n>1)? n* factorial(n-1) : 1; } |
|
e. int factorial(int n){ return (n>1)? factorial(n)* factorial(n-1) : 1; } |
Consider the following program segment, where a is an integer constant and b is an integer variable
that holds a positive value.
int r = 0;
int n = b;
while (n != 0){ r += a; n--; }
Which of the following is a loop invariant for the while loop?
|
a. r = a(b-1) |
|
b. r= b(a-1) |
|
c. r = a + b |
|
d. r = ab - 1 |
|
e. r = ab |
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images