# Thread: sorting the matrix question..

1. ## sorting the matrix question..

how to sort the matrix so the sums of the rows
will go from the biggest sum on the bottom
to the smallest on top

like in this example:

http://img101.imageshack.us/img101/4580/79171873gv6.gif

2. Just like you would sort anything else. You can use bubblesort, quicksort, insertion sort, ..., ..., ..., ..., ....

3. in pseudo code
Code:
```for each column  {
for i = 0; i < Num of rows - 1; i++)  {
for j = i + 1; j < Num of rows; j++) {
if(Array[column][i] > Array[column][j]
swap Array[i][column] with Array[j][column]
//using a temp value as a holder

}
}
}```
This is a selection sort, and is not meant as actual code to paste into a program. You'll need to flesh it out
with real, not pseudo code, but it's pretty much there.

in pseudo code
Code:
```for each column  {
for i = 0; i < Num of rows - 1; i++)  {
for j = i + 1; j < Num of rows; j++) {
if(Array[column][i] > Array[column][j]
swap Array[i][column] with Array[j][column]
//using a temp value as a holder

}
}
}```
This is a selection sort, and is not meant as actual code to paste into a program. You'll need to flesh it out
with real, not pseudo code, but it's pretty much there.
Did you intentionally transpose column/(j,i) around between the comparison and the swap?

And the original post asks for the "sum of the row" to be sorted- which means that there is a need to first of all calculate the sum of the rows.

--
Mats

5. I just glanced at his jpg and thought he was trying to do a column sort.

My intent was that he notice that since it's a square matrix, you could swap column and row, but I'm not sure that will fly, here.

The craft of writing good pseudo code eludes me. generally. It's either too general, so the specifics are not to be relied on, or it's too specific, and then it is code. The latter is usually quite close, the former is never quite right, and I frequently find it a happy spot in the trash bin.

6. i need to do row sort
and the matrix doesnt have to be square

7. Consider each row as it's own string:

Code:
```for each row

sort each cell in that row

which might look like:

for(r = 0; r < MaxRows; r++)  {
for(i = 0; i < MaxCols - 1; i++)  {
for(j = i + 1; j < MaxCols; j++)  {
if(A[r][i] > A[r][j]  {
temp = A[r][i];
A[r][i] = A[r][j];
A[r][j] = temp;
}
}
}
}```
The red code handles the rows, the blue part is the standard selection sort for a single dimension array.

Edit:
Your picture you linked to in your original post, does not show an array that is row sorted. It shows an array that has been sorted according to the sum of it's rows. That is not what the code above, will do. The above code will move all the lower numbers for each row, to the left position in that row, in ascending order.

To do a row-sum sort, you will compare only each row's sum, and when they are out of order, with another row, you'll swap the two rows - including all the column values, all the way across the rows.

For each row: compute a sum. Put the value in a small sum[] array, of one dimension.
Now you can do your sorting. The slick way to do this is to use an index, and then you show the sorted array by printing it up in the order of the index.

I am not going to show that, because it's above your coding experience, just yet. For now, just know that there is a sort through an index, that involves making no actual swaps in the array at all, but will show the array *as if* it was sorted, in perfect order.

Code:
```for(i = 0; i < MaxElementsofSum[] - 1; i++)  {
for(j = i + 1; j < MaxElementsofSum[]; j++)  {
if(Sum[i] > Sum[j])   {
temp = Sum[i];
Sum[i] = Sum[j];
Sum[j] = temp;

//now swap the rows elements, from row j to row i
for(k = 0; k < MaxColumnsofMatrix[]; k++)   {
temp = Matrix[i][k];
Matrix[i][k] = Matrix[j][k];
Matrix[j][k] = temp;
}
}
}
}```
I haven't run this code, but it looks OK. Isn't that style of indentation (K&R), easy to read? I liiiiiike it!

8. i created an array rows_sum which collects the sums of each row

what to do now

Code:
```#include <stdio.h>
int main(){//star
int rows,cols,jndex,input;
int sum2=0;
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
int matrix[rows][cols];
int sum[rows][cols];
int temp[rows][cols];
int transpose [cols][rows];
int index,kndex,tndex,gndex,lndex;
int rows_sum[rows];
printf("enter power:");
scanf("%d",&input);
for (index = 0; index < rows; index++)
{
rows_sum[index]=0;
for (kndex = 0; kndex < cols; kndex++)
{
matrix[index][kndex] = 0;
sum[index][kndex] = 0;
temp[index][kndex] = 0;
transpose[index][kndex] = 0;
}//end inner for
}//end outer for
printf("enter numbers in a row for a matrix:"); //stat input
for (index = rows - 1;index >= 0; index--)
{
for (kndex = cols - 1; kndex >= 0; kndex--)
{
scanf("%d", &matrix[index][kndex]);
transpose[kndex][index] = matrix[index][kndex];
}
}
getchar();  //needed because of scanf()
//end input
//start power operation
//  rows sum
for (index = 0; index < rows; index++)
{
for (kndex = 0; kndex < cols; kndex++)
rows_sum[index]=rows_sum[index]+matrix[index][kndex];
printf("\n");
}
//  end rows sum
//start print
for (index = 0; index < rows; index++)
{
for (kndex = 0; kndex < cols; kndex++)
printf("%d ", sum[index][kndex]);
printf("\n");
}

//end print
printf("\n");
return 0;
}//end main func```

9. When you want to sort the rows, according to their sums, just copy and paste my code into your program, and check that the Matrix() and the Sums[] arrays in your program, and in my block of code, have the same names.

My block of code, should not go inside any loops in your program, of course. Then check that the variables I used: i, j, temp, etc., are declared in your program.

When you want to then print out your matrix, just print it out the normal way, don't use the row_sums array to print out the sorted matrix - no need for that. The whole matrix is already in row-sum order.

