Question
For C++ Write a program called array.cpp which prompts and reads a list of non-negative numbers (ints), i.e. can be 0, into an array, prints
For C++
Write a program called array.cpp which prompts and reads a list of non-negative numbers (ints), i.e. can be 0, into an array, prints the array, finds the maximum value in the array, adds the maximum value to each array number and then prints the maximum number and the modified array. The input list is terminated by a -99. The program should have a function read_list() for reading in a list of values, a function print_array which prints each array element, a function find_max() for finding maximum number in the array, and a function array_add() which adds a number from each element of the array.
All functions should be written AFTER the main procedure.
A function prototype should be written for each function and placed BEFORE the main procedure.
Each function should have a comment explaining what it does.
Each function parameter should have a comment explaining the parameter.
Declare a constant in the main function that stores the array size of 20. Do not declare this constant globally.
Implement the following functions as described below to receive full credit. Determine when to use the const specifier (see the lecture notes). The const specifier is used when a functions parameter will not be changed in the implementation of the function.
Write a function read_list() which prompts for the list of non-negative numbers terminated by -99. Read the non-negative integers into an array which stores integer numbers. The number of elements in the input list may be less than 20. Stop reading when either the input number is the sentinel value -99 or when 20 numbers have been read into the array. An invalid number entered will be ignored. The function takes three parameters, the array holding integers, the number of elements stored in the array (which is defined as pass by reference), and maximum size of the array (use the constant defined in step 5). Define the first two parameters so that they are modifiable. The function does not return any value.
Write a procedure print_array() which displays the values that were inserted into the array as a comma separated list and ending with a period. For example, if the user entered five integers (not including the sentinel) then only five array values are displayed (not 20). The procedure should take two parameters, the array holding integers, and the number of elements in the array. Define both parameters so that they are not modifiable. The function does not return any value.
Write a function find_max() which returns the maximum value found in an array of numbers. The function takes two parameters, the array holding integers, and the number of elements in the array. Define both parameters so that they are not modifiable.
Write a procedure array_add() which adds a number x to every element of an array. For instance, if the array contains (5, 3, 1) and x equals 2, then the procedure changes the array to (7, 5, 3). The procedure takes three parameters, the number x, the array, and the number of elements in the array. Define the array so that it is modifiable. The function does not return any value.
In the main program, use the following algorithm:
Call the function read_list() to prompt and read the input list of values terminated by the sentinel value.
Call the procedure print_array() to display the array values. Call the function find_max() to search for the largest value in the array. Call the procedure array_add() to add the maximum value to each element of the array. Pass the maximum value computed in the previous step as the number x.
Call the procedure print_array() to display the array values after it was modified in the previous step.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started