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 |
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)