I've read an (int) matrix[N][M] from a .txt file which turned out to be square. I am given a certain dimension "d" which will be the dimension of the sub-matrix[dim][dim] I'll have to use.
I have to scan the original matrix and seek those numbers (peaks) which are (strictly) the highest of the relative sub-matrix, then print to video the original matrix showing only the 'peaks' and "-" in the other places (i , j) where all the lower numbers were.
I'll provide an image to help the understanding as I'm not a native English speaker and I'm afraid It only makes sense to me.
Thank to all of you in advance.
---NOTE: Can't use 'break' because of my professor. Comments all over the code to better understand. Output expected Research method
My code:
#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;
}