Advertisement
valve2

2D ARR row col shifter

May 12th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | Software | 0 0
  1. #include <stdio.h>
  2. int fill(int rows,int cols,int arr[rows][cols]){
  3.  
  4.  for(int i=0;i<rows;i++){
  5.     for(int j=0;j<cols;j++){
  6.     printf("Fill the indicie %dx%d\n",i ,j);
  7.     scanf("%d", &arr[i][j]);
  8.     }
  9.  }
  10.    int userchoice=choice();
  11.    if(userchoice == 1)
  12.     rowswap(rows,cols,arr);
  13.  
  14.    else
  15.     colswap(rows,cols,arr);
  16. }
  17. int choice(){
  18. int choice=0;
  19. do{
  20.     puts("1 to swap rows");
  21.     puts("2 to swap cols");
  22.     scanf("%d", &choice);
  23. }
  24. while(choice != 1 && choice !=2);
  25. return choice;
  26. }
  27. void colswap(int rows,int cols, int arr[rows][cols]){
  28. puts("PreSwap");
  29.         for(int i=0;i<rows;i++){
  30.             for(int j=0;j<cols;j++){
  31.                 printf("%d ", arr[i][j]);
  32.         }
  33.         puts(""); }
  34.             int swap1,swap2;
  35.             printf("Enter two cols to swap 0-%d", cols-1);
  36.             scanf("%d%d", &swap1, &swap2);
  37.             for(int j=0;j<rows;j++){
  38.                 int temp=arr[j][swap1];
  39.                 arr[j][swap1] = arr[j][swap2];
  40.                 arr[j][swap2] = temp;
  41.             }
  42.             for(int m=0;m<rows;m++){
  43.             for(int n=0;n<cols;n++){
  44.                 printf("%d ", arr[m][n]);
  45.             }
  46.             puts("");}
  47.             char str[5] = "cols";
  48.             char newstr[5];
  49.             int k=0;
  50.             puts("Enter ""cols"" if you want to swap rows, exit to end program");
  51.             scanf("%s", newstr);
  52.             for(int i=0;i<4;i++){
  53.             if(str[i]==newstr[i])
  54.                 k++;
  55.             }
  56.             if(k==4)
  57.                 rowswap(rows, cols, arr);
  58.                 else
  59.                     return 0;
  60. }
  61. int rowswap(int rows, int cols, int arr[rows][cols]){
  62.     puts("PreSwap");
  63.     for(int a=0; a<rows;a++){
  64.         for(int b=0;b<cols;b++){
  65.     printf("%d ", arr[a][b]);
  66.         }
  67.         puts("");
  68.     }
  69.         puts("");
  70.         printf("Enter two rows to swap 0-%d\n", rows-1);
  71.         int swap1, swap2;
  72.         scanf("%d%d", &swap1,&swap2);
  73.  
  74.         for(int i =0;i<cols;i++){
  75.             int temp = arr[swap1][i];
  76.         arr[swap1][i] = arr[swap2][i];
  77.         arr[swap2][i] = temp;
  78.  
  79.         }
  80.         puts("PostSwap");
  81.         for(int i=0;i<rows;i++){
  82.             for(int j=0;j<cols;j++){
  83.             printf("%d ", arr[i][j]);
  84.             }
  85.         puts("");
  86.         }
  87.         puts("Enter cols to swap cols, exit to end the program");
  88.         int k=0;
  89.         char str[5]= "cols";
  90.         char strnew[5];
  91.         scanf("%s", strnew);
  92.         for(int w=0;w<4;w++){
  93.             if(strnew[w]==str[w])
  94.                 k++;
  95.         }
  96.         if(k==4)
  97.             colswap(rows,cols,arr);
  98.         else
  99.             return 0;
  100. }
  101.  
  102. int main(){
  103. puts("Enter the dimenssions of the arr");
  104. int rows, cols;
  105. scanf("%d %d", &rows, &cols);
  106. int arr[rows][cols];
  107. fill(rows, cols, arr);
  108. return 0;
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement