Friday, December 12, 2014

Math C++

  Write a C++ program that creates the following functions:

 double Max (double Values[], int size);

 double Min (double Values[], int size);

 double calc Average(double Values[], int size);

 double calc Median(double Values[], int size);

double calc Standard Deviation (double Values[], int size);

 Write a driver program that creates an array with 11 values and calculates all of the preceding values. Display the values to the user. Create a second array that has 10 values and display all of the preceding values.

Note: You do not need to get input from the user. Must use the previous functions prototype.





#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

double calcMax (int arg[], int length)
{
    int maxValue = 0;
    for(int i = 0; i < length; i++)
        {
        if(arg[i] > maxValue)
        {
            maxValue = arg[i];
        }

        }
         cout << "The highest value is: " << maxValue<<endl;

}
double calcMin (int arg[], int length)
{
    int minValue = 1000;
    for(int i = 0; i < length; i++)
        {
        if(arg[i] < minValue)
        {
            minValue = arg[i];
        }

        }
        cout << "The minimum value is: " << minValue<<endl;

}
double calcAverage (int arg[], int length)
{
int sum=0;
for (int i=0; i<length; i++)
sum += arg[i];

cout << "The average value is: " << sum/length<<endl;


}
double calcMedian (int arg[], int length)
{
    sort(arg, arg + length);
    cout << "The new sorted array looks like this." << endl;
    cout << "=====================================" << endl;
    for (size_t i = 0; i != length; ++i)
        cout << arg[i] << " ";
{    double* dpSorted = new double[length];
    for (int i = 0; i < length; ++i) {
        dpSorted[i] = arg[i];
    }
    for (int i = length - 1; i > 0; --i) {
        for (int j = 0; j < i; ++j) {
            if (dpSorted[j] > dpSorted[j+1]) {
                double dTemp = dpSorted[j];
                dpSorted[j] = dpSorted[j+1];
                dpSorted[j+1] = dTemp;
            }
        }
    }

        double dMedian = 0.0;
    if ((length % 2) == 0) {
        dMedian = (dpSorted[length/2] + dpSorted[(length/2) - 1])/2.0;
    } else {
        dMedian = dpSorted[length/2];
    }
    delete [] dpSorted;
     cout << "                              " << endl;
     cout << "The median value is: " << dMedian<<endl;
 }
 }
double calcStandardDev (int arg[], int length)
{
double deviation;
double sum2;
double mean;
for ( int i = 0; i <=length; i++ )
{
sum2 += pow((arg[i]-mean),2);
}
deviation= sqrt(sum2/(length-1));
cout << "The Standard Deviation is : " <<deviation<<endl;
cout << "                              " << endl;
cout << "                              " << endl;
cout << "                              " << endl;
cout << "                              " << endl;

}

int main ()
{

    int number1 [] = {1,2,3,4,25,6,7,8,9,11,12};
    calcMax(number1, 11);
    calcMin(number1, 11);
    calcAverage(number1,11);
    calcMedian(number1, 11);
    calcStandardDev(number1,11);
    int number2 [] ={10,9,8,7,6,5,4,3,2,1,11};
    calcMax(number2, 10);
    calcMin(number2, 10);
    calcAverage(number2,10);
    calcMedian(number2, 10);
    calcStandardDev(number2,10);

  return 0;
}

No comments:

Post a Comment