# Thread: How to scroll up a squre matrix

1. ## 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. 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' */
}```

3. Originally Posted by slingerland3g
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.

4. Originally Posted by brewbuck
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.

5. 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.

6. Originally Posted by slingerland3g
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. 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.

8. Originally Posted by quzah
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. 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];
}```