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.