1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void sortArray(int array[], int size);
int findGrades(int iArray[], int & iMax, int & iMin, int & iSum, double & iAverage, double & iMedian);
const int iSize = 12;
int main ()
{
//delcare variables.
string sFirst, sLast, filename;
int iSum = 0;
double iAverage = 0.0;
int grades[iSize];
int iMax = 0;
int iMin = 0;
double iMedian = 0.0;
ifstream datafile;
datafile.open( "Lab13Data.txt" );
if (datafile.is_open())
{
cout << "Names" << " " << "Grades";
while (!datafile.eof())
{
datafile >> sFirst;
cout << endl << endl << sFirst << " ";
datafile >> sLast;
cout << sLast << " ";
for (int i = 1 ;i <= iSize ;i++) //done to get the show the array next to the names.
{
datafile >> grades[i];
if (i % 13 == 0) //separates the next set of students and their respective grades.
{
cout << endl;
}
cout << grades[i] << " ";
}
}
sortArray(grades, iSize);
findGrades(grades, iMax, iMin, iSum, iAverage, iMedian);
//print out results from formulas
cout << iMax << " is the largest value present.\n";
cout << iMin << " is the smallest value present.\n";
cout << iMedian << " is the median of all of the numbers.\n";
cout << iAverage << " is the average of all of the numbers.\n";
}
else
cout << "Unable to open file\n"; //returns an error message if the file could not open.
datafile.close();//closes file
}
//***********************************************************
// Definition of function sortArray *
// This function performs an ascending order bubble sort on *
// array. size is the number of elements in the array. *
//***********************************************************
void sortArray(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
int findGrades(int iArray[], int & iMax, int & iMin, int & iSum, double & iAverage, double & iMedian)
{
int index = 0;
for(iArray[index] >= iMax; index++;) //for loop used to find the maximum value
{
iMax = iArray[index];
}
for(iArray[index] <= iMin; index++;) //for loop used to find the minimum value
{
iMin = iArray[index];
}
for(iArray[index] <= iSum || iArray[index] >= iSum; index++;) //for loop used to sum up the int values
{
iSum += iArray[index];
}
iAverage = static_cast<double>(iSum)/iSize; //changed iSum to a double value so nothing would get truncated.
}
|