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
|
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void findstudentavg(int quiz1[], int quiz2[], int quiz3[], double avg[], int n)
{
for(int i = 0; i < n; i++)
avg[i] = (quiz1[i] + quiz2[i] + quiz3[i]) / 3.0;
}
int findlow(int array[], int n)
{
int low = array[0];
for(int i = 1; i < n; i++)
if(array[i] < low)
low = array[i];
return low;
}
int findhigh(int array[], int n)
{
int high = array[0];
for(int i = 1; i < n; i++)
if(array[i] > high)
high = array[i];
return high;
}
double findqzavg(int quiz[], int n)
{
double sum = 0.0;
for(int i = 0; i < n; i++)
sum += quiz[i];
return sum / n;
}
int getdata(ifstream &fin, int studentID[], int quiz1[], int quiz2[], int quiz3[])
{
int count = 0;
while(true)
{
fin>>studentID[count];
if(studentID[count] == 0)
return count;
fin>>quiz1[count]>>quiz2[count]>>quiz3[count];
count++;
}
}
void printall(ofstream &fout, int studentID[], int quiz1[], int quiz2[], int quiz3[], double avg[], int n)
{
for(int i = 0; i < n; i++)
fout<<studentID[i]<<"\t"<<quiz1[i]<<"\t"<<quiz2[i]<<"\t"<<quiz3[i]<<"\t"<<fixed<<setprecision(2)<<avg[i]<<endl;
fout<<"High Score : "<<findhigh(quiz1, n)<<"\t"<<findhigh(quiz2, n)<<"\t"<<findhigh(quiz3, n)<<endl;
fout<<"Low Score : "<<findlow(quiz1, n)<<"\t"<<findlow(quiz2, n)<<"\t"<<findlow(quiz3, n)<<endl;
fout<<"Quiz Average: "<<findqzavg(quiz1, n)<<"\t"<<findqzavg(quiz2, n)<<"\t"<<findqzavg(quiz3, n)<<endl;
}
int main()
{
ifstream fin;
ofstream fout;
fin.open("pr2data.txt");
fout.open("outData.txt");
if(!fin.is_open())
{
cout<<"Unable to open the file."<<endl;
return 0;
}
int studentID[30], quiz1[30], quiz2[30], quiz3[30];
double studentAvg[30];
int count = getdata(fin, studentID, quiz1, quiz2, quiz3);
findstudentavg(quiz1, quiz2, quiz3, studentAvg, count);
printall(fout, studentID, quiz1, quiz2, quiz3, studentAvg, count);
}
|