CS2023 Assignment 5

Winter 2005

Due Friday, February 25, 10:30 AM, in the assignment bin on E-Level. This assignment will be marked out of 40 points.
  1. (3 points) In the following program the invocation of change_it() seems to have no effect. Explain.
    void change_it(int []);

    int main()
    {
      int a[5], *p;
      p = a;
      printf("p has the value %p\n", p);
      change_it(a);
      p = a;
      printf("p has the value %p\n", p);
      return 0;
    }
     void change_it(int a[])
    {
      int i = 777, *q = &i;
      a = q; /* a is assigned a different value */
    }
  1. (4 points) What is wrong with the following program? Correct the program and explain the meaning of its output.

  2. int main()
    {
      int a[] = {0, 2, 4, 6, 8};
      int *p = a + 3;

      printf("%s%d%s\n%s%d%s\n",
         "a[?] = ", *p,  "?",
         "a[?+1] = ", *p + 1, "?");
      return 0;
    }

  3. (6 points) Write the following function:
  4. void split_time(long int total_sec, int *hr, int *min, int *sec);
    total_sec is a time measured in number of seconds since midnight. hr, min, and sec are pointers to variables into which the function will store the equivalent time in hours (0-23), minutes(0-59), and seconds (0-59), respectively.
  5. (10 points) Write the following function:

    void FindTwoLargest(int a[], int n, int *largest, int *secondLargest);

    When passed an array a of length n, the function will search for its largest and second-largest elements, storing them in the variables pointed to by largest and secondLargest, respectively. Use pointer arithmetic, and not array subscripting. In other words do not use counter variables or the [] operator to traverse the array.

Deliverables

A harcopy of your assignment in the bin on E-level.
 
Eric Aubanel, Feb. 16, 2005