0

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;
}
  • 1
    Not the problem here, but you should read https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong and if you can properly read the matrix, then you should edit the question and remove all file reading and replace it with a hard coded matrix. – klutt Jun 18 '20 at 17:09
  • 1
    Start with simpler problems. Write some code to work with hard coded matrixes as klutt as suggested. Write code which finds the max in a 2x2 matrix. then a 3x3. then the 2x2 in the 3x3. build up the complexity as slowly as possible. – john elemans Jun 18 '20 at 17:12
  • I have created a small guide about how to debug C. It's not done, but you can have a look. https://github.com/klutt/debug-small-c-programs – klutt Jun 18 '20 at 17:26
  • First of all thank you for the tips, but unfortunately we have some impositions from our professor which, as much as I told about the 'break', sometimes are really annoying, plus we're studying (basis of C) in conditions where we must assume data and input are correct. –  Jun 18 '20 at 17:35
  • Also, hoping I won't sound rude, I really need a solution to my problem so that I can deep-study it, tomorrow I'll have my exam and this is *the* problem I could never solve. –  Jun 18 '20 at 17:37
  • @AlessandroRodolico Well, it's still a good idea to do what we said in order to solve it. If you construct and post a [mre] with expected and actual behavior, then we can just copy paste the code and run it and make the modifications necessary. – klutt Jun 18 '20 at 17:40
  • 1
    Plus, unfortunately, SO does not work that way that you just ask for code. – klutt Jun 18 '20 at 17:42
  • In normal situations i would follow your guidance, and I will, but I really lack hours this time. The MRE you explained in your guide sounds nice, but I feel like my mind is just stuck and couldn't even cope with doing 1+1 at the moment, so I can't even do that. On an unrelated note: This is my first question and I'm trying to understand how everything works, so..i noticed a flag on the side of your comment, and someone signaled it..does that work to say "good comment" or someone was angry at you? –  Jun 18 '20 at 17:48
  • @AlessandroRodolico I understand your problems, but unfortunately, it's very rare that people here are willing to construct all the boilerplate just to get an example up and running. Especially if it involves constructing a huge input file filled with numbers. My first comment here is upvoted and so is johns. The flag is something you can click if you want to notify the moderators of problems, like comments being rude and such. You cannot (at least I think so) see if anyone has flagged a comment. – klutt Jun 18 '20 at 17:55
  • Also, I'll explain why I asked for code: I won't have opportunity to debug during the exam, not to talk about the time I would spend doing an MRE that I couldn't run! Unfortunately it's not the easier of the conditions. I must code something like this in 2h without access to anything but a text editor (not even copy-paste lol). –  Jun 18 '20 at 17:56
  • @klutt Maybe I saw the flags because I asked the question, but still don't understand why anyone would do that. By the way if the problem is the file (kinda boring I know), and there's a chance I could send it to anybody, I would. –  Jun 18 '20 at 17:58
  • @klutt anyway, apart from the MRE which i'm trying at the moment (but still can't grow more than I already coded, failing) is there any other tips code-like you could think of? –  Jun 18 '20 at 18:00
  • @AlessandroRodolico I'll see if I can write something quickly that at least help you on your way – klutt Jun 18 '20 at 18:07
  • @klutt Thank you very much, I'll do better next time, you have been very kind –  Jun 18 '20 at 18:10
  • @AlessandroRodolico Done. Bear in mind that I have not even tried the code. https://pastebin.com/80NAYAkb – klutt Jun 18 '20 at 18:15
  • @AlessandroRodolico Also, these kind of sites are never the right place for "quick solutions". You should come to these sites way in advance, when you still have the energy to do the necessary corrections needed to create a good question. – klutt Jun 18 '20 at 18:16
  • Oh, and I forget the ending `}` for the if statement. – klutt Jun 18 '20 at 18:18
  • @klutt understood, thanks, I'm not going to bother you anymore. –  Jun 18 '20 at 18:19
  • @AlessandroRodolico No worries. I assume you understand it, but after that code is run, you'll have the maximum value for the submatrix in `max` and the coordinates in `maxx` and `maxy`. What you want to do with those is up to you. An idea might be to loop over every possible submatrix. Happy coding. – klutt Jun 18 '20 at 18:21
  • @klutt I got the idea now, it was the push I needed and I'll code what will be needed, thanks again (how do I flag your comment as the answer?) –  Jun 18 '20 at 18:25
  • @AlessandroRodolico You cannot flag a comment as an answer, only answers and tbh, the question itself is too unclear to be of any real value to others. – klutt Jun 18 '20 at 18:30
  • @AlessandroRodolico And if you are in the hurry you say you are, I would not really recommend spending time on improving the question right now. :) – klutt Jun 18 '20 at 18:31
  • @klutt It's just that I wanted to express gratification, not going to edit or changing anything until I'm done with the program first, and the exam tomorrow –  Jun 18 '20 at 18:37
  • @AlessandroRodolico Yes, I understood that. But you don't have to express gratification more than you have. – klutt Jun 18 '20 at 18:39

0 Answers0