All functions should be written AFTER the main procedure. 2. A function prototyp
ID: 3531351 • Letter: A
Question
All functions should be written AFTER the main procedure.
2. A function prototype should be written for each function and placed BEFORE the main
procedure.
3. Each function should have a comment explaining what it does.
4. Each function parameter should have a comment explaining the parameter.
5. Declare a constant variable storing the array size of 100. This constant variable can either be
a global constant or can be declared in the procedure main().
6. Prompt for the list of positive numbers terminated by 0. (You do not need to check that the
numbers are positive.) The inputs may be floating point numbers. Read the numbers into an
array which stores double precision floating point numbers. The number of elements in the
input list may be less than 100. Stop reading when either the input number is the sentinel
value 0 or when 100 numbers have been read into the array. (0 is not part of the input list and
should not be stored in the array.)
7. Write a function find_minimax_avg() which returns the average between two values: the
minimum value found in an array of numbers and the maximum value found in the same
array. The function should take two parameters, the array, and the number of elements in the
array. The array parameter can either be declared as a fixed size array or as a variable size
array.
Use the examples found in the lecture slides to search for the maximum value in an array,
then similarly find the minimum value, and finally compute and return the average of these
two numbers.
8. Write a procedure array_add() which adds a number x to every element of an array.
For instance, if the array contains (1.5, 3.5, 1) and x equals 0.2, then the procedure changes
the array to (1.7, 3.7, 1.2).
The procedure should take three parameters, the number x, the array, and the number of
elements in the array. The array parameter can either be declared as a fixed size array or as a
variable size array.
The procedure modifies the array but does not return any value.
9. In the main program, call the function find_minimax_avg() to get the average of the
minimum and maximum values. Print this average value.
10. In the main program, call the procedure array_add() to add the average value from each
element of the array. Pass the average value computed in the previous step as the number x.
Print the new values of the array..
This is what i have so far
Explanation / Answer
Everything that was asked for except descriptions of functions. Enjoy!
#include <iostream>
using namespace std ;
const int MAX = 101; //1 extra for null termination to avoid seg fault can remove
double find_minimax_avg(double [],int);
void array_add(double, double [], int);
int main()
{
int i;
double userInput, avg;
double arr[MAX];
cout << "Enter element into array (0 to exit): ";
for(i = 0; i < 100; i++){
cin >> userInput;
if(userInput == 0){
break;
}
arr[i] = userInput;
}
avg = find_minimax_avg(arr,i);
cout << "Average of minimum and maximum of array is: " << avg << endl ;
array_add(avg,arr,i);
cout<<endl<<"Modified array after adding average: ";
for(int j = 0; j < i; j++){
cout << arr[j] << " ";
}
cout << endl;
system("pause");
return 0;
}
double find_minimax_avg(double arr[],int size)
{
double min = arr[0], max = arr[0];
for(int i = 1; i < size ;i++){
if(arr[i] < min)
min = arr[i];
else if(arr[i] > max)
max = arr[i];
}
return (min+max)/2.0;
}
void array_add(double num,double arr[], int size)
{
for( int i = 0; i < size; i++){
arr[i] += num ;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.