Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

arrays - 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: 
");
        for (i = 0; i < 10; i++) {
            for (j = 0; j < 10; j++) {
                printf ("%d ", m[i][j]);
            }
            printf("
");
        }
    }
}
question from:https://stackoverflow.com/questions/65894741/matrix-and-swapping-rows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem in the case where max and min are the same is due to imax1 and imnin1 being Uninitialized. The problem surfaces here:

    if(imin1==imax1)                      /* <== here */
        printf("Same row");
    else {
        for(j=0;j<10;j++){
            a=m[imin1][j];
            m[imin1][j]=m[imax1][j];      /* <=== and here */
            m[imax1][j]=a;
        }
        printf("New matrix: 
");
        for(i=0;i<10;i++){
            for(j=0;j<10;j++){
                printf("%d ", m[i][j]);
            }
            printf("
");
        }
    }

Since you have set min and max to the same first element. If all elements are the same, if(m[i][j]<min) and if(m[i][j]>max) are always false and imax1 and imnin1 are never assigned values.

For variables with automatic storage duration, attempt to access the value of a variable while it value is indeterminate results in Undefined Behavior (bad JuJu)

C11 Standard - 6.7.9 Initialization(p10) "If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate." and C11 Standard - J.2 Undefined Behavior "The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.9, 6.8)."

You can eliminate the problem by always initializing min to the maximum value for the type and initializing max to the minimum value for the type, e.g.

#include <limits.h>
...
        min = INT_MAX,
        max = INT_MIN,

Putting it altogether and eliminating the use of the Magic-Numbers, you can do:

#include <stdio.h>
#include <limits.h>

#define ROWS  10        /* if you need a constant, #define one (or more) */
#define COLS  ROWS

int main() {
    
    int m[ROWS][COLS] = {{0}},
        min = INT_MAX,
        max = INT_MIN,
        iminx = 0, iminy = 0, imaxx = 0, imaxy = 0;
    
    puts ("Original matrix:");
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (scanf("%d", &m[i][j]) != 1) {   /* validate EVERY input */
                fputs ("error: invalid integer input.
", stderr);
                return 1;
            }
            printf (" %2d", m[i][j]);
        }
        putchar ('
');
    }
    
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (m[i][j] < min){
                min = m[i][j];
                iminx = i;
                iminy = j;
            }
            if (m[i][j] > max) {
                max = m[i][j];
                imaxx = i;
                imaxy = j;
            }
        }
    }
    
    printf ("
min: %2d (%d,%d)
max: %2d (%d,%d)

",
            min, iminx, iminy, max, imaxx, imaxy);
    
    if (iminx == imaxx)
        puts ("Same row
");
    else {
        int a = m[iminx][iminy];
        m[iminx][iminy] = m[imaxx][imaxy];
        m[imaxx][imaxy] = a;
        
        printf("New matrix: 
");
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                printf(" %2d", m[i][j]);
            }
            putchar ('
');
        }
    }
}

(note: the variable names for e.g. imin1, imin2 were changed to iminx and iminy, the same for imax., just for the simplified thought of x, y as coordinate indexes instead of 1, 2 -- entirely up to you)

Example Use/Output

$ ./bin/arr_max_min_swap < dat/arr_int_10x10.txt
Original matrix:
 41 48  2 35  6 74 92 89 70 14
 32 85 74 78 37 25 49 36 93 17
 79 73 23 88 22 83 21 42 85 86
 67 26 66  2 62 72 76 86 94 78
 32 26 63  7 37  1 64 86 69 57
 36 81 62 59  1 84 42 23 27 59
 41 26 86 39 60 80 11 68 98 37
 47 30 64 10 69 33 43 33 51 45
 90 87 26 52 46 27 69 21 82 28
 80 23 86 98 62 47 10  6 15  8

min:  1 (4,5)
max: 98 (6,8)

New matrix:
 41 48  2 35  6 74 92 89 70 14
 32 85 74 78 37 25 49 36 93 17
 79 73 23 88 22 83 21 42 85 86
 67 26 66  2 62 72 76 86 94 78
 32 26 63  7 37 98 64 86 69 57
 36 81 62 59  1 84 42 23 27 59
 41 26 86 39 60 80 11 68  1 37
 47 30 64 10 69 33 43 33 51 45
 90 87 26 52 46 27 69 21 82 28
 80 23 86 98 62 47 10  6 15  8

Same value:

$ ./bin/arr_max_min_swap < dat/arr_int_10x10_7.txt
Original matrix:
  7  7  7  7  7  7  7  7  7  7
  7  7  7  7  7  7  7  7  7  7
  7  7  7  7  7  7  7  7  7  7
  7  7  7  7  7  7  7  7  7  7
  7  7  7  7  7  7  7  7  7  7
  7  7  7  7  7  7  7  7  7  7
  7  7  7  7  7  7  7  7  7  7
  7  7  7  7  7  7  7  7  7  7
  7  7  7  7  7  7  7  7  7  7
  7  7  7  7  7  7  7  7  7  7

min:  7 (0,0)
max:  7 (0,0)

Same row

Let me know if you have further questions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...