# Thread: sorting the matrix question..

1. i changed it to

Code:
```

#include <stdio.h>

int main()   { //star
int i,j,k,temp;
int rows = 4;
int cols = 4;
int jndex,input;
//	int sum2=0;  sum2 is never used

/* You can't make an array without telling it's size. rows and cols
have not been assigned a value, yet.
*/
int matrix[50][50];
//	int matrix[rows][cols];
//	int sum[rows][cols];
int sum[50][50];
//	int temp1[rows][cols];
int temp1[50][50];
//	int transpose [cols][rows];
int transpose [50][50];
int index,kndex,tndex,gndex,lndex;
int rows_sum[4];
//int rows_sum[rows]; //rows could be ANY value, still.

/* your program isn't designed for this
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
*/
printf("enter power:");
scanf("%d",&input);

/* You can save yourself this code, just by making the array initialize to
zero's, when you declare it:

row_sum[4] = { 0 };

This only works when you first declare them, not at any later time
*/
for (index = 0; index < rows; index++)
{
rows_sum[index]=0;
for (kndex = 0; kndex < cols; kndex++)
{
matrix[index][kndex] = 0;
sum[index][kndex] = 0;
temp1[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];
}
}
i = 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

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;
}  //you missed this brace :)
//now swap the rows elements, from row j to row i
for(k = 0; k <rows; k++)   {
temp = matrix[i][k];                                      //here is line 62
matrix[i][k] = matrix[j][k];
matrix[j][k] = temp;
}
}
}

//print the swapped matrix
for(i = 0; i < rows; i++)  {
putchar('\n');
for(j = 0; j < cols; j++)
printf(" %d", matrix[i][j]);
}

i = getchar();
return 0;

}//end main```

2. it works

how to replace the getch();
i was told that this command is not studied in the course

Code:
```

#include <stdio.h>

int main()   { //star
int i,j,k,temp;
int rows = 4;
int cols = 4;
int jndex,input;
//	int sum2=0;  sum2 is never used

/* You can't make an array without telling it's size. rows and cols
have not been assigned a value, yet.
*/
int matrix[50][50];
//	int matrix[rows][cols];
//	int sum[rows][cols];
int sum[50][50];
//	int temp1[rows][cols];
int temp1[50][50];
//	int transpose [cols][rows];
int transpose [50][50];
int index,kndex,tndex,gndex,lndex;
int rows_sum[4];
//int rows_sum[rows]; //rows could be ANY value, still.

/* your program isn't designed for this
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
*/

/* You can save yourself this code, just by making the array initialize to
zero's, when you declare it:

row_sum[4] = { 0 };

This only works when you first declare them, not at any later time
*/
printf("enter the size of a  matrix:");
scanf("%d %d",&rows,&cols);
for (index = 0; index < rows; index++)
{
rows_sum[index]=0;
for (kndex = 0; kndex < cols; kndex++)
{
matrix[index][kndex] = 0;
sum[index][kndex] = 0;
temp1[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];
}
}
i = 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("%d ",matrix[index][kndex]);
}
printf("\n");
}
//  end rows sum

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;
}  //you missed this brace :)
//now swap the rows elements, from row j to row i
for(k = 0; k <rows; k++)   {
temp = matrix[i][k];                                      //here is line 62
matrix[i][k] = matrix[j][k];
matrix[j][k] = temp;
}
}
}

//print the swapped matrix
for(i = 0; i < rows; i++)  {
putchar('\n');
for(j = 0; j < cols; j++)
printf(" %d", matrix[i][j]);
}

i = getchar();
return 0;

}//end main```

3. ahh never mind
its getchar in the code

4. what to do in a case where the sum of two rows is equal??

5. Equals are usually left where they are when sorting. Nothing to be gained by swapping them, and some advantage to keeping them in their original order, if they're equal.

6. i need to stick the equal rows together
one next to the the other

if the matrix is
1 1 1 1
9 9 9 9
0 0 0 0
9 9 9 9

then it has to be sorted to
0 0 0 0
1 1 1 1
9 9 9 9
9 9 9 9

??

7. i dont need to equal sum case

but its sorting regularily

