# Thread: How to apply selection sort in a two-dimensional array

1. ## How to apply selection sort in a two-dimensional array

hi,

I am having a bit of a problem applying selection sort in a two -dimensional array..

trying to apply selection sort only to array[i][4] (only on the fourth column..

so if the array is so while not sorted.

Code:
```100 80 90 3
101 90 70 2
102 78 60 4```
it should become
Code:
```102 78 60 4
100 80 90 3
101 90 70 2```
Thank you

2. So when you run your sort, you'll need to swap all the columns in your array, not just the fourth one.

3. yes exactly

4. Unless you meant, sort the forth column. In that case I think the easiest thing to do is make a 1D array of ray[][4], sort it, then put it back in the values for that column.

5. but the thing is the values of the other columns will have to change accordingly

7. I get an error?

Code:
```#include <cstdlib>
#include <iostream>
#include <cmath>

#define width 5

void SelectionSort(double array[][5], int height);
using namespace std;

int main()
{
int height;
int i,j,lab,midterm,final,number;
float avglab,avgmid,avgfinal,totallab,totalmid,totalfinal;

cout<<"Number of students: ";
cin>>height;

cout<<"\n";

double array[height][5];

for(i=0;i<height;i++)
{
cin>>number;
array[i][0]=number;
}
cout<<"\n";
for(i=0;i<height;i++)
{
cout<<"Student ";printf("%.0f",array[i][0]);cout<<": ";
cin>>lab;
array[i][1]=lab;

}
cout<<"\n";
for(i=0;i<height;i++)
{
cout<<"Student ";printf("%.0f",array[i][0]);cout<<": ";
cin>>midterm;
array[i][2]=midterm;

}
cout<<"\n";
for(i=0;i<height;i++)
{
cout<<"Student ";printf("%.0f",array[i][0]);cout<<": ";
cin>>final;
array[i][3]=final;

}
cout<<"\n";
for(i=0;i<height;i++)
{
totallab+=array[i][1];
totalmid+=array[i][2];
totalfinal+=array[i][3];
}
avglab=totallab/height;
avgmid=totalmid/height;
avgfinal=totalfinal/height;
for(i=0;i<height;i++)
{
array[i][4]=array[i][1]*0.35+array[i][2]*0.25+array[i][3]*0.40;
}
SelectionSort(array[i][5]);
cout<<"\n----------------------------------------------";
for(i=0;i<height;i++)
{
cout<<"\n";

for(j=0;j<width;j++)
{
printf("%.0f",array[i][j]);
cout<<"\t";
}

}
cout<<"\n";
cout<<"\n\n";
cout<<"Average of Labs: "<<avglab;
cout<<"\nAverage of Midterms: "<<avgmid;
cout<<"\nAverage of Finals: "<<avgfinal;
cout<<"\n\n\n";

for(i=0;i<height;i++)
{
if(50>array[i][4])
{
cout<<"Student ";printf("%.0f",array[i][0]);cout<<" is ........ed!!\n";
}
}

return 0;

}
void SelectionSort(int array[][5], int height)
{
int i,j,min,temp;
for(i=0;i<height-1;i++)
{
min=i;
for(j=i+1;j<height;j++)
{
if(array[j][5]<array[min][5])
min=j;
}
temp=array[i][5];
array[i][5]=array[min][5];
array[min][5]=temp;
}
}```

8. Array index [5] does not exist. Don't use it. You should be checking array index [4], and when you swap, you should swap [0], [1], [2], [3], and [4].

9. What does this mean ?
error: cannot convert `double' to `double (*)[4]' for argument `1' to `void SelectionSort(double (*)[4], int)'|

10. You need to pass an array, not a single solitary number.

11. I thought i was doing that... how?

12. By saying
Code:
`selectionSort(array)`
Code:
`selectionSort(array[i][5])`

13. Originally Posted by tabstop
By saying
Code:
`selectionSort(array)`
Code:
`selectionSort(array[i][5])`
result:

Code:
```|7|error: too few arguments to function `void SelectionSort(double (*)[5], int)'|
|71|error: at this point in file|```

14. And you also need to pass the height along with.

15. Originally Posted by tabstop
And you also need to pass the height along with.
Code:
`SelectionSort(array, height);`
Code:
`|71|undefined reference to `SelectionSort(double (*) [5], int)'|`