Question

Design ONE FUNCTION in a C++ code to find minimum, maximum and average of items in...

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.

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
find a 95% confidence interval for the mean electricity consumption for all days having average temperature...
find a 95% confidence interval for the mean electricity consumption for all days having average temperature 73.6 degrees. Also, find a 95% prediction interval for the electricity consumption for a new day with average temperature 73.6 degrees. temperatureavg kilowattsperhour 77.5 45 80 73 78 43 78.5 61 77.5 52 83 56 83.5 70 81.5 70 75.5 53 69.5 51 70 39 73.5 55 77.5 55 79 57 80 68 79 73 76 57 76 51 75.5 55 79.5 56 78.5...
1.Write a c++ program to find Maximum out of two numbers using friend function. Here one...
1.Write a c++ program to find Maximum out of two numbers using friend function. Here one member is of one class and second belongs to another class. 2.Write a c++ program to swap the values of private data members of classes names classOne and classTwo using friend keyword.
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a text file data_in.txt with the following information (highlight the piece of text below with numbers and copy it to a text file): 54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15,...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
We want to compare the average gas mileage of American-made cars vs. Japanese-made cars. The claim...
We want to compare the average gas mileage of American-made cars vs. Japanese-made cars. The claim is that the Japanese cars and American cars do not get the same gas mileage. Use the list data below to test the hypothesis that the Japanese cars and American cars do not get the same gas mileage. The American cars are in list1 and the Japanese cars in list2 below We do not know whether the mileages are normally distributed or not, but...
ASK YOUR TEACHER Consider the sample space given below. A die is a cube with six...
ASK YOUR TEACHER Consider the sample space given below. A die is a cube with six sides on which each side contains one to six dots. Suppose a blue die and a gray die are rolled together, and the numbers of dots that occur face up on each are recorded. The possible outcomes of the sample space S are listed as follows, where in each case the die on the left is blue and the one on the right is...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
Pinky and The Brain are great friends. They like to play games with numbers. This time,...
Pinky and The Brain are great friends. They like to play games with numbers. This time, Pinky has given The Brain a list of numbers and given him the task of determining if it is possible to choose a subset of them such that they sum is equal to another given number. Build an algorithm using dynamic programming to help The Brain with his problem. INPUT The first line corresponds to N, the amount of numbers given by Pinky The...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
1. Driving under the influence of alcohol (DUI) is a serious offense. The following data give...
1. Driving under the influence of alcohol (DUI) is a serious offense. The following data give the ages, in order, of a random sample of 50 drivers arrested while driving under the influence of alcohol. This distribution is based on the age distribution of DUI arrests given in the Statistical Abstract of the United States (112th Edition). 16 18 20 21 21 22 22 22 23 24 24 25 26 26 26 27 27 27 29 30 30 31 31...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT