Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Apr 2017 |
Last Sign in: | 9 Weeks Ago, 5 Days Ago |
Questions Answered: | 4870 |
Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
How can i create a sort function to sort the entered integers from lowest to biggest before calculating the mean, median, and the rest of the functions. sort function is what i need . email attachment on what I need to do for the sorting
Â
Â
#include <iostream>
#include <stdlib.h>
#define SIZE 10
using namespace std;
// SIGNATURES of all the required Functions
void display(int []);
double mean(int []);
int median(int []);
void lessThanFive(int []);
int minimum(int []);
int sum(int []);
// MAIN FUNCTION
int main()
{
int arr[SIZE];Â // Array to store the integers
double avrg;Â // To store the average returned by the mean() Function
int mid = 0;Â // To store the median returned by the median() Function
int mini;Â // To store the minimum value of the array returned by the minimum() Function
int total =0;
// Read the values for the array
cout << "nEnter 10 values in the array (one by one):" << endl;
for(int i =0; i < SIZE; i++){
cin >> arr[i];
}
// Function call to display()
display(arr);
// Function call to mean()
avrg = mean(arr);
cout << "nAverage of the 10 values of the array is: " <<avrg;
// Function call to median()
mid = median(arr);
cout << "nMiddle Value of the array is: " << mid;
//Function call to lessThanFive()
lessThanFive(arr);
// Function call to minimum()
mini = minimum(arr);
cout << "nMinimum value in the array is: " << mini << endl;
// Function call to sum()
total = sum(arr);
cout << "nSum of the 10 digits in the array: " << total;
cout << endl;
return 0;
}
Â
// Function display(): To display all the integers in the array
void display(int arr[]){
cout << "nIntegers in the array: " << endl;
// Loop through every index of the array and print it on the screen
for(int i =0; i < SIZE; i++){
cout << arr[i] << " " ;
}
cout << endl;
}
// Function mean(): To return the average of the 10 values in the array
double mean(int arr[]){
int sum = 0;
double average = 0.0;Â // To store the average of Integers
// Loop through all the values and add them together
for(int i =0; i < SIZE; i++){
sum = sum + arr[i];
}
average = (double)sum / SIZE ;
return (average);Â // Returning the Average of the values
}
// Function median(): To return the middle value of the array
int median(int arr[]){
int average = 0;
int middle1, middle2;Â // To store the middle positions
middle1 = SIZE / 2;
middle2 = (SIZE / 2) - 1;
if((SIZE % 2) == 0)Â // If the size of the array is even
{
average = (arr[middle1]+arr[middle2]) / 2;
cout<<"nnThe no. of values is even! Getting Average of the two middle values...";
return average;
}
else{
return arr[middle1];
}
}
// Function lessThanFive(): To display values in the array which are less than 5
void lessThanFive(int arr[]){
int count = 0;
cout<<"nnThese are the values in the array which are less than 5: " << endl;
// Loop through all the values and check if they are less than five
for(int i =0; i < SIZE; i++){
if (arr[i] < 5){
cout << arr[i] << " ";
count ++;
}
}
if(count == 0)
cout << "There is no such value!" << endl;
cout << endl;
}
// Function minimum(): To return the minimum value in the array
int minimum(int arr[]){
int minval = arr[0];Â // set the default minimum value to the first value
// Loop through all the values and check if they are less than the current minimum value
for(int i =0; i < SIZE; i++){
if(arr[i] < minval)Â // If the value is less, set minval = value in the array
minval = arr[i];
}
return minval;Â // Returning the minimum value
}
// Function sum(): To return the sum of the 10 digits in the array
int sum(int arr[]){
int totalval = 0;Â // Set default sum equal to 0;
// Loop through each value in the array and add it to the previous sum
for(int i =0; i < SIZE; i++){
totalval = totalval + arr[i];
}
return totalval;Â // Return the sum
}
Attachments:-----------