Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language: C++ Write a program called array.cpp which prompts and reads a list of

ID: 3761503 • Letter: L

Question

Language: C++

Write a program called array.cpp which prompts and reads a list of positive numbers (ints) into an array, prints the array, finds the minimum value in the array, subtracts the minimum value to each array number and then prints the minimum number and the modified array. The input list is terminated by a 0. 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_min() for finding minimum number in the array, and a function array_subtract() which subtracts 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 25. 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 function’s parameter will not be changed in the implementation of the function.

Write a function read_list() which prompts for the list of positive numbers terminated by 0. Read the positive integers into an array which stores integers. The number of elements in the input list may be less than 25. Stop reading when either the input number is the sentinel value 0 or when 25 numbers have been read into the array. An invalid number, i.e. a negative 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 25). 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_min() which returns the minimum 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_subtract() which subtracts 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 (3, 1, -1). 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_min() to search for the smallest value in the array.
Call the procedure array_subtract() to subtract the minimum value to each element of the array. Pass the minimum 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.

Thoroughly test your program on different input. For example, the user enters no input numbers or negative values. Also ensure that your program does not allow the user to enter more than 25 valid numbers. Compare your output against the provided solution array_solution.exe.

Be sure to add the header comments “File”, “Created by”, “Creation Date” and “Synopsis” at the top of the file. Each synopsis should contain a brief description of what the program does.

Be sure that there is a comment documenting each variable.

Be sure that your if statements, for and while loops and blocks are properly indented.

Explanation / Answer

#include <iostream>
#include <cmath>

using namespace std;
void read_list(int array[], int & num_elements, const int ARRAY_SIZE);
void print_array(const int array[], const int num_elements);
int find_min(const int array[], const int num_elements);
void array_scale(const int x, int array[], const int num_elements);

int main()
{
const int ARRAY_SIZE(25);
int array[ARRAY_SIZE];
int num_elements(0);
int x(0);
read_list(array[ARRAY_SIZE],num_elements,ARRAY_SIZE);
print_array(array[ARRAY_SIZE], num_elements);
find_min(array[ARRAY_SIZE], num_elements);
array_scale(x, array[ARRAY_SIZE], num_elements);

return 0;
}
void read_list(int array[], int & num_elements, const int ARRAY_SIZE){
cout<<"Enter positive numbers (ints) terminated by 0";
cin>> array[0];
for(int i =1; i <ARRAY_SIZE-1; i++){
cin>> array[i];
num_elements = num_elements +1;
}
}
void print_array(int array[],int num_elements){
cout<<"Before list: ("<<num_elements<<" numbers):"<<endl;
for (int i =0; i<num_elements; i++){
cout<<array[i]<<", ";
}
cout<<"."<<endl;
}
int find_min(int array, int num_elements){
int array_min(0);
if (num_elements < 1){
return(0);
}
array_min = array[0];
for(int i =1; i<num_elements; i++)
{
if (array[i] < array_min)
{
array_min = array[i];
}

}
return(array_min);
}
void array_scale(int x, int array[], int num_elements){
for(int i =0; i<num_elements; i++)
{
array[i] = array[i] * x;
}
for(int i =0; i<num_elements; i++){
cout<<array[i]<<", ";
}
}