1. How to compare 2 adjacent elements and, if they are unequal, get out of the cycle?
  2. How to determine the row number of a two-dimensional array, the sum of the elements of which is maximum? How is this better done?

-

#include "stdafx.h" #include "stdio.h" #include "conio.h" #include <locale.h> #define HB 5 void main() { setlocale(LC_CTYPE, "Russian"); // вывод на русском языке int a[HB]; // массив int k; // индекс int ok; // 1 - последовательность неубывающая printf("Проверка, упорядочен ли массивn"); printf("по возрастаниюn"); printf("Введите массив (%i целых чисел ", HB); printf("в одной строке) и нажмите <Enter>n"); for (k = 0; k < HB; k++) scanf("%i", &a[k]); k = 0; ok = 1; do { if (a[k] > a[k + 1]) ok = 0; k++; } while (k < HB - 1 && ok); printf("Элементы массива "); if (!ok) printf("не "); printf("упорядочены по возрастаниюn"); printf("nДля завершения работы нажмите <Enter>"); _getch(); } 

    1 answer 1

    Comparison of two adjacent:

     for (k = 0; k < HB - 1; k++) { if (a[k] != a[k + 1]) break; } 

    Search for the line with the maximum sum of elements:

     maxRowIndex = -1; maxSum = 0; for (i = 0; i < n; i++) { sum = 0; for (j = 0; j < m; j++) { sum += mas[i][j]; } if (maxSum < sum || maxRowIndex == -1) { maxSum = sum; maxRowIndex = i; } } 

    Where n is the number of rows, m is the number of columns, maxSum is the maximum sum of the row elements for the current moment, sum is the sum of the elements of the current row, maxRowIndex is the index of the row with the maximum sum of elements.

    • Are you talking about what? - Victor Khorolsky
    • I apologize, accidentally during typing click "Answer the question" - Donil
    • I understood) it happens to everyone) thanks a lot) - Victor Khorolsky
    • But how to check whether the elements of the array entered from the keyboard represent a decreasing sequence? - Victor Khorolsky
    • Replace if (a [k] <a [k + 1]) in line with> - Donil