How to scroll up a squre matrix

This is a discussion on How to scroll up a squre matrix within the C Programming forums, part of the General Programming Boards category; Hi friends. There's something wrong with my code. I want to shift each row one place upwards. The following square ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    10

    Post How to scroll up a squre matrix

    Hi friends.
    There's something wrong with my code. I want to shift each row one place upwards. The following square matrix of size 5 should look like this after the rotation.
    e f g h x
    i j k l x
    m n o p x
    q r s t x
    a b c d x

    Any help is appreciated.
    Code:
    #include<stdio.h>
    
    #define SIZE 5
    int main()
    {
    	char a[SIZE][SIZE] =  
    	{ 
    		{'a','b','c','d','x'},
    		{'e','f','g','h','x'},
    		{'i','j','k','l','x'},
    		{'m','n','o','p','x'},
    		{'q','r','s','t','x'}
    	
    	};
    
    
    	int i, j;
    	char temp[SIZE][SIZE];
        for(i=0;i<SIZE;i++)
    	{
    		for(j=0;j<SIZE;j++)
    			printf("%c", a[i][j]);
    			printf("\n");
    	}
    	printf("\n\n");
    	
    	
    
    
    for(i=0;i<SIZE;i++) // reverse_count necessary here!!
    
    	for(j=0;j<SIZE;j++)
           {
    			if (i==SIZE)                    //check_the last_row
    			{
    				for(j=0;j<SIZE;j++)
    					temp[i][j]= a[i][j]; 
    					a[i][j]= a[1][j]; 
    					a[1][j] =temp[i][j];	
    			}
    			
               temp[i][j]= a[i][j]; // initializing
               a[i][j]= a[i/(SIZE-1)+1][j]; // change a[i][j] into its new value
               a[i/(SIZE-1)+1][j] =temp[i][j];
    
    
    		   }
    		   /*	for(i=0;i<SIZE;i++){ // reverse_count necessary here!!
    
    			
    			for(j=0;j<SIZE;j++)
           {
               temp[i][j]= a[i][j]; // initializing
               a[i][j]= a[i/3+1][j] ; // change a[i][j] into its new value
               a[i/3+1][j] =temp[i][j];
    	   }
    	}
    */
    		//--------------------
    		for(i=0;i<SIZE;i++)
    		{
    			for(j=0;j<SIZE;j++)
    		
    			printf("%c", a[i][j]);
    			printf("\n");
    		}
    		
    			
    return 0;

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    If you know of ASCII values all you will need to do is increment accordingly. Granted your column data aligns properly, this is crucial. Below is just some starter code that may shove you in the right direction. The trick here is to assign that last row! You will see the error once you make this change and compile in.

    Code:
    /*Note this will not adjust properly for that last row shift */
     for (i = 0; i < SIZE; i++)
          {
            for (j = 0; j < SIZE -1; j++)
              temp[i][j] = (a[i][j] + 4);    /*convert 'a' to 'e' then assign to temp array and so on... */
    
            /*A more direct approach ( This is more of what you want actually)
             *   temp[i][j] = a[i+1][j];
             *
             * You will then not need the below code
             */
    
            temp[i][SIZE-1] = a[i][SIZE-1];  /* Keep the last column in tact = 'x' */
          }
    Last edited by slingerland3g; 12-14-2009 at 03:14 PM.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by slingerland3g View Post
    If you know of ASCII values all you will need to do is increment accordingly. Granted your column data aligns properly, this is crucial. Below is just some starter code that may shove you in the right direction. The trick here is to assign that last row! You will see the error once you make this change and compile in.
    I don't understand the point of your "solution" because it assumes the contents of the matrix. If you're assuming the contents of the matrix, then you already know what the result should be and the entire computation is pointless. The solution should do the right thing no matter what is in the matrix.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by brewbuck View Post
    I don't understand the point of your "solution" because it assumes the contents of the matrix. If you're assuming the contents of the matrix, then you already know what the result should be and the entire computation is pointless. The solution should do the right thing no matter what is in the matrix.
    Yes, was solely working of the OPs data, lots of assumption there. Code snippet was modified a 14 minutes prior to your post.
    Last edited by slingerland3g; 12-14-2009 at 03:33 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Store the top row.
    Shift every "row + 1" to "row", unless on the final row, in which case, you put in 'top row' that you saved earlier.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    Quote Originally Posted by slingerland3g View Post
    If you know of ASCII values all you will need to do is increment accordingly. Granted your column data aligns properly, this is crucial. Below is just some starter code that may shove you in the right direction. The trick here is to assign that last row! You will see the error once you make this change and compile in.

    Code:
    /*Note this will not adjust properly for that last row shift */
     for (i = 0; i < SIZE; i++)
          {
            for (j = 0; j < SIZE -1; j++)
              temp[i][j] = (a[i][j] + 4);    /*convert 'a' to 'e' then assign to temp array and so on... */
    
            /*A more direct approach ( This is more of what you want actually)
             *   temp[i][j] = a[i+1][j];
             *
             * You will then not need the below code
             */
    
            temp[i][SIZE-1] = a[i][SIZE-1];  /* Keep the last column in tact = 'x' */
          }
    Thanks for the fast response. However, this is actually going to be a function which has to work, when called by a mouse click "kbhit()" for matrices of any type and SIZE. I defined a size for the matrix just to illustrate and tried to generalize by using SIZE inside the for loop. How do I get this to work for say 20X20 ? Further help is appreciated.
    Code:
    ...
    		char display[SIZE][SIZE] /* user defines */
                    int key;
                     while(1)
    		{
    			key = 0;
    			if ( kbhit() )
    				key_code = getch();
    			if (key_code == CURSOR_UP ) {
    				clrscr();
    				matrix_scroll_UP(..); /*each click shifts_1_row_up*/

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Replace "SIZE" with "20". If you get it working for one size, all that's required to make it work for any other is simply changing "SIZE" to something else.
    Code:
        for( row = 0; row < ROWS; row++ )
        {
            for( col = 0; col < COLS; col++ )
            {
                if( row == 0 )
                    toprow[ col ] = matrix[ row ][ col ];
    
                matrix[ row ][ col ] = row == ROWS -1
                    ? toprow[ col ]
                    : matrix[ row+1 ][ col ];
            }
        }

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    Quote Originally Posted by quzah View Post
    Replace "SIZE" with "20". If you get it working for one size, all that's required to make it work for any other is simply changing "SIZE" to something else.
    Code:
        for( row = 0; row < ROWS; row++ )
        {
            for( col = 0; col < COLS; col++ )
            {
                if( row == 0 )
                    toprow[ col ] = matrix[ row ][ col ];
    
                matrix[ row ][ col ] = row == ROWS -1
                    ? toprow[ col ]
                    : matrix[ row+1 ][ col ];
            }
        }

    Quzah.
    The problem is the int division part " i/3" which accidentally solely worked for 4X4 matrix. If I change the SIZE into 20 the shifted matrix doesn't turn out to be right. (need a better revision)
    1- could you define toprow (assigning one dimensional to two ?)
    2- could you explain shortly the ternary part (difficult to understand)
    thanks

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    Thank you ALL for your help.
    generally for square matrices of any size
    shift_up_rows :
    Code:
    for(i=0;i<SIZE-1;i++)  // SIZE --> SIZE - 1 then it works
    
    	for(j=0;j<SIZE;j++)
           {
    	   temp[i][j] = a[i][j];  
               a[i][j] = a[i+1][j]; 
               a[i+1][j] = temp[i][j];
    
    	}
    shift_down_rows :
    Code:
    	for(i=SIZE-1;i>0;i--) 
    		for(j=0;j<SIZE;j++)
    		{
    			temp[i][j] = a[i][j];
    			a[i][j] = a[i-1][j]; 
    			a[i-1][j] = temp[i][j];
    		}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

Tags for this Thread


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