0
#include <stdio.h>
#include <iostream>
void main(void) {
    setlocale(LC_ALL, "Russian");
    int m, n, Min, Max;
    printf("Введите количество строк матрицы:");
    scanf_s("%d", &m);
    printf("Введите количество столбцов матрицы:");
    scanf_s("%d", &n);
    if (n <= 0 || m <= 0)
    {
        printf("Введите положительные размеры\n");
        return;
    }
    int** B = new int* [m];
    if (B == NULL)
    {
        printf("Недостаточно памяти!");
    }
    for (int x = 0; x < m; x++)
    {
        B[x] = new int[n];
        if (B[x] == NULL)
        {
            printf("Недостаточно памяти!");
            for (int j = 0; j < x; j++)
                delete[] B[j];
            delete[] B;
            return;
        }
    }
    printf("Введите элементы матрицы:\n");
    for (int x = 0; x < m; x++)
        for (int y = 0; y < n; y++)
            scanf_s("%d", &B[x][y]);
    //min, max 
    int min = B[0][0], indexmin1,indexmin2;
    for (int x = 1; x < m; x++)
        for (int y = 0; y < n; y++)
            if (B[x][y] < min) {
                min = B[x][y];
                indexmin1 = x;
                indexmin2 = y;
            }       
    int max = B[0][0],indexmax1, indexmax2;
    for (int x1 = 1; x1 < m; x1++)
        for (int y1 = 0; y1 < n; y1++)
            if (B[x1][y1] > max) {
                max = B[x1][y1];
                indexmax1 = x1;
                indexmax2 = y1;
            }
    Min = max;
    Max = min;
    B[indexmin1][indexmin2] = Max;
    B[indexmax1][indexmax2] = Min;
    printf("\nmin=%d \nmax=%d ", min, max);
    printf("%d%d", B[indexmin1][indexmin2], B[indexmax1][indexmax2]);
    system("pause");
}

1 ответ 1

1

Ну так очевидно же, что если B[0][0] у вас окажется тем самым минимальным и/или максимальным элементом, то соответствующие индексы (indexmin1,indexmin2 и/или indexmax1,indexmax2) останутся неинициализированными.

Инициализируйте свои индексные переменные.

1
  • 1
    Можете подсказать, как именно?
    – user364810
    21 дек 2019 в 22:12

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.