CS 16, Winter 2018

Programming Assignment 6

Due: Sunday, March 11, 11:59pm
Worth: 100 points

PA6 can be done either in two-people teams or individually. If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.

This assignment is all about practice using pointers. Therefore, please try to solve the problems using pointer notation only, and not equivalent array notation (i.e., no [], the square brackets).

There is a program skeleton and three other necessary files this time, and you probably want a copy of the sample data file from Part 2 as well. Get copies of ptrfuncs.cpp, ptrfuncs.h, ptrtest1.cpp, ptrtest2.cpp and data1.txt from ~cs16/pa6/ and we suggest you store them in a new directory in your own account at CSIL, specifically ~/cs16/pa6. You can use the Linux wildcard character '*' to get them all at once:

cp ~cs16/pa6/* ~/cs16/pa6/
  1. [50 points] Complete ptrfuncs.cpp to implement the first four functions declared in ptrfuncs.h. All of the functions in ptrfuncs.h involve C++ pointers. Most of them also manipulate a collection of consecutively stored double values - much like a double array. A pointer to the first value provides access.
    Hint/reminder: *p is the same as p[0], and p[i] is the same as *(p + i).
    Begin with this ptrfuncs.cpp skeleton and follow any instructions in that file. For this part, 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 its address.
    • 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.cpp to test your implementations. You can compile it and link to your functions, and then run it as follows:
    g++ -o ptrtest1 ptrtest1.cpp ptrfuncs.cpp
    ./ptrtest1
    Your results must match our ptrtest1 results exactly.
  2. [50 points] After your Part 1 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.
    • void sortValues(double *first, double *last) - sort the values from first through last, where these parameters are pointers into the same dynamic array. Write your own sorting routine, such as the selection sort procedure presented in Chapter 7 - but remember you should use pointer notation, not array notation to do it. You may write helper functions if you want, but they should use pointer notation too.
    Use ptrtest2.cpp to test your implementations. Compile it as you did above, subsituting ptrtest2 for ptrtest1 in both places. This program expects three command line arguments in this order: an input filename, the number of values to read from the file, and the number of values to print per row. Here are two sample runs using data1.txt as the input file: test2-out.txt. Your results must exactly match if we test your program the same way, but you should ensure the program works for different inputs too.
  3. Submit PA6 at https://submit.cs.ucsb.edu/, or use the following command from a CS terminal:
    ~submit/submit -p 967 ptrfuncs.cpp
    Be sure to wait for the test results. If you score 100/100 and you've followed all of the other rules, then you'll earn full credit.

Updated 3/4/2018, by C. Michael Costanzo