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) Give the UNIX command needed to copy the file example.c from the examples directory of user jill27 to your current working directory.

    cp ~jill27/examples/example.c .
    

  3. (10 pts) Describe at least two situations in which the following program will fail (crash or do something unexpected).
    #include <stdio.h>
    #define MAX 21
    
    main () {
    
    FILE *infile;
    char word[MAX];
    
      infile = fopen("in.dat", "r");
    
      fscanf(infile, "%s", word);
      printf("%s", word);
      fclose(infile);
    }
    

    If the file in.dat does not exist.

    If the user types in a filename that is more than 20 characters in length.

  4. (5 pts) Give the UNIX command needed to run the executable a.out in such a way that it treats the contents of the file in.dat as if they were being typed in from the keyboard.

    a.out < in.dat
    

  5. (5 pts) In the following program, which method of reading a sentence from the keyboard is preferred and why?

    #include <stdio.h>
    #define SSIZE 81
    
    main () {
    
      char sentence[SSIZE];
                    
      printf("Enter a sentence : ");
      /* method one */
      gets(sentence);
      /* method two */
      fgets(sentence, SSIZE, stdin);
    }
    

    Using fgets is preferred because it won't write outside the bounds of the string sentence if the user types in a sentence of more than 80 characters.

  6. (5 pts) What will the following program print when it is run?
    #include <stdio.h>
    
    void inc2(int i, int *j) {
    
      i++;
      *j++;
    }
    
    main () {
    
      int a = 5, b = 10;
    
      inc2(a, &b);
      printf("a: %d and b: %d.\n", a, b);
    }
    

    This program prints:

    a: 5 and b: 11.
    

  7. (10 pts) In the following code fragment:
    int *arr;
                          
      arr = (int *) malloc(10 * sizeof(int));
    
      arr[3] = 5;
    Why is it legal to refer to arr as if it were declared as an array?

    Because the C compiler represents arrays internally as a pointer of type address of the base type of the array. So, this declaration of arr is the same as declaring arr as an array of ints.

  8. (5 pts) In the following code fragment:
    int *ptr;
    
      ptr = (int *) malloc(sizeof(int));
      
    Why is it necessary to put (int *) before the call to malloc?

    malloc() returns a pointer to void, so this result must be cast (converted) to a pointer to int to be assigned to ptr so that the types match.

  9. (15 pts) Given the definition:
    #define ROWS 5
    #define COLS 10
    
    typedef int table[ROWS][COLS];
    
    Write a C function that takes a parameter of type table and returns the sum of all of the integers contained in the table.

    int sum(table t) {
      /* return the sum of all integers in the table */
    
      int tsum = 0;
      int row, col;
    
      for (row = 0; row < ROWS; row++) {
        for (col = 0; col < COLS; col++) {
          tsum += t[row][col];
        }
      }
      return (tsum);
    }
    

  10. Given the definitions:
    #define SIZE 15
    
    typedef struct {
      double x;
      double y;
    } point;
    
    typedef point pointlist[SIZE];
    
    1. (10 pts) Write a C function that takes a parameter of type pointlist and initializes both fields of each struct in the parameter to be 0.0.

      void initpoints(pointlist p) {
        /* initialize an array of points */
      
        int count;
      
        for (count = 0; count < SIZE; count++) {
          p[count].x = 0.0;
          p[count].y = 0.0;
        }
      }
      

    2. (15 pts) Write a C function that takes a parameter of type pointlist and writes the contents of that parameter to file out.dat, one point per line in format: (x, y). That is, after your function has been called, each line of out.dat will contain the x and y fields of a point in parenthesis, separated by a comma. You can assume that stdio.h has been included and that your function will be able to open file out.dat correctly.

      void tofile(pointlist p) {
        /* write the contents of an array of points
           to out.dat */
      
        int count;
        FILE *fptr;
      
        fptr = fopen("out.dat", "w");
        for (count = 0; count < SIZE; count++) {
          fprintf(fptr, "(%lf, %lf)\n", p[count].x, p[count].y);
        }
      }
      

    3. (5 pts) What error does the following function definition contain?
      void incpoint(point *apoint) {
      /* this function updates both fields of a point by 
         incrementing both by 1.0 */
      
        apoint.x += 1.0;
        apoint.y += 1.0;
      }
      

      Parameter apoint is an address of a struct, not a struct. So, fields have to be accessed using -> and not the . notation.

    4. (5 pts) Given the declaration:
      pointlist somepoints;
      Write the call to scanf that would be needed to read a new value into the x field of the third point in somepoints from the keyboard.

        scanf("%lf", &somepoints[2].x);
      

    5. (5 pts) Given the declaration:
      point apoint;
      What is the type of the expression:
      apoint.x + apoint.y * 3;

      Its type is double.

  11. (5 pts) Why is it necessary to include the appropriate header file (for example, stdio.h) to use library functions in C?

    Including the header file shows the compiler prototypes for the functions in the library. If the compiler hasn't seen the prototype, it can't check that the function is being called correctly.

  12. (5 pts) Give a UNIX command (or commands) that will show a listing of the contents of the current directory, one page at a time.

    ls | more
    

  13. (5 pts) What does the following program print?
    #include <stdio.h>
    
    void inc(void);
    
    main() {
      int x = 7;
    
      inc();
      printf("x is: %d\n", x);
    }
    
    void inc(void) {
      int x;
    
      x++;
    }
    

    This program prints:

    x is 7
    

  14. (10 pts) What logical error does the following program contain?
    #include <stdlib.h>
    
    main() {
      int i;
      int *ptr;
    
      ptr = &i; 
      free((void *) ptr);
    }
    

    Memory should only be returned using free() if it was allocated using malloc(). This program could crash or the value of i could change unpredictably.

  15. (5 pts) What does the following program print?
    #include <stdio.h>
    #include <stdlib.h>
    
    main() {
    int i;
    int *ptr;
    
      ptr = &i;
      *ptr = 4;
      i = 3;            
      printf("*ptr is: %d\n.", *ptr);
    }
    

    This program prints:

    *ptr is: 3.
    

  16. (5 pts) Describe the value of the array variable arrayOfInts after the following declaration.
    int arrOfInts[10] = {0};

    All 10 index positions in arrOfInts contain 0.

  17. (5 pts) Why is it useful to keep track of a list by keeping the address of the first element of the list, rather than the first element itself?

    Because a pointer set to NULL can be used to represent the empty list. It is difficult to represent an empty list with a list element.