Sample Exam 2

  1. (20 points) Write a complete C function that displays a string with all its a's removed. For example, if the function is given the string

    Are we having any fun, yet?

    it will display the string

    Are we hving ny fun, yet?

    You may use either array subscript notation or pointer notation to solve this problem.

  2. (20 points)

    (a)
    In a single statement, declare an array of type float called numbers with 5 elements, and initialize the elements to the values 0.0, 1.1, 2.2, 3.3, 4.4.

    (b)
    Declare a pointer variable called nptr that can point to an object of type float.

    (c)
    Give two different statements that assign the starting address of array numbers to the pointer variable nptr.

    (e)
    Write a for-loop that adds 1.0 to each element of the array. Access the elements of the array using pointers (instead of subscripts).

  3. (15 points) What would be displayed by the following program?
    #include <stdio.h>
    
    void passPointers(int *p1, int *p2);
    
    int main()
    {
      int array[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
      int *pBeg, *pEnd;
      int i;
    
      pBeg = array;
      pEnd = &array[9];
    
      passPointers (pBeg, pEnd);
    
      for (i=0; i<10; i++)
        printf("%d  ", array[i]);
      return 0;
    }
    
    void passPointers (int *p1, int *p2)
    {
      int i;
    
      for (i=0; i<3; i++)
        p1++;
    
      for (i=0; i<3; i++)
        p2--;
    
      while (p1 != p2){
        *p1 = *p1 + 5;
        p1++;
      }
    
      return;
    }
    

    Questions 4 - 6 are worth 5 points each:

  4. Given these C declarations:
      float x;
      float *z=&x;
    
    which scanf statement would successfully read a float value into x?

  5. Complete the following sentence: The function fwrite

  6. An advantage of text files over binary files is that

  7. (15 points) Here is a makefile:
    
    
    # makefile for exam 2
    
    projectAlpha:  main.o tree.o list.o
            gcc main.o tree.o list.o -o projectAlpha
    
    main.o:  main.c tree.h list.h
            gcc -c -Wall main.c
    
    tree.o:  tree.c tree.h
            gcc -c -Wall tree.c
    
    list.o:  list.c list.h
            gcc -c -Wall list.c
    
    # end of makefile
    
    
    

  8. (15 points) The babies array declared below holds the birth records of babies born at General Hospital:
    struct date{
       int month;
       int day;
       int year;
    };
    
    struct birthrecord{
       char lastname[25];  /* a null-terminated string */
       char firstname[25]; /* a null-terminated string */
       struct date dob;    /* date of birth            */
       float length;
       int pounds;
       int ounces;
    };
    
    struct birthrecord babies[500];