10. I know that "sum[i]" is the sum of row "i"
what is purpose of this array MaxElementsofSum[] ??

and why you dont put any value in it when you construct a loop with it
Code:
```for(i = 0; i < MaxElementsofSum[] - 1; i++)  {        //there is no value inside the cols
for(j = i + 1; j < MaxElementsofSum[]; j++)  {    //what is the purpose of MaxElementsofSum
if(Sum[i] > Sum[j])   {
temp = Sum[i];
Sum[i] = Sum[j];
Sum[j] = temp;

//now swap the rows elements, from row j to row i
for(k = 0; k < MaxColumnsofMatrix[]; k++)   {
temp = Matrix[i][k];
Matrix[i][k] = Matrix[j][k];
Matrix[j][k] = temp;
}
}
}
}```

11. This is the index sort I referred to earlier. It doesn't sort the matrix at all, but sorts a small index, instead.

That index array can be used to print out the matrix, just like it was sorted.

Say that you have an array of three friends ages, and a corresponding array of their names:

Code:
```int ages[3] = { 30, 20, 40 };
char names[3][25] = {
{"Kevin"  },
{"Zoi" } };```
So Adam is 30, Kevin is 20, and Zoi is 40. Now we want to print out our friends, sorted by age, but not mess with changing the original age[] or names[] arrays.

First we need an index array, initialized in sorted order:
Code:
```int index[3];

//initialize it
for(i = 0; i < sizeOfIndex; i++)
index[i] = i;

//Now we sort, using that index:
for(i = 0; i < sizeOfIndex - 1; i++)  {

for(j = i + 1; j < sizeOfIndex; j++)  {

if(age[index[i]] > age[index[j]]  { //I'm comparing ages, through the index array
temp = index[i];
index[i] = index[j];             //only the index contents are swapped
index[j] = temp;
}
}
}

//now print out the data, in sorted order, by going through the index array.
putchar('\n');
for(i = 0; i < sizeOfIndex; i++)
printf( " Age: %d  Name: %s \n", age[index[i]], names[index[i]] );

//showing:

Age: 20  Name: Kevin
Age: 40  Name: Zoi

/*
Original arrays still are in their original order:

20 Kevin
40 Zoi

*/```

When you want to sort the rows, according to their sums, just copy and paste my code into your program, and check that the Matrix() and the Sums[] arrays in your program, and in my block of code, have the same names.

My block of code, should not go inside any loops in your program, of course. Then check that the variables I used: i, j, temp, etc., are declared in your program.

When you want to then print out your matrix, just print it out the normal way, don't use the row_sums array to print out the sorted matrix - no need for that. The whole matrix is already in row-sum order.
apparently i tried to understand the wrong code
what code you meant?

13. Originally Posted by transgalactic2
apparently i tried to understand the wrong code
what code you meant?
This code:
Code:
```for(i = 0; i < MaxElementsofSum[] - 1; i++)  {
for(j = i + 1; j < MaxElementsofSum[]; j++)  {
if(Sum[i] > Sum[j])   {
temp = Sum[i];
Sum[i] = Sum[j];
Sum[j] = temp;

//now swap the rows elements, from row j to row i
for(k = 0; k < MaxColumnsofMatrix[]; k++)   {
temp = Matrix[i][k];
Matrix[i][k] = Matrix[j][k];
Matrix[j][k] = temp;
}
}
}
}```
MaxElementsOfSum[] is the number of elements in the Sum[] array, which hold a sum in them. In your example
with a 4 X 4 matrix, you would have 4 elements in Sum[].

14. i tried to implement that
sum is an array which holds the sum of each row
i switched MaxColumnsofMatrix[] by "rows" (because rows is the number cells in the sum array)
but i get many error of this type

ex2.c(62) : error C2109: subscript requires array or pointer type
Code:
```#include <stdio.h>
int main(){//star
int rows,cols,jndex,input;
int sum2=0;
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
int matrix[rows][cols];
int sum[rows][cols];
int temp[rows][cols];
int transpose [cols][rows];
int index,kndex,tndex,gndex,lndex;
int rows_sum[rows];
printf("enter power:");
scanf("%d",&input);
for (index = 0; index < rows; index++)
{
rows_sum[index]=0;
for (kndex = 0; kndex < cols; kndex++)
{
matrix[index][kndex] = 0;
sum[index][kndex] = 0;
temp[index][kndex] = 0;
transpose[index][kndex] = 0;
}//end inner for
}//end outer for
printf("enter numbers in a row for a matrix:"); //stat input
for (index = rows - 1;index >= 0; index--)
{
for (kndex = cols - 1; kndex >= 0; kndex--)
{
scanf("%d", &matrix[index][kndex]);
transpose[kndex][index] = matrix[index][kndex];
}
}
getchar();  //needed because of scanf()
//end input
//start power operation
//  rows sum
for (index = 0; index < rows; index++)
{
for (kndex = 0; kndex < cols; kndex++)
rows_sum[index]=rows_sum[index]+matrix[index][kndex];
printf("\n");
}
//  end rows sum

int i,j,k,temp;
for(i = 0; i <rows - 1; i++)  {
for(j = i + 1; j <rows; j++)  {
if(rows_sum[i] >rows_sum[j])   {
temp = rows_sum[i];
rows_sum[i] = rows_sum[j];
rows_sum[j] = temp;

//now swap the rows elements, from row j to row i
for(k = 0; k <rows; k++)   {
temp = matrix[i][k];
matrix[i][k] = matrix[j][k];
matrix[j][k] = temp;
}
}
}
}```

15. So far as I can tell, line 62 is the penultimate closing brace. If you're going to remove lines so that we can't count, you'll have to tell us what line line 62 actually is.