Practice Midterm

Important Note

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.

I: Produce Output

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.

I.1: Output-1

#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

I.2: Output-2

#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

I.3: Output-3

#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

II: Write a C Program

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).

II.1: Swapping Various Data Types

II.1A Swapping Integers

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.

II.1B Swapping Strings

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

II.2: Inspect and Report

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.

II.3: Double Space at Sentence End

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.

II.4: Reversing Arguments

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 a4
  
would produce
    a4 a3 a2 a1
  

III: What is Wrong?

III.1: Doubling Arguments

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;
    }
  

III.2: Limiting x to a Small Range

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;
  

III.3: Finding the Max

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