CIS 150 C++ Programming Project: Arrays
Objectives
- Follow instructions (program requirements)
- Declare and use a constant
- Create and use arrays
- Create and use functions
- Create prototypes for functions
- Create random numbers
- Use repetition
- Pass arrays to functions
- Format output
Overview
This assignment involves creating an array and three functions to
process an array. The first function fills an array with random numbers.
The second function calculates a sum for the elements of an array.
The third function displays an array in a nice format.
Program requirements
- You must use prototypes for all of the functions other than main.
- You may not have any global variables.
- In main, use a constant to define the size of the array. That constant should be set to 40 (but may be changed during testing).
- In main, define an array of integers using the constant as the size, and pass it to the required functions to fill the array and then display the array. Remember that you are passing by reference.
- Create prototypes for the following functions. The prototypes must be placed before main(). The function implementations must be placed after main().
- A function that will load random integers into an array. The random numbers should range from 1 through 100. The function has 2 parameters, the array and the size of the array. This function should not return anything.
- A function that will add all the values in an array and return the total. The function has 2 parameters, the array and the size of the array. It should return the total. The array should be passed as a constant to prevent changes to the content. This functon is not called by main. It is called by the function where a total and average are needed.
- A function that will output all the values in the array in rows which have 10 numbers per row. Each number should be in a field 4 characters wide. This function should also output the total and the average on separate lines after the array is displayed. This function has 2 parameters, the array and the size of the array. This function returns nothing.
- The display function should still display the total and average on separate lines after the array even if the number of elements in the array is not a multiple of 10. See sample run 3 for an example.
Sample Runs
Sample run #1
30 26 56 28 2 70 89 49 1 81
24 56 1 61 94 44 68 50 46 45
33 41 92 38 30 33 38 88 59 96
30 43 23 39 33 60 9 62 61 46
The total is 1875
The average is 46.875
Sample run #2
32 75 87 18 60 60 8 56 56 75
89 53 25 30 47 75 37 14 16 88
89 57 81 1 15 88 35 90 67 37
42 54 69 43 95 46 88 42 46 42
The total is 2128
The average is 53.2
Sample run #3 (with the constant set to 13)
14 82 77 6 41 12 39 15 23 19
32 17 71 7
The total is 455
The average is 32.5
Rubric
- 3 points: for proper documentation comments and for correctly following coding conventions (indentation, naming rules, NO tabs, etc.)
- 3 points: for having correct function prototypes
- 1 point: defining and using a constant to size the array
- 1 point: defining the array in main
- 2 points: for correctly calling the two functions main must call
- 5 points: for the function that loads the array with random numbers
- 1 point: for using srand() correctly
- 2 points: for correctly generating random numbers in the range from 1 through 100
- 5 points: for the function that adds all the values in the array and returns the total
- 5 points: for correct output formatting of the array in the display function
- 2 points: for correctly using the totaling function from the display function (not from main) and displaying the total and average on separate lines