- True or False. Write 'T' for True and 'F' for False for each of
the following statements:
- A recursive function cannot include a for loop.
Answer:
F
- The fopen function returns 0 if the file does not exist and
the second argument to fopen is "r".
Answer:
T
- The first argument to fscanf is a format string.
Answer:
F
- Using "bubble sort" to sort n items takes a number of
comparisons proportional to log n.
Answer:
F
- Binary search is strictly superior to linear search; there is no
reason why we would ever need to use linear search except possibly because
the code is easier to understand.
Answer:
F
- The index of the last element of an array of 100 integers is 100.
Answer:
F
- Strings are represented in C by arrays of null-terminated characters.
Answer:
T
- main208 is the only computer you are allowed to use for this class.
Answer:
F
- The first element of the argv array in main is the
name of your program.
Answer:
T
- fclose is a function that performs a "float conversion long
operating system execute."
Answer:
F
-
What is the output of the following program?
#include <stdio.h>
void foo (int n) {
if (n != 0) {
foo (n-1);
printf ("%d\n", n);
}
}
int main () {
foo (10);
return 0;
}
Answer:
1
2
3
4
5
6
7
8
9
10
- What is the output of the following program?
#include <stdio.h>
double f (double x) {
return x * x;
}
int main () {
double dx = 0.000001;
double x, sum;
sum = 0.0;
for (x=0; x<=3; x+=dx) {
sum = sum + f(x) * dx;
}
printf ("%0.3f\n", sum);
return 0;
}
Answer:
9.000
- Write a C program to print the odd numbers between 1 and 99 inclusive.
Your program should be correct and should not take more than 8 lines of
C code.
Possible answers:
/* this version checks for oddness */
#include <stdio.h>
int main () {
int i;
for (i=1; i<=99; i++)
if (i % 2 == 1) printf ("%d\n", i);
return 0;
}
/* this version only computes odd numbers */
#include <stdio.h>
int main () {
int i;
for (i=1; i<=99; i+=2) printf ("%d\n", i);
return 0;
}
-
Write a C function called find_min that accepts two parameters:
an array of integers v and an integer n. The function
should return the index of the smallest element of the array.
int find_min (int v[], int n) {
int i, mini;
mini = 0;
for (i=1; i<n; i++) if (v[i] < v[mini]) mini = i;
return mini;
}
- Write a C function called prime that accepts a single integer
argument n. The function should return 1 if n is prime,
or 0 if n is not prime.
int prime (int n) {
int i;
if (n < 2) return 0;
for (i=2; i<n; i++) if (n % i == 0) return 0;
return 1;
}