Code:
```#include <stdio.h>

int main()   { //star
int i,j,k,temp;
int rows = 50;
int cols = 50;
int jndex,input;
//	int sum2=0;  sum2 is never used

/* You can't make an array without telling it's size. rows and cols
have not been assigned a value, yet.
*/
int matrix[50][50];
//	int matrix[rows][cols];
//	int sum[rows][cols];
int sum[50][50];
//	int temp1[rows][cols];
int temp1[50][50];
//	int transpose [cols][rows];
int transpose [50][50];
int index,kndex,tndex,gndex,lndex;
int rows_sum[50];
//int rows_sum[rows]; //rows could be ANY value, still.

/* your program isn't designed for this
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
*/

/* You can save yourself this code, just by making the array initialize to
zero's, when you declare it:

row_sum[4] = { 0 };

This only works when you first declare them, not at any later time
*/
printf("enter the size of a  matrix:");
scanf("%d %d",&rows,&cols);
for (index = 0; index < rows; index++)
{
rows_sum[index]=0;
for (kndex = 0; kndex < cols; kndex++)
{
matrix[index][kndex] = 0;
sum[index][kndex] = 0;
temp1[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];
}
}
i = 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("%d ",matrix[index][kndex]);
}
printf("\n");
}
//  end rows sum

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;
}  //you missed this brace :)
//now swap the rows elements, from row j to row i
for(k = 0; k <rows; k++)   {
temp = matrix[i][k];                                      //here is line 62
matrix[i][k] = matrix[j][k];
matrix[j][k] = temp;
}
}
}

//print the swapped matrix
for(i = 0; i < rows; i++)  {
putchar('\n');
for(j = 0; j < cols; j++)
printf(" %d", matrix[i][j]);
}

i = getchar();
return 0;

}//end main```

8. OK, it's fixed.

Code:
```#include <stdio.h>

int main()   { //star
int i,j,k,temp;
int rows = 50;
int cols = 50;
int jndex,input;
//	int sum2=0;  sum2 is never used

/* You can't make an array without telling it's size. rows and cols
have not been assigned a value, yet.
*/
int matrix[50][50];
//	int matrix[rows][cols];
//	int sum[rows][cols];
int sum[50][50];
//	int temp1[rows][cols];
int temp1[50][50];
//	int transpose [cols][rows];
int transpose [50][50];
int index,kndex,tndex,gndex,lndex;
int rows_sum[50];
//int rows_sum[rows]; //rows could be ANY value, still.

/* your program isn't designed for this
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
*/

/* You can save yourself this code, just by making the array initialize to
zero's, when you declare it:

row_sum[4] = { 0 };

This only works when you first declare them, not at any later time
*/
printf("enter the size of a  matrix:");
scanf("%d %d",&rows,&cols);
for (index = 0; index < rows; index++)
{
rows_sum[index]=0;
for (kndex = 0; kndex < cols; kndex++)
{
matrix[index][kndex] = 0;
sum[index][kndex] = 0;
temp1[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];
}
}
i = 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("%d ",matrix[index][kndex]);
}
printf("\n");
}
//  end rows sum

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;
//change this line remove the brace
//now swap the rows elements, from row j to row i
for(k = 0; k <rows; k++)   {
temp = matrix[i][k];                                      //here is line 62
matrix[i][k] = matrix[j][k];
matrix[j][k] = temp;
}
}
}

//print the swapped matrix
for(i = 0; i < rows; i++)  {
putchar('\n');
for(j = 0; j < cols; j++)
printf(" %d", matrix[i][j]);
}

i = getchar();
return 0;

}//end main```

9. I changed the code you gave me so the input will be from left to right

and then i get this wrong output.

http://img371.imageshack.us/img371/9926/50883916wi6.gif

