Assessment 3 on Functions and Program Running Time
1. What does the code segment below print?
int fun(int x)
{
++x;
}
int main()
{
int x=1;
fun(x);
printf(“%d”,x);
return 0;
}
Ans: 1
1 point
2. What is the output of the following code snippet?
int fun(int x, int y, int z) { y=y+4; z=x+y+z; } int main() { int x=10; int y=3; fun(y,x,x); y=x+2; fun(x,y,y); x=y+2; printf(“%d,%d”,x,y); return 0; }
Ans:14,12
1 point
3. What is the output of the following code snippet?
int fun(int *x, int *y, int *z) { *y=*y+4; *z=*x+*y+*z; } int main() { int x=10; int y=3; fun(&y,&x,&x); y=x+2; fun(&x,&y,&y); x=y+2; printf(“%d,%d”,x,y); return 0; }
Ans:107,105
1 point
4. What does the below recursive function do ?
int * func(int *arr, int n){ if(n==1 || n==0) return arr; else{ int temp=arr[0]; arr[0]=arr[n-1]; arr[n-1]=temp; return func(++arr,n-2); } }
Reverse the given array.
1 point
5. Let S(n) denote the sum of first n natural numbers. Specify which of the following statements are correct.
S(n)=O(n^2) & S(n)=O(n^3) )
1 point
6. Let S(n) denote the sum of squares of first n natural numbers. Specify which of the following statements are correct.
1 point
7. Consider the following segment of code:
for(i=0;i<n/2;i++) { . . . }Let the body of for loop take O(n^3) time. Let S(n) represent the total running time of the whole segment. Specify which of the following statements are correct.
1 point
8. Consider the following code segment. Assume n to be a positive integer.
for(i=1; i<=n; i++) { for(j=1; j<=n; j=j+i) { printf(“Hi”); } }Let T(n) represent the total running time of the above code segment. Specify which of the following statements are correct.
1 point
9. Let the function f(n) = n + 2n + 3n+ …+ (n – 1 )*n. Assume n to be a positive integer. Specify whether the following statement is true or false. f(n) = O(n^2)
1 point
10. Specify whether the following statement is true or false. If a function T(n) = O(n^i), then T(n) = O(n^j) whenever i < j and i, j are positive integers.
No comments:
Post a Comment