next up previous
Next: About this document

centering17

name:

  1. (10 pts) Fill out the questionaire about the textbook distributed with this exam, and turn it in along with your exam when you are finished.
  2. (5 pts) What error does the following code fragment contain?
    int *ptr;
    
      ptr = (double *) malloc(sizeof(double));
  3.   (10 pts) What will the following program print when it is run?
    #include <stdio.h>
    
    typedef struct {
      int first;
      int second;
    } pair;
    
    typedef int intlist[10];
     
    void inc2(intlist il, pair p) {
     
      il[3]++;
      p.first++;
    }
     
    main () {
     
      intlist m;
      pair q;
     
      m[3] = 4;
      q.first = 5;
      inc2(m, q);
      printf("m[3]: %d and q.first: %d.\n", m[3], q.first);
    }
  4. (10 pts) The following program contains at least two errors. What are they? Describe at least two.
    #include <stdio.h>
     
    main () {
     
    FILE *fptr;
    char filename[21];
     
      printf("Enter a filename: ");
      scanf("%s", filename);
      fptr = fopen(filename, "r");
      fprintf(fptr, "this is a sentence\n");
      fclose(fptr);
    }
  5. (5 pts) Suppose that the file foo.c in the current working directory contains the program given in question 3. How many lines of output does the following Unix command produce?
    cat foo.c | grep first
  6. (5 pts) What does the following program print?
    #include <stdio.h>
    #include <stdlib.h>
     
    main() {
    int i;
    int *ptr;
     
      i = 10;   
      ptr = &i;
      *ptr = 7;
      printf("i is: %d\n.", i);
    }
  7. (5 pts) Describe the contents of the file out.txt after the following command is executed. Assume that foo.c is a text file in the current working directory.
    wc foo.c > out.txt
  8. Six students take a five question true-false test. Their answers can be stored in an array of the following type answertype, where 0 is used to represent false, and 1 is used to represent true.

    #define NUMSTUDENTS 6
    #define NUMQUES 5
    
    typedef int answertype[NUMSTUDENTS][NUMQUES];

    The answer key to this exam is stored in an array of type keytype.

    typedef int keytype[NUMQUES];

    1. (5 pts) Declare a variable key of type keytype initialized so that the first three answers are true (1) and the rest are false (0).
    2. (5 pts) Given your declaration of key, what error does the following code fragment contain? (Hint: the answer is NOT that the key for the exam is changed.)
      int i, a;
      
      for (i = 0; i <= NUMQUES; i++) {
        printf("Enter the next answer: ");
        scanf("%d", &a);
        if (a == 0) key[i] = 0;
        else key[i] = 1;
      }
    3. (25 pts) Write a C function to compute exam scores. Your function should have the following prototype:
      void gradeit(answertype, keytype);

      i.e. it takes student answers and the key as arguments. Your function should write exam scores into the output file scores.dat, one line per exam, in the following form:

      Exam: exam-number Score: score

      where each correct answer is worth five points. For example, if the first exam has four answers correct, the first line of the file should be:

      Exam: 0  Score: 20

      You can assume that <stdio.h> has been included, that file scores.dat will open correctly (i.e. you don't need to check for errors when opening the file) and that your function appears in the same file with the previous type and constant declarations.

    4. (15 pts) Write a C function that reads file scores.dat and finds and prints the highest exam score. The user may have edited the file by hand, so you can't assume that the file contains exactly six lines. However, you can assume that all lines in the file have the format given in the previous part of this problem, and you don't need to check for impossible scores. You don't need to check for errors when opening the file, and you can assume that <stdio.h> has been included in the program file.
  9. (10 pts) What does the following program print when it is run?
    #include <stdio.h>
    
    int inc(int x) {
    
      x++;
      return (x);
    }
    
    main() {
      int y = 9;
      int res;
    
      res = inc(y);
      printf("The value and result are: %d %d.\n", y, res);
    }
  10. (5 pts) Where does output from the following code fragment appear?
    FILE *fptr;
    
      fptr = stdout;
      fprintf(fptr, "I'm the output.\n");
  11. (5 pts) What does the following program print when it is run?
    #include <stdio.h>
    
    int x = 7;
    
    void printx(void) {
      printf("x is %d.\n", x);
    }
    
    main() {
      int x = 8;
    
      x++;
      printx();
    }
  12. (5 pts) What does the following program print when it is run?
    #include <stdio.h>
    
    main() {
      char c = 'n';
    
      switch (c) {
        case 'u': printf("u");
        case 'n': printf("n");
        case 'i': printf("i");
                  break;
        case 'x': printf("x");
                  break;
        default:  printf("dos");
                  break;
      }
    }
  13. Given the following type definitions:
    #define NAMELEN 41
    #define LINELEN 81
    
    typedef char linetype[LINELEN];
    typedef struct {
      linetype street;
      linetype cityAndState;
    } addresstype;
    
    typedef struct {
      char name[NAMELEN];
      addresstype address;
      int age;
    } persontype;
    and the following declaration of p:
    persontype p;
    1. (5 pts) Give the call to printf() needed to print the cityAndState associated with p.
    2. (5 pts) Give the call to scanf() needed to read a new age for p from the keyboard.
    3. (5 pts) Give the function call needed to read a new name for p from the keyboard. Make sure that a name can contain multiple words (i.e. can contain spaces and tabs).
    4. (5 pts) What is the type of the expression:
        p.name[10];
  14. (5 pts) Give a Unix command that will show whether or not user elvis is logged into the Unix system kitchen.graceland.com.



next up previous
Next: About this document

Tim Wahls
Tue Feb 18 13:10:22 EST 1997