There is a two-dimensional array of 5x5, it is necessary to find the maximum and minimum elements of the array, then transfer the minimum to the beginning of the array, and the maximum to the end. Actually, below is my attempt to implement this, the problem is the incorrect output of the array itself:

#include <stdio.h> #include <conio.h> #include <math.h> int main(){ int K[5][5]; int i, j, max=40, min=-20; int iMax=0, jMax=0, iMin=0, jMin=0; int tmp; for (i=0; i<5; i++) { for (j=0; j<5; j++) { K[i][j] = min+ rand()%(max + 1 - min); } } for(i=0;i<5;i++){ for(j=0;j<5;j++) { if(K[i][j]<K[iMin][jMin]) { iMin = i; jMin = j; } if(K[i][j]>K[iMax][jMax]) { iMax = i; jMax = j; } } } tmp = K[0][0]; K[0][0] = K[iMin][jMin]; K[iMin][jMin] = tmp; tmp = K[4][4]; K[4][4] = K[iMax][jMax]; K[iMax][jMax] = tmp; for (i=0; i<5; i++) { for (j=0; j<5; j++){ printf ("%d\n", &K[i][j]); }} return 0; 

}

  • one
    So what exactly is the problem? Your question is asking for closure for a standard reason: Study assignments are allowed as questions only if you tried to solve them yourself before asking a question. Please edit the question and indicate what caused you difficulties in solving the problem. For example, provide the code you wrote when trying to solve a problem. - Harry
  • Thanks for the advice, edited - NickDolphin 3:54 pm

1 answer 1

To begin with, you don’t need to write at all & at output -

  printf ("%d\n", K[i][j]); 

And - I would display the matrix more carefully, and twice - before and after the conversion:

 #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { int K[5][5]; int i, j, max = 40, min = -20; int iMax = 0, jMax = 0, iMin = 0, jMin = 0; int tmp; for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { K[i][j] = min + rand() % (max + 1 - min); printf("%4d ", K[i][j]); } printf("\n"); } for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { if (K[i][j] < K[iMin][jMin]) { iMin = i; jMin = j; } if (K[i][j] > K[iMax][jMax]) { iMax = i; jMax = j; } } } tmp = K[0][0]; K[0][0] = K[iMin][jMin]; K[iMin][jMin] = tmp; tmp = K[4][4]; K[4][4] = K[iMax][jMax]; K[iMax][jMax] = tmp; printf("\n\n"); for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { printf("%4d ", K[i][j]); } printf("\n"); } } 

See https://ideone.com/wV1Gs0