0

I need to calculate the standard deviation for an array in my program, the array I need to calculate it for is the pgmavg and testavg everything else in my program works correctly.

```
/********************************************************************
CSCI 240 - Assignment 7 - Spring 2009
Progammer: Justin R. Smith
Section: 9
TA: Pooja Uppalapati
Date Due: March 26, 2009
Purpose: The program reads in data from a averages file and prints out
the unsorted data information and uses functions to fill arrays
and prints out the sorted data information.
*********************************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
int buildAr(int [], double [], double []); //function prototype
void displayAr(int [], double [], double [], int); //fuction prototype
void sortAr (int [], double [], double [], int, char); //function prototype
void calcStatByRef (double [], int, double&, double&, double&, double&);
double sqrt( double );
#define ARSIZE 20 //symbolic constant
int main()
{
int size; //declare size as an int
int sub = 0; //declare sub as an int sets equal to zero
int zid[ARSIZE]; //declares the array zid using symbolic constant
double pgmavg[ARSIZE]; //declares the array prgavg using symbolic constant
double testavg[ARSIZE]; //declares the array testavg using symbolic constant
double minval, valmax, avg, dev;
buildAr (zid, pgmavg, testavg); //calls the buildAr function
size = buildAr (zid, pgmavg, testavg); //sets size to the buildAr function
//displays title for sorted student information
cout << endl << "The SORTED student information" << endl;
cout << "----------------------------------------------------------------"
"-------" << endl;
char sortchar = 'i';
sortAr (zid, pgmavg, testavg, size, sortchar); //calls the sortAr function
displayAr (zid, pgmavg, testavg, size); //calls the displayAr fucntion
sortchar = 'p';
sortAr (zid, pgmavg, testavg, size, sortchar); //calls the sortAr function
displayAr (zid, pgmavg, testavg, size); //calls the displayAr fucntion
calcStatByRef(pgmavg, size, minval, valmax, avg, dev);
cout << endl << "The minimum value of the program averages is: " << minval <<endl;
cout << "The maximum value of the program averages is: " << valmax << endl;
cout << "The average value of the program averages is: " << avg << endl << endl;
cout << dev;
sortchar = 't';
sortAr (zid, pgmavg, testavg, size, sortchar); //calls the sortAr function
displayAr (zid, pgmavg, testavg, size); //calls the displayAr fucntion
calcStatByRef(testavg, size, minval, valmax, avg, dev);
cout << endl << "The minimum value of the test averages is: " << minval <<endl;
cout << "The maximum value of the test averages is: " << valmax << endl;
cout << "The average value of the test averages is: " << avg << endl << endl;
system ("pause");
return 0;
}
/**********************************************************
Function: int buildAr
Use: Builds each of the arrays and returns the subscript
of them to int main().
Arguments: 1. int zid[] array that holds student zid's
2. double prgavg[] array that holds studnets program
averages
3. int testavg[] array that holds students test
averages
Returns: sub which is used for the subscript for the array's
**********************************************************/
int buildAr (int zid [], double pgmavg [], double testavg [])
{
int num;
double num2, num3;
int sub = 0;
ifstream inFile;
inFile.open( "averages.txt" );
if ( inFile.fail() )
{
cout << "input file did not open";
exit(0);
}
while ( inFile )
{
inFile >> num;
zid[sub] = num;
inFile >> num2;
pgmavg[sub] = num2;
inFile >> num3;
testavg[sub] = num3;
sub++;
}
inFile.close();
return (sub);
}
/**********************************************************
Function: void displayAr
Use: Displays the unsorted and sorted grades when called
by int main().
Arguments: 1. int zid[] array that holds student zid's
2. double prgavg[] array that holds studnets program
averages
3. int testavg[] array that holds students test
averages
4. int size which holds the buildAr fucntion from
int main()
Returns: nothing
**********************************************************/
void displayAr(int zid [], double pgmavg [], double testavg [], int size)
{
int sub = 0;
double overall;
char sortchar;
cout << "Student ID Program Average Test Average "<<
"Overall Average" << endl;
for(sub = 0; sub < size; sub++)
{
overall = (pgmavg[sub] * .4) + (testavg[sub] * .6);
cout << " " << zid[sub] << " " << setw(6) << fixed <<
setprecision(2) << pgmavg[sub] << " " << setw(6) << fixed <<
setprecision(2) << testavg[sub] << " " << setw(4) << fixed
<< setprecision(2) << overall << endl;
}
}
/**********************************************************
Function: void sortAr
Use: Organizes the output given by the function display
in ascending order.
Arguments: 1. int zid[] array that holds student zid's
2. double prgavg[] array that holds studnets program
averages
3. int testavg[] array that holds students test
averages
4. int size which holds the buildAr fucntion from
int main()
Returns: nothing
**********************************************************/
void sortAr(int zid [], double pgmavg [], double testavg [], int size, char sortchar)
{
int i, j, minat;
for(i = 0; i<(size); i++)
{
minat = i;
for(j = i+1; j < size; j++)
{
if (sortchar == 'i' || sortchar == 'I')
{
if(zid[minat] > zid[j])
{
minat = j;
}
}
else if (sortchar == 'p' || sortchar == 'P')
{
if(pgmavg[minat] > pgmavg[j])
{
minat = j;
}
}
else if (sortchar == 't' || sortchar == 'T')
{
if(testavg[minat] > testavg[j])
{
minat = j;
}
}
}
double temp = zid[i];
zid[i] = zid[minat];
zid[minat]=temp;
temp = pgmavg[i];
pgmavg[i] = pgmavg[minat];
pgmavg[minat] = temp;
temp = testavg[i];
testavg[i] = testavg[minat];
testavg[minat] = temp;
}
}
/*******************************************************************/
void calcStatByRef( double average[], int size, double &m, double &v, double &a, double &d )
{
double sum;
m = average[0];
v = average[size-1];
for (int i = 0; i < size; i++)
sum += average[i];
a = sum/size;
for (int i = 0; i < size; i++)
d = sqrt(average[i*i] - (sum*sum)-(size))/(size-1);
}
```