CS 60, Fall 2009
Assignment 2

Due: Friday, October 23, 9:00 pm
Worth: 100 homework points

  1. Read usingptrs.c (this file and the others are also in ~cs60/hw2/). Then run it, and demonstrate your understanding of the program's features by explaining the output. Type your explanations into a plain text file named hw2.txt. Label them (a to e) to correspond with the various program and output sections.
  2. Complete ptrfuncs.c to implement the functions declared in ptrfuncs.h, all of which involve C pointers. Most of the functions also manipulate a collection of consecutively stored double values - much like a double array. A pointer to the first value provides access.
    You are not allowed to use any array index notation (i.e., [], the square brackets) anywhere in ptrfuncs.c. All references to the double values in the collection must use pointer notation.
    1. Begin with this ptrfuncs.c skeleton and follow any instructions in that file. For now, just implement the first four functions:
      • double sum(double *values, int n) - find and return the sum of the n values - the pointer holds the address of the first one.
      • double *maxPtr(double *values, int n) - find the maximum value, and return a pointer to it.
      • double *minPtr(double *values, int n) - ditto the minimum value.
      • double valueDiff(double *left, double *right) - find and return the difference between the two double values as left value minus right value.
      Use ptrtest1.c to test your implementations. You can compile it and link to your functions as follows:
      gcc -Wall -o ptrtest1 ptrtest1.c ptrfuncs.c
      An executable version of our solution is available in ~cs60/hw2/ (along with executable versions of ptrtest2 and randata from the parts below). Your results should match our ptrtest1 results exactly.
    2. After that testing is complete, implement the other two functions:
      • void printTable(double *values, int n, int perRow) - print the values as a neatly-formatted table with the specified number of values per row, and each value in a field of width 10 showing 2 significant digits.
      • double *randomValues(int n, double low, double high) - dynamically allocate memory to store n double values using malloc(). Then fill this memory with n random values in the range low to high, inclusive. Page 168 of the K&R text shows how the integer function named rand() can be used to find a random double value ranging from 0.0 to 1.0:
        (double) rand() / (RAND_MAX + 1.0)
        Then convert this random value to a value in the required range.
      Use ptrtest2.c to test your implementations. Insure your results match the ptrtest2 results from our solution, except for the random values themselves. Also notice you can specify a random seed on the command line when you run ptrtest2.c - doing so will cause the same random sequence to be produced whenever the same seed is used - this feature may facilitate your testing.
  3. Complete randata.c, an application that generates random data and calculates statistics (using the functions of ptrfunc.h of course).
  4. Turn in hw2.txt, ptrfuncs.c and randata.c from your engineering account as follows:
    turnin hw2@cs60 hw2.txt ptrfuncs.c randata.c
    Late assignments may not be accepted.

Updated 10/9/09 by C. Michael Costanzo