Task: Set an NxM integer matrix from the keyboard. Find in each odd row of the matrix element with the minimum value. Display the positions and values ​​of the items found.

Here is the program code:

#include <stdio.h> #include <stdlib.h> #include <string.h> int main () { int n,m; int mas2[4][4]; for (n = 1; n<4; n++) { for (m = 1; m<4; m++) { printf ("mas2[%d,%d] = ", n,m); scanf ("%d", &mas2[n] [m]); // Ввод массива } } for (n = 1; n<4; n++) { for (m = 1; m<4; m++) { printf ("%d ", mas2[n] [m]); // Вывод массива } printf ("\n"); } for (n = 1; n <4; n++) { for (m = 1; m <4; m++) { if ((n) % 2 != 0) { printf ("mas2[%d,%d] = %d\n", n,m,mas2[n] [m]); // Вывод нечетных строк } } } } 

I organized the output of odd lines, it is impossible to output the positions and values ​​of elements with the minimum value in these lines.

  • your_variable % 2 == 0 - this sort of even has always been - LFC
  • one
    Thank you, corrected - Zhichi
  • The last but one header of the for (n = 1; n <4; n ++) loop is better written as: for (n = 1; n <4; n + = 2) . Then the if statement will become unnecessary. - Sergey
  • Actually on the question of how to search for a minimum. First: get two integer variables - the first value of the expected minimum will be stored in the first one, and the position in the line of this very minimum will be stored in the second. Before starting the execution of the innermost loop (where you are now simply printing the elements of the line), assign the values ​​of the first element in the line and "1" to these variables. Inside the loop, check, and maybe the next element is less memorized? And, if so, assign new values ​​to this pair of variables. At the end of the cycle, you will have the minimum value in the line and its position. - Sergey

1 answer 1

The numbering of the elements of an array in C starts from zero, that is, mas [4] consists of the elements mas [0] - mas [3]. If the odd lines are the mas2 [1] [m] and mas2 [3] [m] lines, then:

 #include <stdio.h> #include <stdlib.h> int main () { int n,m,min, pos; int mas2[4][4]; for (n = 0; n<4; n++){ for (m = 0; m<4; m++){ printf ("mas2[%d,%d] = ", n,m); scanf ("%d", &mas2[n] [m]); // Ввод массива }} for (n = 0; n<4; n++){ for (m = 0; m<4; m++){ printf ("%d ", mas2[n] [m]); // Вывод массива } printf ("\n");} for (n = 1; n < 4; n += 2){ // нечётные строки min = mas2[n][0]; pos = 0 for (m = 1; m < 4; m++){ if(min > mas2[n][m]){ min = mas2[n][m]; pos = m;}} printf ("mas2[%d,%d] = %d\n", n, pos, min); // Вывод } return 0;}