1 2 3 4 5 6 7 8
|
int main()
{
const int array_size = 4;
struct records rec_array[4];
for (int i = 0; i < array_size; ++i) { rec_array[i].rec[i] = i; }
sort_records(rec_array, array_size);
return 0;
}
|
Do you see "print" anywhere in that? I don't.
Your program has awfully many
4
in it, even though it does have
array_size
too. Furthermore, you have
coincidences that make difficult to separate logically distinct things.
One record of yours has actually
8 integers. You do set the value of only one of them. You do print the value of only one of them.
In a sort one usually uses two suboperations:
bool comp( const record & lhs, const record & rhs );
Returns true, if lhs is before rhs, i.e. one does not need to change the order of lhs and rhs. All you have to do, is to implement this comp() so that it compares one menber, say 'rec3'.
void swap( record & lhs, record & rhs );
This will exchange the values of lhs and rhs.
The standard library does have
std::swap()
that does work for
record
.
Write the sorting routine using the comp() and the std::swap().
Initialize all members of each record.
Show all members of each record.
Show the entire array both before and after sorting so that you can see what the sorting did do.