Sorting arrays

Hello,I need help sorting this array. I dont know where to start.

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
#include <iostream>
#include <iomanip>
using namespace std;
const int NUMBER_STUDENTS = 200, MIN_Numb = 1,ID=500;
const int MAX_SCORE = 100, MIN_SCORE = 1;

void randomInitArray(int grade[][MIN_Numb], int min, int max);
void computeStAve(const int grade[][MIN_Numb], double StAve[]);
void display(const int grade[][MIN_Numb], const double StAve[]);

int main(){

	int grade[NUMBER_STUDENTS][MIN_Numb];
	double StAve[NUMBER_STUDENTS];

	srand(time(NULL));

	randomInitArray(grade, MIN_SCORE, MAX_SCORE);
	computeStAve(grade, StAve);

	display(grade, StAve);

	

	system("pause");
	return 0;

}
void randomInitArray(int grade[][MIN_Numb], int min, int max){
	for (int stNum = 1; stNum <= NUMBER_STUDENTS; stNum++)
	{
		for (int randNumb = 1; randNumb <= MIN_Numb; randNumb++)
		{
			grade[stNum - 1][randNumb - 1] = rand() % max + min;
		}
	}
}
void computeStAve(const int grade[][MIN_Numb], double StAve[]){
	for (int stNum = 1; stNum <= NUMBER_STUDENTS; stNum++){
		int sum = 0;
		for (int randNumb = 1; randNumb <= MIN_Numb; randNumb++){
			sum = sum + grade[stNum - 1][randNumb - 1];
		}
		StAve[stNum - 1] = (double)sum / MIN_Numb;
	}
}
void display(const int grade[][MIN_Numb], const double StAve[]){

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout << setw(10) << "Student ID"

		<< setw(10) << "Score\n";
	for (int stNum = 1; stNum <= NUMBER_STUDENTS; stNum++){
		cout << setw(10) << stNum
			<< setw(10) << StAve[stNum - 1] << " ";
		for (int randNumb = 1; randNumb <= MIN_Numb; randNumb++){
			
		}
		cout << endl;
	}
}

  
#include <algorithm> std::sort( std::begin(StAve), std::end(StAve) );
http://www.cplusplus.com/reference/algorithm/sort/
Hope this helps.
How would I get it to display with sorted Score and then Sorted ID? I have to display three times.
If you click the link above, you'll see that std::sort accepts a third parameter - this parameter is a functor that takes two of the objects being sorted by const reference and returns whether the first comes before the second. You can use it to decide what to base your sort order on.

If you want to preserve the sort order from a previous sort when the new sort deems two elements as equal, use std::stable_sort.
But i have to use something like this. Thats where I'm confused. I don't know where to input the integers.

1
2
3
4
5
6
7
8
9
10
11
12
void selectionSort(int a[ ], int n) {
int i, k, indexOfNextSmallest, temp;
for (i = 0; i < n - 1; i++){
indexOfNextSmallest = i;
for (k=i+1; k<n; k++)
if (a[k] < a[indexOfNextSmallest])
indexOfNextSmallest = k;
temp = a[i];
a[i] = a[indexOfNextSmallest];
a[indexOfNextSmallest] = temp;
}
} 
Are you required to use your own sorting function?
Yes I have to
I haven't had to write a sorting function ever, so you'll need to consult your textbook and/or the internet to discover how to implement selection sort.
I keep forgetting that exists, thanks Duoas!
Topic archived. No new replies allowed.