I did not consider length when producing this practice midterm. I worry a great deal about length when I write real exams. For example, as I (or one of the TAs) comes up with a new question or question type, I just add it. For real exams, I know that when you add something you need to take something else out.
Treat each of the following problems independently. For each one assume the file is compiled with the executable named a.out and then executed as indicated in the problem.
#include <stdio.h> int main (int argc, char *argv[argc]) { while (--argc >=0) printf ("output-1 %s : %c\n", argv[argc], (argc==1) ? 'x' : 'y'); } ./a.out a b c d
#include <stdio.h> int main(int argc, char *argv[argc]) { int n; char *s; while ((s = *++argv)) { printf ("%s\n", s); n = 0; while (*(s++)) n++; printf("%d\n", n); } } ./a.out midterm exam cso 2009-10 spring
#include <stdio.h> int main(int argc, char *argv[argc]) { int i, n; int A[] = {1, 2, 3, 4}; int B[] = {4, 3, 2, 1}; n = (sizeof A) / (sizeof A[0]); for (i=0; i<n; i++) printf("%c", (A[i]>B[i]) ? '>' : '<'); printf ("\n"); } ./a.out
Treat each of the following problems independently. For each one write a C program meeting the specification. If the program is called a command, you are to write a main() program (and possibly other functions). If it is not called a command, some other main program will call your function(s).
Write a function void swapInt(int *A, int *B); that swaps the values in two integer variables. Upon return, A should be the integer formerly in B and vice versa.
Write a function void swapStr(char **A, char **B); that swaps the values in the two strings. Upon return, A should be the string formerly in B and vice versa.
Do not use any library functions
The following ins-rep.h file is available in the current directory.
struct bookType { int ID; char *book; // the book itself might be huge // other large components (reviews, etc) };
Write two functions
void inspect(struct bookType *); char *report(void);
inspect() is called multiple times and finally report() is called once.
report() is to return the char *book pointing to the book with the largest ID that inspect() has seen.
Since the books are of unknown and presumed huge size, you cannot
copy an entire book.
Similarly, since the other large components
are of unknown
and presumed huge size, you cannot copy
a struct bookType either.
(A comment that would not appear on the real exam
is that pointers to huge objects are small and hence may be copied.)
You may assume all ID's are unique and that inspect() is called at least once.
Write a very primitive version of a program that copies standard input to standard output leaving an extra space after a sentence. This primitive version should just add a blank after each period.
Write a program that prints out its arguments in reverse order. So, if the executable was a.out, then the command
./a.out a1 a2 a3 a4would produce
a4 a3 a2 a1
The following function is supposed to double its arguments. Why doesn't it work? Fix it.
void doubleIt(int x, float y) { x *= 2; y *= 2.0; return; }
The following code snippet is supposed to raise negative values to 0 and lower large values to 10. Why doesn't it work? Fix it.
if (x >= 0) if (x > 10) x = 10; else x = 0;
The following function is supposed to return the max an array of n integers. Why doesn't it work? Fix it.
int max (int n, int A[n]) { int m = 0; int i; for (i=0; i<n; i++) if (A[i] > m) m = A[i]; return m; }Allan Gottlieb