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 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include<iostream>
#include<fstream>
using namespace std;
ifstream infile("IDSAT.dat");
const int value=25;
void readdata(int&, int[], int[]);
void printarrs(int, int[], int[]);
void lsort(int, int[], int[]);
void printarrs(int, int[], int[]);
void bsort(int, int[], int[]);
void printarrs(int, int[], int[]);
double mean(int, int[]);
void findlimits(int [], int, int&, int&);
int search(int[], int, int);
int main()
{
int n=0, ID[value], scores[value], max, min, numb, pos;
double average;
readdata(n, ID, scores);
printarrs(n ,ID, scores);
lsort(n, ID, scores);
printarrs(n, ID, scores);
bsort(n, ID, scores);
printarrs(n, ID, scores);
average=mean(n, scores);
cout<<"The average of the SAT scores is: "<<average<<endl;
findlimits(scores, n, max, min);
cout<<"Max is: "<<max<<" and Min is: "<<min<<endl;
cout<<"Please enter a score value to find: ";
cin>>numb;
pos=search(scores, n, numb);
if(pos!=-1)
cout<<numb<<" found in position "<<pos<<endl;
else
cout<<numb<<" not found"<<endl;
infile.close();
system("pause");
return 0;
}
//Reads the data into the 2 functions using a parallel array.
void readdata(int& n, int ID[], int scores[]){
for(int i=0; i<10; i++){
infile>>ID[i];
infile>>scores[i];
n++;
}
return;
}
////Prints the values stored in the arrays.
void printarrs(int n, int ID[], int scores[]){
cout<<"Original Set of Data"<<endl;
cout<<"ID"<<" "<<"SAT score"<<endl;
for(int i=0; i<n; i++)
cout<<ID[i]<<" "<<scores[i]<<endl;
cout<<endl;
return;
}
//sorts ID numbers in descending order and syncs the SAT nnumbers with the ID array
void lsort(int n, int ID[], int scores[]){
int j, temp, temp2;
for (int i=1; i<n; i++) {
j=i;
while (j > 0 && ID[j - 1] < ID[j]) {
temp = ID[j];
temp2=scores[j];
ID[j] = ID[j - 1];
scores[j]=scores[j-1];
ID[j - 1] = temp;
scores[j-1]=temp2;
j--;
}
}
return;
}
//Sorts the SAT scores into ascending order while syncing the ID scores with the SAT scores
void bsort(int n, int ID[], int scores[]) {
bool swapped = true;
int j = 0, temp, temp2;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++) {
if(scores[i]>scores[i+1]) {
temp=scores[i];
temp2=ID[i];
scores[i]=scores[i+1];
ID[i]=ID[i+1];
scores[i + 1]=temp;
ID[i+1]=temp2;
swapped = true;
}
}
}
return;
}
//Finds the average of the SAT scores.
double mean(int n, int scores[]){
int sum=0;
for (int i = 0; i < n; ++i)
sum+=scores[i];
return sum/n;
}
//Finds the maximum and minimum score of the SAT results.
void findlimits(int scores[], int n, int& max, int& min){
max=scores[n-1];
min=scores[0];
return;
}
//Searches for the number inputed.
int search(int scores, int n, int numb){
for(int i=0; i<n; i++)
if(scores[i]==numb)
return i;
return -1;
}
|