2021年1月25日星期一

Matrix and swapping rows

My task is to find min and max value in matrix, and print that they are in the same row if they are in the same row, and swap their rows if they are not equal. My program worked when they are not equal, but it crashed in line where a = [imin1][j] when they were equal, and I don't know why. I hope you could help me.

#include <stdio.h>    int main() {      int m[10][10], i, j, min, max, imin1, imax1, imin2, imax2, a;      printf ("Elements of matrix: ");      for (i = 0; i < 10; i++) {          for (j = 0; j < 10; j++) {              scanf("%d", &m[i][j]);          }      }      min = m[0][0];      max = m[0][0];      for (i = 0; i < 10; i++) {          for (j = 0; j < 10; j++) {              if (m[i][j] < min) {                  min = m[i][j];                  imin1 = i;                  imin2 = j;              }              if (m[i][j] > max) {                  max = m[i][j];                  imax1 = i;                  imax2 = j;              }          }      }        if (imin1 == imax1)          printf ("Same row");      else {          for (j = 0; j < 10; j++) {              a = m[imin1][j];              m[imin1][j] = m[imax1][j];              m[imax1][j] = a;          }          printf ("New matrix: \n");          for (i = 0; i < 10; i++) {              for (j = 0; j < 10; j++) {                  printf ("%d ", m[i][j]);              }              printf("\n");          }      }  }  
https://stackoverflow.com/questions/65894741/matrix-and-swapping-rows January 26, 2021 at 09:31AM

没有评论:

发表评论