Code:
```#include <stdio.h>

int main()   { //star
int i,j,k,temp;
int rows = 50;
int cols = 50;
int jndex,input;
//	int sum2=0;  sum2 is never used

/* You can't make an array without telling it's size. rows and cols
have not been assigned a value, yet.
*/
int matrix[50][50];
//	int matrix[rows][cols];
//	int sum[rows][cols];
int sum[50][50];
//	int temp1[rows][cols];
int temp1[50][50];
//	int transpose [cols][rows];
int transpose [50][50];
int index,kndex,tndex,gndex,lndex;
int rows_sum[50];
//int rows_sum[rows]; //rows could be ANY value, still.

/* your program isn't designed for this
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
*/

/* You can save yourself this code, just by making the array initialize to
zero's, when you declare it:

row_sum[4] = { 0 };

This only works when you first declare them, not at any later time
*/
printf("enter the size of a  matrix:");
scanf("%d %d",&rows,&cols);
for (index = 0; index < rows; index++)
{
rows_sum[index]=0;
for (kndex = 0; kndex < cols; kndex++)
{
matrix[index][kndex] = 0;
sum[index][kndex] = 0;
temp1[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 =0;index <rows; index++)
{
for (kndex =0; kndex<cols; kndex++)
{
scanf("%d", &matrix[index][kndex]);
transpose[kndex][index] = matrix[index][kndex];
}
}
i = 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("%d ",matrix[index][kndex]);
}
printf("\n");
}
//  end rows sum

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;
//change this line remove the brace
//now swap the rows elements, from row j to row i
for(k = 0; k <rows; k++)   {
temp = matrix[i][k];                                      //here is line 62
matrix[i][k] = matrix[j][k];
matrix[j][k] = temp;
}
}
}

//print the swapped matrix
for(i = 0; i < rows; i++)  {
putchar('\n');
for(j = 0; j < cols; j++)
printf(" %d", matrix[i][j]);
}

i = getchar();
return 0;

}//end main```

10. you marked red

//change this line remove the brace
what brace you mean?
what line to change?

11. when i used your code without any changes i get this mix rows
http://img175.imageshack.us/img175/6662/78048214ws3.gif

why?

12. i meant this input

http://img371.imageshack.us/img371/9926/50883916wi6.gif

when i enter the size and number for the matrix i get a mixed sorted matrix

13. Originally Posted by transgalactic2
you marked red

//change this line remove the brace
what brace you mean?
what line to change?
The only code on that line is a curly brace - *remove it*

The only code on the other line below it, needs a curly brace - *add it*

14. i will try again

15. on the line where you told to remove a braces there is no braces
i guess you meant the "for" opening braces
and i added another closing braces bellow you second comment
and i got a syntax error
Code:
```#include <stdio.h>

int main()   { //star
int i,j,k,temp;
int rows = 50;
int cols = 50;
int jndex,input;
//	int sum2=0;  sum2 is never used

/* You can't make an array without telling it's size. rows and cols
have not been assigned a value, yet.
*/
int matrix[50][50];
//	int matrix[rows][cols];
//	int sum[rows][cols];
int sum[50][50];
//	int temp1[rows][cols];
int temp1[50][50];
//	int transpose [cols][rows];
int transpose [50][50];
int index,kndex,tndex,gndex,lndex;
int rows_sum[50];
//int rows_sum[rows]; //rows could be ANY value, still.

/* your program isn't designed for this
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
*/

/* You can save yourself this code, just by making the array initialize to
zero's, when you declare it:

row_sum[4] = { 0 };

This only works when you first declare them, not at any later time
*/
printf("enter the size of a  matrix:");
scanf("%d %d",&rows,&cols);
for (index = 0; index < rows; index++)
{
rows_sum[index]=0;
for (kndex = 0; kndex < cols; kndex++)
{
matrix[index][kndex] = 0;
sum[index][kndex] = 0;
temp1[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];
}
}
i = 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("%d ",matrix[index][kndex]);
}
printf("\n");
}
//  end rows sum

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;
//change this line remove the brace
//now swap the rows elements, from row j to row i
for(k = 0; k <rows; k++)
temp = matrix[i][k];                                      //here is line 62
matrix[i][k] = matrix[j][k];
matrix[j][k] = temp;
}
}
}
}

//print the swapped matrix
for(i = 0; i < rows; i++)  {
putchar('\n');
for(j = 0; j < cols; j++)
printf(" %d", matrix[i][j]);
}

i = getchar();
return 0;

}//end main```