Thread: Swapping rows and columns in 2D arrray

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    7

    Swapping rows and columns in 2D arrray

    Hi,
    The problem is as follows, I have a 2D array (5x5) with random values.
    It will be displayed at the screen and the user will be given the choice to rotate the rows to the right or to the lef, and the columns up and down.

    Thus far, I only managed to rotate it to the left

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <time.h>
    # define COLS 5
    # define ROWS 5
    
    void shift_right(int a[ROWS][COLS]);
    void shift_left(int a[ROWS][COLS]);
    void shift_up(int a[ROWS][COLS]);
    void shift_dn(int a[ROWS][COLS]);
    void print_array(int a[ROWS][COLS]);
     
    int main() {
        srand(time(NULL));
        char choice;
        int i,j,array[ROWS][COLS];  
    
        for(i=0;i<ROWS;i++) {
            for (j=0;j<COLS;j++)
                array[i][j]=1+rand()%100;
        }
         
         printf("the 2D array is:\n");
         print_array(array);
         printf("Please enter the direction of rotatation: ");
         scanf("%c",&choice);
         printf("the rotated 2D aaray is:\n");
                 
         if(choice=='r'){
            shift_right(array);
            print_array(array);
                }
        else if(choice=='l'){
            shift_left(array);
                print_array(array);
                }
              
         else if(choice=='u'){
            shift_up(array);
            print_array(array);
             }
            
        else if(choice=='d'){
            shift_dn(array);
            print_array(array);
             }
                 
                 
        else
            printf("ERROR; %c is not a valid choice\n", choice);
                  
        return 0;                     
    }
     
     
    void shift_right(int a[ROWS][COLS]) {
             int i,j,temp[ROWS];
             for(i=0;i<ROWS;i++) {
                 for (j=4;j<COLS;j++)
                temp[i]=a[i][j];
             }
             for(i=0;i<ROWS;i++) {                     
                 for (j=0;j<COLS;j++) {
                if (j!=COLS-1)
                    a[i][j]=a[i+1][j]; 
                else
                a[i][COLS-5]=temp[i];
                 }
    }
    }
    void shift_left(int a[ROWS][COLS]) {
        int i,j,temp[ROWS];
        for(i=0;i<ROWS;i++) {
            for (j=0;j<COLS-4;j++)
                temp[i]=a[i][j];
        }
         for (i=0;i<ROWS;i++)                            
             for (j=0;j<COLS;j++) {
                if (j!=COLS-1)
                     a[i][j]=a[i][j+1]; 
                else
                    a[i][COLS-1]=temp[i];
                 }
    }
    void shift_up(int a[ROWS][COLS]) {
        int i,j,k=0,temp[ROWS];
        for(i=0;i<ROWS;i++)
            temp[i]=a[ROWS-5][i];
        for (i=0;i<ROWS;i++) {
            for (j=0;j<COLS;j++) {
                if (i!=ROWS-4)
                    a[i][j]=a[i][j]; 
                else
                    a[ROWS-1][j]=temp[j];
                }
            }
    }    
          
    void shift_dn(int a[ROWS][COLS]) {
         int i,j,temp[ROWS];
         for(i=0;i<ROWS;i++)
             temp[i]=a[ROWS-1][i];
             for (i=0;i<ROWS;i++) {
                 for (j=0;j<COLS;j++) {
                     if (i != ROWS-1)
                    a[i][j]=a[i][j]; 
                else
                    a[ROWS-2][j]=temp[j];
                    }
            }
    }
    void print_array(int a[ROWS][COLS]) {
        int i,j;
        for(i=0;i<ROWS;i++) {
            for (j=0;j<COLS;j++)
                printf("%5d ", a[i][j]);
            printf("\n");
             }
    }
    I would really appreciate any help

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Your inconsistent style is driving me sane.

    Please fix it; the flavor of style doesn't matter nearly so much as consistency.

    Beyond that, an you describe, with words, the process for all for rotations?

    Soma

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    7
    Hi, sorry about that :$

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <time.h>
    # define COLS 5
    # define ROWS 5
     
    void shift_right(int a[ROWS][COLS]);/*Right*/
    void shift_left(int a[ROWS][COLS]);/*Left*/
    void shift_up(int a[ROWS][COLS]);/*Upward*/
    void shift_dn(int a[ROWS][COLS]); /*Downward*/
    void print_array(int a[ROWS][COLS]); /*Prints*/
      
    int main() {
        
        srand(time(NULL));
        char choice;
        int i,j,array[ROWS][COLS];  
     
        for(i=0;i<ROWS;i++) {
            for (j=0;j<COLS;j++)
                array[i][j]=1+rand()%100;
                }
          
         printf("the 2D array is:\n");
         print_array(array);
         printf("Please enter the direction of rotatation: ");
         scanf("%c",&choice);
         printf("the rotated 2D aaray is:\n");
                  
         if(choice=='r'){
             shift_right(array);
             print_array(array);
                }
        
        else if(choice=='l'){
             shift_left(array);
             print_array(array);
                }
               
         else if(choice=='u'){
              shift_up(array);
              print_array(array);
             }
             
        else if(choice=='d'){
             shift_dn(array);
             print_array(array);
                             }
        else
            printf("ERROR; %c is not a valid choice\n", choice);
     return 0;                     
    }
      
    /* Rotates to the right*/
    void shift_right(int a[ROWS][COLS]) {
             int i,j,temp[ROWS];
    
    
             for(i=0;i<ROWS;i++) {
                 for (j=4;j<COLS;j++)
                     temp[i]=a[i][j];
                     }
             for(i=0;i<ROWS;i++) {                     
                 for (j=0;j<COLS;j++) {
                     if (j!=COLS-1)
                        a[i][j]=a[i+1][j]; 
                     else
                        a[i][COLS-5]=temp[i];
                        }
                 }
    }
    
    
    /*Rotates to the left*/
    void shift_left(int a[ROWS][COLS]) {
        int i,j,temp[ROWS];
    
    
        for(i=0;i<ROWS;i++) {
            for (j=0;j<COLS-4;j++)
                temp[i]=a[i][j];
                }
        for (i=0;i<ROWS;i++) {                       
             for (j=0;j<COLS;j++) {
                if (j!=COLS-1)
                     a[i][j]=a[i][j+1]; 
                else
                    a[i][COLS-1]=temp[i];
                 }
             }
    }
    
    
    /*Rotates upward*/
    void shift_up(int a[ROWS][COLS]) {
        int i,j,k=0,temp[ROWS];
    
    
        for(i=0;i<ROWS;i++)
            temp[i]=a[ROWS-5][i];
    
    
        for (i=0;i<ROWS;i++) {
            for (j=0;j<COLS;j++) {
                if (i!=ROWS-4)
                    a[i][j]=a[i][j]; 
                else
                    a[ROWS-1][j]=temp[j];
                }
            }
    }    
    
    
    /* Rotates downward*/       
    void shift_dn(int a[ROWS][COLS]) {
         int i,j,temp[ROWS];
    
    
         for(i=0;i<ROWS;i++)
             temp[i]=a[ROWS-1][i];
    
    
         for (i=0;i<ROWS;i++) {
             for (j=0;j<COLS;j++) {
                     if (i != ROWS-1)
                          a[i][j]=a[i][j]; 
                     else
                          a[ROWS-2][j]=temp[j];
                          }
                     }
    }
    
    
    /* Prints the array*/
    void print_array(int a[ROWS][COLS]) {
        int i,j;
        for(i=0;i<ROWS;i++) {
            for (j=0;j<COLS;j++)
                printf("%5d ", a[i][j]);
            printf("\n");
             }
    }
    And here's a sample output

    Code:
    the 2D array is:
       13    81    19    89    82
       61    25    45    28    46
       30    30    67    39    17
       39    92    69    69    69
       78    64    86    76    60
    Please enter the direction of rotatation: l
    the rotated 2D aaray is:
       81    19    89    82    13
       25    45    28    46    61
       30    67    39    17    30
       92    69    69    69    39
       64    86    76    60    78
    Press any key to continue . . .
    When rotating a 2D array to the right, all the elements in the 1st column moves to the 2nd, all the elements in the 2nd column moves to the 3rd,and all the elements in the last column moves to the 1st

    Thank you

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    When rotating a 2D array to the right, all the elements in the 1st column moves to the 2nd, all the elements in the 2nd column moves to the 3rd,and all the elements in the last column moves to the 1st
    Fair enough; why isn't your code equal that process?

    Actually, your code is kind of bizarre so ignore the existing code for a moment.

    Consider your steps as you've described them. Don't try to combine any steps as you've done in the code (yet). Get the code to move the columns to the right. Don't worry about the last column yet; get the other columns moving first. Once you have that done, no cheating by using the existing code, add fresh code to do that next step.

    [Edit]
    I'm not joking about ignoring the existing code either; I can't figure out an easy way to describe the problem so a safe bet is a fresh attempt.
    [/Edit]

    Soma

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What you originally did is a bit mental. If "rotations" are what you describe as actually being column/row shifts, then surely you understand that whether you shift to the left or to the right(up or down) is irrelevant to the algorithm involved. As a result, you could have a more generic function called shift_rows taking an int argument that tells it whether it's to the top and by how much (maybe by using a negative integer) or whether it is to the bottom and by how much (maybe using a positive integer). A similar solution can be set up for column shifts.

    EDIT:
    For clarity in the future note that a rotation is typically meant to represent turning the matrix by 90 degrees, hence the word ROTATE ( as in a round movement)
    Last edited by claudiu; 05-11-2012 at 09:34 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    7
    Ok, I have managed to copy the last column to the 1st one, and I'm kinda stuck right now :/
    the last column still has the same values

    Code:
    void swap(int a[ROWS][COLS]) {
         int i, j, temp[ROWS];
         for(i=0;i<COLS;i++) {
                             for(j=0;j<ROWS;j++) 
                                      temp[i]= a[i][j];
                                                
                              a[i-1][j] = temp[i];
                              }
         }
    Last edited by 997.2; 05-11-2012 at 09:56 AM.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Fix your spacing and brace placement. Can't you see how stupid it looks? How can you "program" that way?
    It should look something like this (although the "logic" is still wrong).
    Code:
    void swap(int a[ROWS][COLS]) {
        int i, j, temp[ROWS];
        for(i=0;i<COLS;i++) {
            for(j=0;j<ROWS;j++) 
                temp[i]= a[i][j];
            a[i-1][j] = temp[i];
        }
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Ok, I have managed to copy the last column to the 1st one, and I'm kinda stuck right now :/
    the last column still has the same values
    DON'T DO THAT YET.

    Seriously dude, we can all see where you are getting stuck.

    When you get stuck doing something in one direction try a different approach.

    Get this working first: move column 1 values to column 2, column 2 values to column 3, and so on.

    Don't worry about the values the first column should hold until you get that.

    Soma

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    7
    guys I'm really stuck! give me a hint please
    I'm not able to move the columns

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    7
    is this "logic" ok?

    Code:
    void shift_left(int a[ROWS][COLS]) {
        int i,j,temp[ROWS];
    
    
        for(i=0;i<ROWS;i++) {
            for (j=0;j<COLS-4;j++)
                temp[i]=a[i][j];
                }
        for (i=0;i<ROWS;i++) {                       
             for (j=0;j<COLS;j++) {
                if (j!=COLS-1)
                     a[i][j]=a[i][j+1]; 
                else
                    a[i][COLS-1]=temp[i];
                 }
             }
    }

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I still can't get over the "spacing".

    See how the braces are placed below. And see how every tab is 4 spaces, not sometimes 4 sometimes 5.
    Code:
    void shift_left(int a[ROWS][COLS]) {
        int i,j,temp[ROWS];
    
    
        for(i=0;i<ROWS;i++) {
            for (j=0;j<COLS-4;j++)
                temp[i]=a[i][j];
        }
        for (i=0;i<ROWS;i++) {                       
            for (j=0;j<COLS;j++) {
                if (j!=COLS-1)
                    a[i][j]=a[i][j+1]; 
                else
                    a[i][COLS-1]=temp[i];
            }
        }
    }
    The problem with the logic at this point is that it destorys the first column. You probably want to "rotate" that around to the last column.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping Rows and Columns in a 2D array
    By xxshankar in forum C Programming
    Replies: 2
    Last Post: 03-11-2010, 03:40 PM
  2. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  3. printing rows and columns
    By datainjector in forum C# Programming
    Replies: 1
    Last Post: 08-03-2003, 05:42 PM
  4. calculation from rows and columns
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-08-2002, 07:44 PM
  5. sizeof rows (not columns)
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 04:45 AM