Как я могу сканировать максимальное значение в подматрице [dim] [dim], учитывая матрицу int [n] [m]? C - PullRequest
0 голосов
/ 18 июня 2020

Я прочитал (int) матрицу [N] [M] из файла .txt, который оказался квадратным. Мне дано определенное измерение «d», которое будет размером подматрицы [dim] [dim], которое мне придется использовать.

Мне нужно сканировать исходную матрицу и искать эти числа ( пики), которые (строго) являются наивысшими из относительных подматриц, затем распечатайте на видео исходную матрицу, показывающую только «пики» и «-» в других местах (i, j), где были все более низкие числа.

Я предоставлю изображение, чтобы помочь пониманию, поскольку я не являюсь носителем английского sh, и, боюсь, это имеет смысл только для меня.

Спасибо всем заранее.

--- ПРИМЕЧАНИЕ: Не могу использовать «перерыв» из-за моего профессора. Комментарии по всему коду для лучшего понимания. Ожидаемый результат Метод исследования

Мой код:

#include <stdio.h>
#include <ctype.h>

//Function prototype. Code below 'main'.
void trova_picchi(int D, int N, int M, const char nome_file[]);//Procedure to find the 'peaks'.

int main(int argc, const char * argv[])
{
    FILE *fp;
    int i, d, n, m;

    if (argc!=3)//Signals eventual error caused by incorrect number of command 'things' passed on launch.
    {
        printf("--Errore nel numero di parametri passati da linea di comando.\n\n");
        return -9;
    }

    if ((fp=fopen(argv[1], "r"))==NULL)//Signals eventual error after opening the file.
    {
        printf("--Errore nell'apertura del file \"%s\".\n\n", argv[1]);
        return -9;
    }

    i=0;
    while (i==0)
    {
        d=atoi(argv[2]);
        fscanf(fp, "%d %d", &n, &m);//Reads dimensions of the matrx[n][m].
        fclose(fp);
        i++;//Doesn't loop 'while' to infinity.
        trova_picchi(d, n, m, argv[1]);//Allows me to dinamically allocate matrix.
    }

    printf("\n");
    return 0;
}

void trova_picchi(int D, int N, int M, const char nome_file[])
{
    FILE *fPtr;
    int matrice[N][M], c, righe, colonne;
    int dim_sub;

    if ((fPtr=fopen(nome_file, "r"))==NULL)//Signals eventual error after opening the file.
    {
        printf("--Errore nell'apertura del file \"%s\", durante la procedura.\n\n", nome_file);
        return;
    }

    c=0;
    while (!feof(fPtr))
    {
        if (c!=0)//Check end of the 'while' loop why.
        {
            for (righe=0; righe<N; righe++)
                for (colonne=0; colonne<M; colonne++)
                    fscanf(fPtr, "%d", &matrice[righe][colonne]);
        }
        c++; //Skip only the first line (info already obtained).
    }
    fclose(fPtr);

    //Testing what I read.
    for (righe=0; righe<N; righe++)
    {
        for (colonne=0; colonne<M; colonne++)
            printf("%d ", matrice[righe][colonne]);
        printf("\n");
    }//End of the test.

    dim_sub=((2*D)+1));
    printf("\nLa dimensione della sotto-matrice da scansionare è: %d .\n", dim_sub);
    //Here I need to seek the highest values.

    return;
}
...