Thread: sorting the matrix question..

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i wrote "here is line 62" as a comment near it
    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];                                      //here is line 62
                matrix[i][k] = matrix[j][k];
                matrix[j][k] = temp;
             }
          }
       }
    }

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You get a lot of errors, but you don't get that one.

    You have two conflicting definitions of temp. You also have eleven opening curly braces, but nine closing curly braces.

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am using visual studio 2005
    this is the whole list
    i dont know why
    i checked every error
    and i cant see the problem


    ex2.c(6) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files\microsoft visual studio 8\vc\include\stdio.h(295) : see declaration of 'scanf'
    1>c:\documents and settings\ \my documents\visual studio 2005\projects\es\es\ex2.c(7) : error C2143: syntax error : missing ';' before 'type'
    1>c:\documents and settings\ \my documents\visual studio 2005\projects\es\es\ex2.c(8) : error C2143: syntax error : missing ';' before 'type'
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(9) : error C2143: syntax error : missing ';' before 'type'
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(10) : error C2143: syntax error : missing ';' before 'type'
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(11) : error C2143: syntax error : missing ';' before 'type'
    1>c:\documents and settings \my documents\visual studio 2005\projects\es\es\ex2.c(12) : error C2143: syntax error : missing ';' before 'type'
    1>c:\documents and settings\ \my documents\visual studio 2005\projects\es\es\ex2.c(15) : error C2065: 'index' : undeclared identifier
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(17) : error C2065: 'rows_sum' : undeclared identifier
    1>c:\documents and settings \my documents\visual studio 2005\projects\es\es\ex2.c(17) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ \my documents\visual studio 2005\projects\es\es\ex2.c(18) : error C2065: 'kndex' : undeclared identifier
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(20) : error C2065: 'matrix' : undeclared identifier
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(20) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(21) : error C2065: 'sum' : undeclared identifier
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(21) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(22) : error C2065: 'temp' : undeclared identifier
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(22) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(23) : error C2065: 'transpose' : undeclared identifier
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(23) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(31) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(32) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(32) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(42) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(42) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(42) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(47) : error C2143: syntax error : missing ';' before 'type'
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(48) : error C2065: 'i' : undeclared identifier
    1>c:\documents and settings\\my documents\visual studio 2005\projects\es\es\ex2.c(49) : error C2065: 'j' : undeclared identifier
    1>c:\documents and settings \my documents\visual studio 2005\projects\es\es\ex2.c(50) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(50) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(51) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(52) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(52) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(53) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(56) : error C2065: 'k' : undeclared identifier
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(57) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(58) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings my documents\visual studio 2005\projects\es\es\ex2.c(58) : error C2109: subscript requires array or pointer type
    1>c:\documents and settings\ my documents\visual studio 2005\projects\es\es\ex2.c(59) : error C2109: subscript requires array or pointer type
    1>

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Ah. In C89 (which is where Microsoft stopped with their C compiler) you can't mix declarations with code. Once you do a line of code (specifically printf), that's it -- that's all the variables you can have. So you'll have to move all your declarations above all the code, or figure out how to turn on an extension so that you can mix declarations with code (should be possible, but I don't use VS enough to know how).

  5. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    If i understood you correctly:
    I need to move all the variable declaration on top
    i did it
    all the veriables are declared on the top of the main function
    still i get those errors

    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] ==> ");
    	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
    	 
    
    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

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That looks amazingly like the code that has the variable declarations all over. You must declare every single variable before you get to the printf statement.

  7. #22
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    looks like the problem is that i cant use vs2005 to run this thing
    Did I implement the sorting code which i was presented here correctly?
    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] ==> ");
    	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
    	 
    
    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

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I was just going to check that, by trying to run your program.

  9. #24
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so its not running??

    why??
    i put all the decalrations on top

  10. #25
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    For the same reason as your other thread...

    Runtime-sized statically declared arrays is a C99 feature, something that Visual Studio doesn't implement. If you know 50 is the maximum number of rows & columns, then why not write:

    Code:
    /* at the top: */
    #define MAX_ROWS 50
    #define MAX_COLS 50
    
    int matrix[MAX_ROWS][MAX_COLS];

  11. #26
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i will check that issue with the instructor

  12. #27
    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.

  13. #28
    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.

  14. #29
    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!

  15. #30
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ive been told to use 50X50 array

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