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
|
// compiler directives
#include <iostream>
#include<iomanip>
#include<stdio.h>
#include <algorithm>//added
#include <utility>
using namespace std;
// constant identifiers
const int MAXSIZE = 2;
// function prototypes
void GetData(std::pair<int,int> TestScores[ ]);
void ComputeAvg(std::pair<int,int> TestScores[ ],float &Avgf);
void PrintReport(std::pair<int,int> TestScores[ ],float Avgf);
bool pred(std::pair<int,int> i, std::pair<int,int> j);// needed to sort
// main program
int main()
{
// local identifiers
std::pair<int,int> TestScores[MAXSIZE];
float Avg = 0;
//Input modale
GetData(TestScores);
//processing module
ComputeAvg(TestScores,Avg);
//output module
PrintReport(TestScores,Avg);
std::pair<int,float> h[10];
system ("PAUSE");
return 0 ;
}//end main
// function definitions
void GetData (std::pair<int,int> TestScores[ ])
{
for (int i=0; i <MAXSIZE; i++)
{
int t;
cout<<"please enter the test score #"<<i+1<<":\t";
cin>>t;
TestScores[i] = std::pair<int,int>(i+1,t);
}
return;
}
// end GetData
void ComputeAvg( std::pair<int,int> TestScores[ ],float&Avgf)
{
float sum = 0;
for(int i=0; i <MAXSIZE; i++)
{
sum = sum + TestScores[i].second;
}
Avgf =(float)(sum)/MAXSIZE;
return;
} //end ComputeAvg
void PrintReport (std::pair<int,int> TestScores[ ], float Avgf)
{
// std::sort algorithm function : sort a range of elements using the pred function to compare
std::sort(TestScores,TestScores+MAXSIZE,pred);
cout<<setw(23)<<"Test#"<<setw(15)<<"TestScore";
cout<<"\n\n";
for(int i = 0; i <MAXSIZE; i ++)
{
cout<<setw(26)<<TestScores[i].first<<setw(20)<<TestScores[i].second<<endl;
}
cout<<"\n\n"<<"test score average:\t"<<setprecision(3)<<Avgf<<endl;
}
bool pred(std::pair<int,int> i, std::pair<int,int> j)
{
return i.second < j.second;
}
|