Advertisement
iamthehxc

Untitled

Jul 22nd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. void swaprows(int arr[4][4], int row1, int row2);
  3. int main ()
  4. {
  5.     int arr[4][4]={ {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} };
  6.     int row1;
  7.     int row2;
  8.     int i, j;
  9.    
  10.     printf("This is the 2D array...\n");
  11.     for(i=0; i<4; i++)
  12.      {
  13.        for(j=0; j<4; j++)
  14.         printf("%2d ", arr[i][j]);
  15.        printf("\n");
  16.      }
  17.      
  18.     printf("\nWhich rows would you like to swap?");
  19.     scanf("&d" "&d", row1, row2);
  20.    
  21.     swaprows(arr,row1,row2);
  22.    
  23.     system("PAUSE");
  24.     return 0;
  25. }
  26.  
  27. void swaprows(int arr[4][4], int row1, int row2)
  28. {
  29.    int i, j;
  30.      
  31.    if (row1==row2)
  32.     {
  33.       printf("After swapping...\n");
  34.       for(i=0; i<4; i++)
  35.        {
  36.         for(j=0; j<4; j++)
  37.          printf("%2d ", arr[i][j]);
  38.         printf("\n");
  39.        }
  40.     }
  41.    
  42.    else if ((row1>=0 && row1<=3) && (row2>=0 && row2<=3))
  43.     {
  44.       int temp[4][4]={0};
  45.       for (i=0; i<4; i++)
  46.        {
  47.          temp[row1][i]=arr[row1][i];
  48.          arr[row1][i]=arr[row2][i];
  49.          arr[row2][i]=arr[row1][i];
  50.        }
  51.     printf("After swapping...\n");
  52.       for(i=0; i<4; i++)
  53.        {
  54.         for(j=0; j<4; j++)
  55.          printf("%2d ", arr[i][j]);
  56.         printf("\n");
  57.        }
  58.     }
  59.    
  60.    else
  61.       printf("Error: invalid entry.\n\n");
  62.      
  63.   return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement