~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.
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.
|
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.
gcc -Wall -o ptrtest1 ptrtest1.c ptrfuncs.c
~cs60/hw2/
(along with executable versions
of ptrtest2 and randata from the parts below). Your results should match
our ptrtest1 results exactly.
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)
ptrfunc.h
of course).
ptrfuncs.h
. The only extra calculations in this
program involve the average, and the counts of values less than and greater than or equal
to the average.
turnin hw2@cs60 hw2.txt ptrfuncs.c randata.c