sorting the matrix question..

This is a discussion on sorting the matrix question.. within the C Programming forums, part of the General Programming Boards category; i changed it to Code: #include <stdio.h> int main() { //star int i,j,k,temp; int rows = 4; int cols = ...

  1. #31
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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. #32
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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. #33
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ahh never mind
    its getchar in the code

  4. #34
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what to do in a case where the sum of two rows is equal??

  5. #35
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #36
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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. #37
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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;
                }  
             }  //add this brace
          }
       }
    
       //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. #39
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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;
                }
             }  //add this brace
          }
       }
    
       //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
    Last edited by transgalactic2; 12-22-2008 at 02:24 PM.

  10. #40
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you marked red

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

  11. #41
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when i used your code without any changes i get this mix rows
    http://img175.imageshack.us/img175/6662/78048214ws3.gif

    why?

  12. #42
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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. #43
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by transgalactic2 View Post
    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. #44
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i will try again

  15. #45
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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;
                }
             }
             }//add this brace
          }
       }
    
       //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

Page 3 of 4 FirstFirst 1234 LastLast
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, 03:03 AM
  4. Replies: 21
    Last Post: 04-25-2005, 08:18 PM
  5. an actual question (sorting arrays)
    By master5001 in forum C Programming
    Replies: 4
    Last Post: 08-13-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21