I need to write a function that sorts a two-dimensional array using this header:
void sort(int m[][2], int numberOfRows)
my code is supposed to primarily sort the rows and secondarily sort the columns. The program is supposed to take ten points entered by the user and sort them.
I cannot seem to get my code to compile and I am unsure why.
Here is what I have so far:
#include <iostream>
#include <cmath>
using namespace std;
void sort(int m[][2], int numberOfRows)
{
//Sort the rows first.
for (int i = 0; i < numberOfRows; i++)
{
//Find the minimum in the rows list.
int currentMinRows = m[i][0];
int currentMinIndexRows = i;
for (int j = i + 1; j < numberOfRows; j++)
{
if (currentMinRows > m[j][0])
{
currentMinRows = m[j][0];
currentMinIndexRows = j;
}
}
//Swap m[i][0] with m[currentMinIndexRows][0] if necessary.
if (currentMinIndexRows != i)
{
m[currentMinIndexRows][0] = m[i][0];
m[i][0] = currentMinRows;
}
}
//Sort the columns next.
for (int i = 0; i < numberOfRows; i++)
{
//Find the minimum in the columns list.
int currentMinColumns = m[i][1];
int currentMinIndexColumns = i;
for (int j = i + 1; j < numberOfRows; j++)
{
if (currentMinColumns > m[j][1])
{
currentMinColumns = m[j][1];
currentMinIndexColumns = j;
}
}
//Swap m[i][1] with m[currentMinIndexColumns][1] if necessary.
if (currentMinIndexColumns != i)
{
m[currentMinIndexColumns][1] = m[i][1];
m[i][1] = currentMinColumns;
}
}
}
int main()
{
cout << "Enter ten points in the format of x and y coordinates to be sorted: " << endl;
cout << "For example: 1 2 and push enter. next line 3 4 and push enter ect." << endl;
int m[10][2];
for (int i = 0; i < 10; i++)
{
cin >> m[i][0] >> m[i][1];
}
cout << "The points are sorted by row and then by column are as follows:" << endl;
cout << sort(m[][2],10) << endl;
return 0;
}
Any suggestions? Please, be nice. ;)