Last updated Unknown
Status:
Tags: #archivedCards/cmpt125/recursion
Links: CMPT 125 Practice Problems
CMPT 125 Recursion Practice Problems
Piazza
Questions
1- “3 2 1”, while loop from n to 1
2-
“1 2 3”, while loop from 1 to n+1
3- Prints for n=3?
1
2
3
4
5
6
7
8
9
|
int foo(int n) {
if (n == 0)
printf("0 ");
else {
printf("%d ", n);
foo(n-1);
printf("%d ", n);
}
}
|
?
“3 2 1 0 1 2 3”, yeah
4- Functionality? Prints on 3?
1
2
3
4
5
|
long foo(int n) {
if (n==0) return 1;
else return foo(n-1)+foo(n-1);
}
}
|
?
3, 2 2, 1 1 1 1, 0 0 0 0 0 0 0 0, 2n
5- Functionality, n=3?
1
2
3
4
5
6
7
8
|
void foo(int n) {
if (n == 0)
printf("*");
else {
foo(n-1);
foo(n-1);
}
}
|
?
Prints 2k amount of asterisks, n=3 equals ********
6-
1
2
3
4
5
6
7
8
9
|
int foo1(int n) {
if (n <= 1)
return n*2;
int half = n/2; // round down if n is odd
int n1 = foo1(half);
int n2 = foo1(n-half);
return (n1+n2);
}
|
?
Returns n*n
1
2
3
|
void foo1(int n) {
return n*n;
}
|
7-
Backlinks
References:
Created:: 2021-10-22 22:08