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
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,...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
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...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
You are working for a company that is responsible for determining the winner of a prestigious...
You are working for a company that is responsible for determining the winner of a prestigious international event, Men’s Synchronized Swimming. Scoring is done by eleven (11) international judges. They each submit a score in the range from 0 to 100. The highest and lowest scores are not counted. The remaining nine (9) scores are averaged and the median value is also determined. The participating team with the highest average score wins. In case of a tie, the highest median...
USING C++ The purpose of this assignment is the use of 2-dimensional arrays, reading and writing...
USING C++ The purpose of this assignment is the use of 2-dimensional arrays, reading and writing text files, writing functions, and program planning and development. You will read a data file and store all of the input data in a two dimensional array. You will perform calculations on the data and store the results in the 2 dimensional array. You will sort the array and print the results in a report. Instructions You will read in the same input file...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item = 0; 4. while (count < 4) { 5.      cout << "Enter item cost: "; 6.      cin >> item; 7.      item_total += item; 8.      ++count; 9. } 10. int average_cost = round(item_total / count); 11. cout << "Total cost: " << item_total << "\nAverage cost: " << average_cost; (Refer to Code Example 8-1.) If the user enters 5, 10, and 15 at the prompts, the output is: Total...