C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-14-2009, 02:39 PM   #1
Registered User
 
Join Date: Nov 2009
Posts: 5
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;
Armin is offline   Reply With Quote
Old 12-14-2009, 03:39 PM   #2
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 575
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 04:14 PM.
slingerland3g is offline   Reply With Quote
Old 12-14-2009, 04:28 PM   #3
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,763
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.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 12-14-2009, 04:29 PM   #4
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 575
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 04:33 PM.
slingerland3g is offline   Reply With Quote
Old 12-14-2009, 05:24 PM   #5
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 12-14-2009, 05:35 PM   #6
Registered User
 
Join Date: Nov 2009
Posts: 5
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*/
Armin is offline   Reply With Quote
Old 12-14-2009, 06:13 PM   #7
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 12-15-2009, 11:38 AM   #8
Registered User
 
Join Date: Nov 2009
Posts: 5
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
Armin is offline   Reply With Quote
Old 12-15-2009, 03:29 PM   #9
Registered User
 
Join Date: Nov 2009
Posts: 5
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];
		}
Armin is offline   Reply With Quote
Reply

Tags
arrays, matrix, rotation, shiftdown, shiftup

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C - access violation uber C Programming 2 07-08-2009 01:30 PM
Matrix Help HelpmeMark C++ Programming 27 03-06-2008 05:57 PM
What is a matrix's purpose in OpenGL jimboob Game Programming 5 11-14-2004 12:19 AM
Matrix and vector operations on computers DavidP A Brief History of Cprogramming.com 11 05-11-2004 06:36 AM


All times are GMT -6. The time now is 02:06 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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