Good evening. For some unknown reason, this code does not change anything at all in the matrix. Task: arrange the rows of a given matrix in ascending order of the number of identical elements in each row. Here is the code:

int main() { const int N = 5; int m[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) cin >> m[i][j]; int s[N], n[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) for (int k = j + 1; k < N; k++) if (m[i][j] == m[i][k]) n[i][j]++; int maxCount = 0; for (int j = 0; j < N; j++) if (n[i][j] > maxCount) maxCount = n[i][j]; s[i] = maxCount + 1; cout << s[i] << " "; } cout << "nn"; for (int i = 0; i < N; i++) { int maxCount = s[i]; int index = i; for (int j = i + 1; j < N; j++) if (s[j] > maxCount) { maxCount = s[j]; index = j; } int temp = s[i]; s[i] = maxCount; s[index] = temp; for (int k = 0; k < N; k++) { temp = m[i][k]; m[i][k] = m[index][k]; m[index][k] = temp; } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cout << m[i][j] << " "; cout << endl; } system("pause"); return 0; } 
  • @RubyNub: 1 1 1 1 1 means that all the elements in the lines are different, the number of identical elements is always 1, and what really nothing to change? Try to enter other elements in the matrix. - VladD
  • @RubyNub: Help? - VladD
  • @VladD, yes, thank - RubyNub pm
  • @RubyNub: great, then I will make an answer out of it - VladD

1 answer 1

You have forgotten the initial filling of the array n zeros.

 for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) n[i][j] = 0; 

Otherwise there may be random values.

  • Already got rid of this extra matrix - I realized that it can be considered using a single array) Thanks again. - RubyNub
  • @RubyNub: please! :) Yes, and really, she was not needed. rather pass this nudyatinu and go to the really interesting problems. - VladD