Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: Develop a program that calculates basic statistics on a one-dimensi

ID: 3882305 • Letter: D

Question

Description:

Develop a program that calculates basic statistics on a one-dimensional array of doubles. Your program should first prompt the user for the size of the array followed by prompting for individual elements of the array. Your program should contain the following functions:

1.) min()

Finds and returns the minimum value within the array.

2.) max()

Finds and returns the maximum value within the array.

3.) mean()

Finds and returns the mean value of the elements in the array.

4.) median()

Finds and returns the median value of the elements in the array.

5.) sort()*

Sorts the elements of the array in ascending order.

6.) readArray()

Populates the elements of the array with input from the user (or via file redirection).

7.) printArray()

Prints the elements of the array.

*You may use any sorting algorithm that you want. Here are a few easy suggestions:

Bubble sort pseudocode:

do
   swapped = false
   for each i in 1 to length(A) inclusive do:
     if A[i-1] > A[i] then
       swap A[i-1] and A[i]
       swapped = true
     end if
   end for
while swapped

Insertion sort pseudocode:

i 1
while i < length(A)
   j i
   while j > 0 and A[j-1] > A[j]
       swap A[j] and A[j-1]
       j j - 1
   end while
   i i + 1
end while

Additional Specifications:

Your program should not use any pre-existing classes such as string or vector classes!

Make sure your program compiles and runs on one of the Linux machines in the Linux lab before you submit.

Your program should consist of a single source code.

EXAMPLE OUTPUT:

Examples using file redirection:

casey@vanderwaal:-/Dropbox/Teaching/CSCE24oFalt2617/Projects/Assignment2$ g++ Assignment2.cpp casey@vanderwaal:-/Dropbox/Teaching/CSCE240Fall2017/Projects/Assignment2$ ./a.out Please enter the size of your array: 6 Please enter 6 elements to populate the array. 212.3-6799 The elements of the array are: 212.3-6 7 99 The elements of the array sorted in ascending order: 6 0 27 12.3 99 The min of the array is-6 The max of the array is 99 The mean of the array is 19.05 The median of the array is 4.5 casey@vanderwaal:-/Dropbox/Teaching/CSCE240Fall2017/Projects/Assignment2$

Explanation / Answer

#include<iostream>

using namespace std;

void sort(double data[], int n){

   for (int i =0; i<n; i++){
       for (int j = i+1; j<n; j++){
          if (data[i] > data[j]){
             double temp = data[i];
             data[i] = data[j];
             data[j] = temp;
          }
       }
   }
}

void print(double data[], int n){
   for (int i = 0; i<n; i++){
        cout << data[i] << " ";
   }
   cout << endl;   
}

double min(double data[],int n){
   double min = data[0];
   for (int i = 0; i<n; i++){
      if (data[i] < min)
         min = data[i];
   }
   return min;
}

double max(double data[], int n){
   double max = data[0];
   for (int i = 0; i<n; i++){
      if (data[i] > max)
         max = data[i];
   }
   return max;
}

double mean(double data[], int n){
    double sum = 0;
   
    for (int i = 0; i<n; i++){
        sum = sum + data[i];
    }
    return sum/n;
}

double median(double data[], int n){
    double sum = 0;
    double median;
    if ( n% 2 == 0){
       int i = n/2-1;
       int j = n/2;
      median = (data[i] + data[j])/2;
    }
    else {
      median = data[n/2];
    }
    return median;
}


int main(){

   int n;
   cout << "Please enter size of array : ";
   cin >> n;
   double *data = new double[n];
   cout << "Please enter " << n << " elements to poulate the array" << endl;
   for (int i = 0; i<n; i++){
        cin >> data[i];
   }
   cout << "The elements of the array are:" << endl;
   print(data, n);
   cout << endl;

   cout << "The elements of the array sorted in ascending order:" << endl;
   sort(data,n);
   print(data, n);
   cout << endl;

   cout << "The min of the array is:" << min(data,n) << endl;
   cout << "The max of the array is:" << max(data,n) << endl;
   cout << "The mean of the array is:" << mean(data,n) << endl;
   cout << "The median of the array is:" << median(data,n) << endl;
   cout << endl;
  
   return 0;
}