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
|
#include <iostream>
#include <fstream>
using namespace std;
//Function Prototypes:
void printArray(int score[], int size);
bool myLess(int score,int value);
bool myGreater(int score, int value);
int compare(int score[], int size, bool (*comparison)(int,int));
bool prompt(int &choice);
//Main program:
int main()
{
bool run = true;
int score[100]={0},//Set first element to 0 and default the remaining to 0.
size=0, choice=0;
fstream infile("data.txt", ios::in);
while(!infile.eof()&&size<100)
{
infile>>score[size++];
infile.clear();
infile.ignore();
}
infile.close();
while(run)
{
while(prompt(choice)&&(choice<1||choice>7))//0 is also invalid
cout<<"Error, unavailable choice, try again\n";
switch(choice)
{
case 1:
cout<<"Highest score: "<<compare(score,size,myGreater)<<'\n';
break;
case 2:
cout<<"Lowest score: "<<compare(score,size,myLess)<<'\n';
break;
case 3:
case 4:
case 5:
cout<<"Not implemented.\n";
break;
case 6:
printArray(score,size);
break;
case 7:
run = false;
break;
}
}
return 0;
}
//Function Definitions:
bool prompt(int &choice)
{
cout << "\n**************************************\n";
cout << "\nWhat do you wish to do?\n";
cout << "1. Retrieve highest score\n";
cout << "2. Retrieve lowest score\n";
cout << "3. Calculate average\n";
cout << "4. Calculate median\n";
cout << "5. Calculate mode\n";
cout << "6. Print array\n";
cout << "7. Exit\n";
cout << "Choice: ";
cin>>choice;
cin.clear();
cin.ignore(1000,'\n');
return true;
}
int compare(int score[], int size, bool (*comparison)(int,int))
{
int value=score[0];
for(int i=1;i<size;++i)
if(comparison(score[i],value))
value=score[i];
return value;
}
bool myGreater(int score, int value)
{
return (score>value);
}
bool myLess(int score,int value)
{
return (score<value);
}
void printArray(int score[], int size)
{
for(int i=0;i<size;++i)
cout<<score[i]<<' ';
cout<<'\n';
}
|
**************************************
What do you wish to do?
1. Retrieve highest score
2. Retrieve lowest score
3. Calculate average
4. Calculate median
5. Calculate mode
6. Print array
7. Exit
Choice: 6
25 64 35 0 88 0 41 0 1 2 8 1 6 9 4 8 0
**************************************
What do you wish to do?
1. Retrieve highest score
2. Retrieve lowest score
3. Calculate average
4. Calculate median
5. Calculate mode
6. Print array
7. Exit
Choice: 7
Process returned 0 (0x0) execution time : 6.783 s
Press any key to continue. |