Advertisement
valve2

2D ARR row swap prog

May 12th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | Software | 0 0
  1. #include <stdio.h>
  2. #define rows 3
  3. #define cols 3
  4.  
  5. void swap(int arr[][cols], int swap1, int swap2){
  6.  
  7. if(swap1 >= 0 && swap1 <= rows-1 && swap2 >= 0 && swap2 <= rows-1){
  8. for(int i=0; i<cols;i++){
  9.     int temp =  arr[swap1][i];
  10.     arr[swap1][i]=arr[swap2][i];
  11.     arr[swap2][i] = temp;
  12.  
  13.  
  14. }
  15.         for(int i =0; i<rows;i++){
  16.             for(int j=0;j<cols;j++){
  17.                 printf(" %d ", arr[i][j]);
  18.             } puts("");
  19. }
  20.   } else{
  21.     puts("Invalid row input");
  22. }
  23. }
  24.  
  25. int main(){
  26.  
  27. int arr[rows][cols] = {{1,2,3},{4,5,6},{7,8,9}};
  28.  
  29. puts("PreSwap");
  30. for(int i=0;i<rows;i++){
  31.     for(int j=0;j<cols;j++){
  32.     printf(" %d ", arr[i][j]);
  33.     }
  34.     puts("");
  35. }
  36. puts("Enter 2 rows to swap ""0-2"" ");
  37. int swap1,swap2;
  38. scanf("%d %d", &swap1, &swap2);
  39.  swap(arr, swap1, swap2);
  40. return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement