CIS071 Lab10
- Age vs Relative Bone Density DUE:
(Friday, Apr 7, NOON)
Scope: Functions
with output arguments, Input loops with trailer
Lab10: Age vs.
Relative Bone Density Study. Compute the average age and average bone density of
a group of subjects. Write a program which will:
1. In a loop:
a. Prompt the user for an
age (integer value > 0) and bone density (decimal value between 0.0 and
1.0).
b. If bone density not
between 0.0 and 1.0, discard the age-density pair, write an “invalid data”
message, and request new data.
c. Accumulate totals of
age and bone density and count the number of data pairs supplied.
2. The loop terminates when a negative age or bone
density is entered. The average age and bone density is then calculated
(divide the totals by the count of pairs supplied). Display the totals of age
and bone density, count of pairs and the calculated averages.
Use the following functions:
int get_value_pair(int *age, double *density)
This function has output arguments of one pair of age and bone density values.
It returns -1 when a negative age or bone density is entered (loop termination
signal). Otherwise it returns 0 and supplies a pair of age and bone density
values.
void update_sums (int age, double
density, int *age_sum,
double *density_sum, integer *count)
This function updates the age and density totals with the current values of age
and density. It also increases count by 1 for each pair of values.
Define and use the following functions:
update_averages
This
function computes and returns the average age and average density as follows:
average_age
= age_sum/input_count
average_density
= density_sum/input_count
If input_count is 0 (no data supplied) return 0 for
the value of average age and average density.
show_results
This
function displays the sums, input count and averages in a user friendly format.
In your test run,
at least one test should use a set of values whose sum can be easily verified
e.g. the sum of the first n integers is n(n+1)/2 and .1+.2+.3+...+.n =
.1(1+2+3+..+n) = .1(n(n+1)/2)
Sample Output:
% bones
Supply age (<= 0 to end input):3
Results of Age vs Bone Density Study
Age Density Pairs: 0
Ages Total: 0
Bone Density Total: 0.000
Average Age: 0.0
Average Bone Density: 0.000
% bones
Supply age (<= 0 to end input):1
Supply density value (between 0 and 1):0.1
Supply age (<= 0 to end input):2
Supply density value (between 0 and 1):2.0
invalid density, age, density values discarded
Supply age (<= 0 to end input):2
Supply density value (between 0 and 1):0.2
Supply age (<= 0 to end input):3
Supply density value (between 0 and 1):0.3
Supply age (<= 0 to end input):4
Supply density value (between 0 and 1):0.4
Supply age (<= 0 to end input):0
Results of Age vs Bone
Density Study
Age Density Pairs: 4
Ages Total: 10
Bone Density Total: 1.000
Average Age: 2.5
Average Bone Density: 0.250
% exit
%
Hints:
Inside get_value_pair:
- Do a prompt-read for age. If age <= 0 return –1
(Indicating that we are finished reading age-density pairs).
- Otherwise prompt-read bone density. If bone density
<= 0 return –1 (the age value will be discarded in main).
- Otherwise return 0 (Checking that bone density is
between 0.0 and 1.0 is done in main).
Inside main:
- Define variables for total entries, current age,
current density, total age, total density, average age, average density. Set
total entries = 0, total age and total density = 0.0.
- Inside a do-while loop (which continues as long as the
return value from get_value_pair == 0):
- Call function get_value_pair.
- If the function returned 0:
- If
bone density is not between 0 and 1, write message
- Otherwise
call function update_sums to add age, density to
their totals and increase total entries.
- There is no else case (e.g.
the function did not return 0) since we want to bypass function update_sums).
- After the do-while loop terminates:
- Call function update_averages
which (if total entries > 0) computes:
average age = total age / total
entries
average density = total density /
total entries
(If total entries == 0 set the
averages = 0)
- Call function show_results
which displays total entries, total age, total density, average age, average
density
Testing:
1. Negative age as first entry (no data)
2. Mixed valid and invalid age-density pairs terminating with 0 age.
3. Validate calculations using pairs (1, .1), (2,.2),(3,.3)...(n,.n) which has totals for age of n(n+1)/2.
and totals for density of
0.1(n(n+1)/2)