![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 5
| 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 | |
| | #2 |
| Registered User 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 | |
| | #3 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,763
| Quote:
__________________ "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 | |
| | #4 | |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 575
| Quote:
Last edited by slingerland3g; 12-14-2009 at 04:33 PM. | |
| slingerland3g is offline | |
| | #5 |
| +++ OK NO CARRIER 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 | |
| | #6 | |
| Registered User Join Date: Nov 2009
Posts: 5
| Quote:
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 | |
| | #7 |
| +++ OK NO CARRIER 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 | |
| | #8 | |
| Registered User Join Date: Nov 2009
Posts: 5
| Quote:
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 | |
| | #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];
}
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 | |
![]() |
| Tags |
| arrays, matrix, rotation, shiftdown, shiftup |
| Thread Tools | |
| Display Modes | |
|
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 |