Question: Write a C++ program named, arrays.cpp , with the following functions: (name functions EXACTLY as given below, including return type and parameter list As with
Write a C++ program named, arrays.cpp, with the following functions: (name functions EXACTLY as given below, including return type and parameter list As with the previous project, your functions will be tested by a separate program, different from your own version of main.)
void fillArray (int array[], int n) Two Parameters: array of integers and number of random integers to store in the array Description: generate n random numbers from 1 to 100, inclusive, and store them in elements [0] through [n 1] Note: The array is always the same size, i.e., 100 elements, but may be filled with a different number of random values on each run. The different number is passed in to this function and should always by less than or equal to 100. USE: Caller invokes this function to have n random values between 1 and 100, inclusive, stored into the first n elements of the array
void printArray (int array[], int n) Two Parameters: array of integers and number of integers currently stored in the array Description: print the values in the array. The values must be displayed with 10 integers per line and a comma after each value. The last value on each line does not have a comma after it. The last line may have fewer than10 values if the total numbers of values printed is not evenly divisible by 10. For example, if the user asked for 15 array elements to be printed, then the last line would only have 5 values; or if the user asked for 92 values, then the output would be 9 lines containing 10 values and the last output line would have only 2 values. USE: Caller invokes this function to display the first n elements of the array
double average (int array[], int n) Two Parameters: array of integers and number of integers stored in the array Description: calculate the floating point average of the values in the array. Return the average as a double value. Make sure you code this function to accurately compute the average as a double value and NOT an integer. USE: Caller invokes this function to have computed the average of the first n elements of an array
int countAboveAverage (int array[], int n, double average) Three Parameters: array of integers, number of integers stored in the array, and average of values in array Description: count and return the number of values in the array that are above the average. USE: Caller invokes this function to have computed the number of the first n elements of an array that are greater than the average of those n element values. Note: This function does NOT call the average function. The caller passes the average in as the third parameter.
void printConsecutive (int array[], int n) Two Parameters: array of integers and number of integers stored in the array Description: print the positions, i.e., the subscripts, of the three consecutive array elements that have the largest average. If more than 3 consecutive values have the same average, print the positions with the lowest subscripts. Note: print the positions (subscripts), NOT the actual values. Subscripts begin with 0 (zero) and the last position would be equal to n 1. USE: Caller invokes this function to display the subscripts of the 3 consecutive array elements that have the greatest average of all sets of 3 consecutive array elements.
double coverage (int array[], int n) Two Parameters: array of integers and number of integers stored in the array Description: determine the percentage of the numbers from 1 to 100 that appear in the array. For example, if the array holds 100 integers and all the integers are unique (all numbers from 1 to 100 appear), the percentage would be 100%. The expected percentage is lower as duplicates may appear. To do this, each value from 1 to 100 is searched for in the array (using the search function) and if the number appears, a counter is incremented. Then the percentage is calculated. The result will be a value from 0.01 to 1.00. Note: if the user has asked for 10 values, then the maximum coverage in this case would be .1. This would mean that ten unique values between 1 and 100 are contained in the array. If, for example, the user asked for 50 values, then the max coverage would be 50% (.5), meaning than the array contained 50 values between 1 and 100 with NO duplicates. If there were two duplicates, this would make the coverage only 48% (.48). As a benchmark, when you ask for 100 random values the coverage typically (not ALWAYS!) runs around 62-66%. This function should return values as decimal, for example: .05 for 5%, .64 for 64%, etc. Note: This is the ONLY function that uses the search function (described next). Nothing else needs or calls the search function.
int search (int array[], int n, int key) Three Parameters: array of integers, number of integers stored in the array, and key to search for in the array Description: using the linear search from the notes (Not the text), search for the key in the array and return the position if found or -1 if not found This function will only be needed (and used) by the coverage function (above). USE: ONLY the function, coverage, invokes this function (search) and does so to find out if a certain value (key) is found among the first n elements of the array.
Use the following algorithm for the main function
declare list to be an array to hold 100 integer (note: array is ALWAYS 100 integers long, but user may not use them all) declare number to an an integer prompt for a value from 3 to 100 and store the input into number call fillArray (list, number) call printArray (list, number) output the result of calling countAboveAverage (list, number, average(list, number)) call printConsecutive (list, number) output the result of calling coverage (list, number)
So, while the array is declared to have 100 elements, the number of elements used is the number entered by the user (3..100). This is why the user's input value (n) is passed around to the various functions: so these functions know how many elements of the array to use. The unused elements should be ignored.
Coding Requirements and Restrictions
All variables must be declared locally to a function. No global variables.
Make sure your program has good prompts and clearly labeled output. Add blank lines to the output to improve its readability.
Do NOT use any language constructs that have not been covered in class.
Make your code readable by using meaningful variable names, indentation and spacing in other words, ALL of the coding conventions for this course.
All functions must be documented with a description (prolog).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
