Lab 8 - Passing Arrays to Functions
The purpose of this array is to learn how to pass arrays to functions, in
particular, an input function, a calculation function and an output function.
Program Requirements: You will be creating a program to read in an
array of doubles from the keyboard, calculate the sum, and print the array
and its sum. In main(), declare a double array that can hold up to 50 doubles
and an integer to store the current size (initialize this to 0). Then call
the getArray() and printArray() functions (integer versions were discussed
in class on Wednesday).
The required functions for this assignment are:
- getArray - This is a void function. It will take two parameters: a double
array variable and a by-reference integer for the current size. It will prompt
the user to enter how many doubles they wish to add and read that into size.
It will then validate (using a validation loop) that the size is greater
than 0 and less than or equal to 50 (the capacity). It will then use a for()
loop to read in the number of doubles the user wishes to enter into the passed
array.
- calcSum - This is a calculation function that returns a double. It will
take two parameters: a double array variable and a by-value integer for the
current size. It will calculate and return the sum.
- printArray - This is a void function. It will take two parameters: a double
array variable and a by-value integer for the current size. It will print out
all of the elements in the array using 2 decimal places of precision and a
column size of 7. It will then call calcSum() and print out the returned sum.
NOTE: Do NOT call calcSum() in main(). Only call it from this
printArray() function.
The output should resemble the following (the user input is in
blue font face):
How many doubles do you wish to add? -5
That is an invalid size. You must enter a value between 1 and 50.
How many doubles do you wish to add? 5
Enter double 1: 1
Enter double 2: 1.5
Enter double 3: 2
Enter double 4: 3.2
Enter double 5: 2.5
The array contains:
1.00 1.50 2.00 3.20 2.50
The sum is 10.2
Name your code lab8.cpp and email your completed code to me.