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<string>
using namespace std;
void printArray(int *s,string *n, int *size);
void sortArray(int score[], string name[], int size);
void averageArray(int *s, int *size);
void sortName(string name[], int size);
int main()
{
int size;
int score[size];
string name[size];
float average;
cout << "Enter the amount of tests taken: " << endl; //Enter size
cin >> size;
for(int i = 0; i < size; i++) //Enter scores
{
cout << "Enter score and name: ";
cin >> score[i] >> name[i];
}
sortName(name, size); // sort names
sortArray(score, name, size); //Sort scores
cout << "Sorted Scores and names: " << endl;
printArray(score, name, &size); //display sorted scores
cout << "Average: ";
averageArray(score, &size); //Find average of scores
return 0;
}
void sortArray(int score[], string name[], int size)
{
int temp;
bool madeAswap;
do
{ madeAswap = false;
for(int count = 0; count < (size - 1); count++)
{
if(score[count] > score[count + 1] && name[count] > name[count + 1])
{
temp = score[count];
score[count] = score[count +1];
score[count + 1] = temp;
madeAswap = true;
}
}
} while(madeAswap);
}
void sortName(string name[], int size)
{
int startScan, minIndex;
string minValue;
for(startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = name[startScan];
for(int index = startScan + 1; index < size; index++)
{
if(name[index] < minValue)
{
minValue = name[index];
minIndex = index;
}
}
name[minIndex] = name[startScan];
name[startScan] = minValue;
}
}
void averageArray(int *s, int *size)
{
int sum;
float average;
for(int i = 0; i < *size; i++)
{
sum+=*(s + i);
}
average = sum / *size;
cout << average;
}
void printArray(int *s, string *n, int *size)
{
for(int i = 0; i < *size; i++)
{
cout << *(n + i) << *(s + i) << endl;
}
}
|