Design ONE FUNCTION in a C++ code to find minimum, maximum and average of items in an array, then place them proper locations in the array.
Follow these steps: 1. Create an array with 11 integers, which will be randomly selected from the range of 10 to 100. Only random numbers between 10 and 100 are allowed in the array. Print the items of the array on screen as one line.
2. Develop a function that takes the array as argument and perform these operations:
-Find the minimum of array’s items and replace (swap) with first item of the array.
-Find the maximum of array’s items and replace (swap) with last item of the array.
-Find the average of array’s items and assign it to middle location of the array. The average of numbers should be calculated as an integer. (hint: static_cast(float))
Note that you need to complete ALL of three operations in ONE FUNCTION. After calling this function, print the array on screen in one line.
An example run is shown here.
32, 41, 12, 71, 34, 22, 45, 11, 94, 55, 35,
11, 41, 12, 78, 34, 41, 45, 32, 35, 55, 94,
The min of items, 11, replaced with the first item, 32. The max of item, 94, replaced with the the first item, 35. The average 41.09 overwrites 22 as 41. It is ok to lose the mid item of the array.
PROGRAM:
#include <iostream>
// <bits/stdc++.h> for STL libraries
#include <bits/stdc++.h>
using namespace std;
// function to calculate minimum,maximum and average
// and place them in respective positions
void min_max_avg(int array[]) {
// using STL libraries to find minimum,maximum
// and total sum of the array elements
int min_index,max_index;
// *min_element (first_index, last_index) returns minimum element
// *max_element (first_index, last_index) returns maximum element
// accumulate(begin, end , initial_sum) returns sum
int max = *max_element(array, array + 11);
int min = *min_element(array, array + 11);
int sum = accumulate(array, array + 11, 0);
// static_cast as in the question
float avg = static_cast<float>(sum/11);
// typecasting to int using round()
int avg_int = round(avg);
// to find index of maximum element and minimum element
for (int i = 0;i < 11 ; i++) {
if (array[i] == max)
max_index = i;
else if (array[i] == min)
min_index = i;
}
// swapping minimum,maximum,average with respective positions
int temp = array[0];
array[0] = min;
array[min_index] = temp;
temp = array[10];
array[10] = max;
array[max_index] = temp;
array[5] = avg_int;
// displaying modified array
cout << "\nThe final resultant array is : \n";
for (int j = 0;j < 11;j++) {
cout << array[j] << " ";
}
}
int main() {
int random_numbers[11];
for (int i = 0;i < 11;i++) {
// rand() generates random number
// for random number within range = (rand() % upper_limit - lower_limit + 1)) + lower_limit
random_numbers[i] = (rand() % (100 - 10 + 1 ) )+ 10;
}
cout << "The random array is : \n";
for (int j = 0;j < 11;j++) {
cout << random_numbers[j] << " ";
}
// function call
min_max_avg(random_numbers);
cout<<"\n";
return 0;
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.