Thread: sorting the matrix question..

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This works fine, obviously it's not your whole program. Should give you some guidance on
    some particulars and confidence that the algorithm we were talking about, works OK.


    Code:
    /* RowSum.c  Sort the matrix by the sum of their rows: for Transgalactic
    */
    
    #include <stdio.h>
    #define rowmax 4
    #define colmax 4
    
    int matrix[rowmax][colmax] = {
    { 8,4,9,7 },
    { 2,3,2,6 },
    { 5,2,2,9 },
    { 0,1,2,4 } };
    
    
    int main(void)  {
       int i,j,k,temp, r, c, rows;
       int sum[4] = { 28, 13, 18, 7 };
    
       for(i = 0; i < rowmax - 1; i++)  {       
          for(j = i + 1; j < rowmax; 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 <rowmax; k++)   {
                   temp = matrix[i][k];
                   matrix[i][k] = matrix[j][k];
                   matrix[j][k] = temp;
                }
             }
          }
       }
    
       //now print it
       for(r = 0; r < rowmax; r++)  {
          putchar('\n');
          for(c = 0; c < colmax; c++)
             printf(" %d", matrix[r][c]);
       }
       getchar();
       return 0;
    }
    Edit: I've got your whole program now, and I'm testing it.
    Last edited by Adak; 12-22-2008 at 04:46 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'll put my comments in the code.



    Code:
    #include <stdio.h>
    
    int main(){//star
    	int i,j,k,temp;
    	int rows,cols,jndex,input;
    	int sum2=0;
    	printf("enter rows and cols [1..50] ==> ");  //move these 2 line down below 
    	scanf("%d %d",&rows,&cols);                      //printf(" enter power"); 
    	int matrix[rows][cols];
    	int sum[rows][cols];
    	int temp1[rows][cols];   //changed name of array to temp1 - temp is used by an int
    	int transpose [cols][rows];
    	int index,kndex,tndex,gndex,lndex;
            int rows_sum[rows];                           
    	printf("enter power:");
    	scanf("%d",&input);
    
    //Do you want to do a sort via the index that I wrote about, or do you want to use the 
    //the first sort I showed you?
    
    //I wouldn't do both.
    
    	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];
    		}
    	}
    	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;
     
             //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;
             }
          }
       }
    }//end main
    That's all I see wrong, right now (it's very late, here). Let me know what kind of sort you want - and consider getting rid of index, kndex, gndex, etc. It's confusing. Standard practice is to use i or j, as you counters inside for loops, and here, row, or r, and col, or c, would be a natural to use. I highly recommend it.

    So far, I can't get your program to compile, but I'll work with it for a bit longer.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is as far as I could get with your full program. Note the corrections in the code.

    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[4][4];
    //	int matrix[rows][cols];
    //	int sum[rows][cols];
       int sum[4][4];
    //	int temp1[rows][cols];
       int temp1[4][4];
    //	int transpose [cols][rows];
       int transpose [4][4];
    	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
    It compiles, but I'm not sure how far it will run, or whether it's accurate. You need to get some more print statements in there, to see if your program is working right.

    good luck!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. power operation on a matrix question..
    By transgalactic2 in forum C Programming
    Replies: 6
    Last Post: 12-19-2008, 11:11 AM
  2. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  3. Replies: 3
    Last Post: 04-29-2005, 02:03 AM
  4. Replies: 21
    Last Post: 04-25-2005, 07:18 PM
  5. an actual question (sorting arrays)
    By master5001 in forum C Programming
    Replies: 4
    Last Post: 08-13-2001, 10:21 PM