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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
// C++ Program
// This program can show the average score for each student
// Date 11/05/2015
// *************************************************************
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
// function prototype
double calcAverage (double [], int);
double calcMedian (double [], int);
void selectionSort (double [], int);
int main()
{
ifstream infile;
string filename;
const int MAX_SCORES = 10;
const int MAX_STUDENTS = 12;
string stuName[MAX_STUDENTS];
double final[MAX_STUDENTS];
double scores[MAX_SCORES];
double classAverage;
double classMedian;
double sort;
int count = 0; // loop counter
// open file
cout << "Enter filename: ";
cin >> filename;
infile.open(filename.c_str());
cout << "\n";
if(infile)
{
while (infile >> stuName[count])
{
for(int i = 0; i < MAX_SCORES; i++)
{
infile >> scores[i];
}
final[count] = calcAverage(scores, MAX_SCORES); // store average to fianl array
count++;
}
cout << "--------------------------\n";
cout << setw(8) << "Name" << "\t" << "Average\n";
cout << "--------------------------\n";
for(int k = 0; k < MAX_STUDENTS; k++)
{
cout << setw(8) << stuName[k] << "\t" << final[k] << endl;
}
cout << "--------------------------\n\n";
cout << "**************************\n\n";
selectionSort (final, MAX_STUDENTS);
cout << "Following is the sorted average:\n";
for(int i = 0; i < MAX_STUDENTS; i++)
{
cout << setprecision(3) << final[i] << " ";
}
classAverage = calcAverage(final, MAX_STUDENTS);
cout << "\nThe class average is " << setprecision(3) << classAverage << endl;
classMedian = calcMedian(final, MAX_STUDENTS);
cout << "The class median is " << classMedian << endl;
cout << "**************************\n\n";
}
else // display a error message if file does not open
{
cout << "Error! Please enter correct filename with file type";
cout << "such as lab5.txt\n";
}
return 0;
}
double calcAverage(double array[], int MAX)
{
double total = 0;
for(int i = 0; i < MAX; i++)
{
total += array[i];
}
return (total / MAX);
}
double calcMedian(double array[], int MAX)
{
int a, b;
double median;
selectionSort (array, MAX);
if(MAX % 2)
{
a = MAX / 2;
median = array[a];
}
else
{
a = MAX / 2;
b = a - 1;
median = (array[a] + array[b]) / 2;
}
return median;
}
void selectionSort(double array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if(array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
|