//
// This project asks the user for a series of grade.
// Then, it will calculate the grade average, standard Deviation
// Then it prints out how many grades the user entered,
// The average, standard deviation, max, and the min of the grades.
// The program will exit if the user enters -1, numbers that are not
// Between 0 and 100 or the user entered more than 100 numbers
//

#include<iostream>
#include<cassert>
#include<math.h>
using namespace std;

void get_grades(double a[], int size, int& numnberUsed);
void sort_grades(double a[], int& numberUsed);
void comp_stats(double a[], int& numberUsed);

//This is the main method
// It prints out the grade in a sorting oder
// The grades will be print 10 per row

int main()
{

    double sampleArray[100]; // set the array with size 100
    int numberUsed;
    get_grades(sampleArray, 100, numberUsed); // passing the sampleArray, size, and number used into the function get_grade
    sort_grades(sampleArray, numberUsed);//passing sampleArray, and numberUsed into the function sort_grade

    cout<<"\n";

//Using a loop to print out the array
//Check the condition, if the reminder of the index equals to 0 go to the next line

    for(int index = 0; index < numberUsed; index++)
    {
        if(((index)%10)==0 && index !=0)
        cout<<"\n";
        cout<<sampleArray[index]<<" ";
    }
    cout<<"\n"<<"\n";

    comp_stats(sampleArray, numberUsed);

    system("PAUSE"); // uses to pause the command window when the program is executed

    return 0;
  }

//A void fuction that asks the user for a series of grade
//The parameters are sampleArray, 100, and numberUsed
// We are using the int& instead of int because it's a void function and not a returning function
//
void get_grades(double a[], int size, int& numberUsed)
{

    cout<<"\nEnter grade(-1 to end ):"
            <<"\n?";
        double next;
        int index = 0;
        cin>>next;

// If user enters something more than 100 or less than 0
// The program will terminate
//
    if((next>=100) || (next < -1 ))
    {
        cout<<"Error: number must be between 0 and 100\n "
                <<"Program will exit\n";
            system("PAUSE");
            exit(1);
    }
// The program will continue to loop until the user enters -1
// Two condition inside the loop
//
    else
        while(next != -1)
        {
            a[index]= next;
            index++;
            cout<<"?";
           cin>>next;

// If the user enters something that is below 0 or above 100
// The program terminates
//
        if((next>=100) || (next < -1 ))
        {
            cout<<"Error: number must be between 0 and 100\n"
                    <<"Program will exit\n";
                    system("PAUSE");
                    exit(1);

        }
// If the user enters more than 100 grades
// The program terminates
//
        if(index > size)
        {
            cout<< " Warning: maximum number of grades reached\n"
                    <<"Program will exit now\n";
                        exit(1);
        }


    }
//Determines how many numbers the use entered
//
        numberUsed = index;

}
// A void function that sorts the grade
// int& numberUsed holds the size that the user entered
//
void sort_grades(double a[], int& numberUsed)
{
        double temp;
        for(int index = 0; index < numberUsed-1; index++)
        {
            for(int j = (index + 1); j < numberUsed; j ++)
            {
                if(a[index] > a[j])
                {
                    temp = a[index];
                    a[index] = a[j];
                    a[j] = temp;
                }
            }

    }
}
// This is a void function that computes the mean, max, min, and standard deviation
//
void comp_stats(double a[], int& numberUsed)
{
        double sum = 0, total = 0;
        int index=0;
        double max, ave, std, sqstd;
        int startIndex = 0;
        max = a[startIndex];
        int indexOfMax = startIndex;
        double dev[numberUsed];

        for (index = 0; index < numberUsed; index++)
        {
            if(a[index] > max)
            {
                max = a[index];
                indexOfMax = index;
            }

        sum = sum + a[index];

       }

        ave = static_cast<double>(sum)/numberUsed;

        for (index =0; index < numberUsed; index++)
        {
               dev[index] = ave - a[index];

        }
        for(index = 0; index < numberUsed; index++)
        {
            total = total + pow(dev[index], 2);

        }

    std = total / (numberUsed - 1);
    sqstd = sqrt(std);

    cout<<"Count = " << numberUsed << " Mean = " << ave
            <<" Std. Dev.= "<< sqstd<<" Max.= " <<a[indexOfMax] <<" Min.= " << a[startIndex]
            <<"\n";
}






 

< C++ >    < Home >