Reading integers from a file and counting the length of a descending subsequence I had a school project to solve, which I did. The basic focus of the project was to create the simplest code possible to solve it. This topic was created just to ensure if I wrote the code correctly.

The problem is the following:


  We have declared a dynamic array which will take numeric values from a txt file. We
  want to read the array backwards and to make a simple check.
  Read every 'i' element in the array and check if the value of the array is greater than > the last one.If it is the value saved in the int called temp_max which will be the > value of the array in the i position. The program will continue to check every number
  and if a value in the array is greater than temp_max we will overwrite the value of the temp_max to the value of the array in that position.


#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int main() {

    FILE *input, *output;
    int *children;
    int size, i;

    input = fopen("xxx.in", "r");

    if(input == NULL)
    return 1;

    fscanf(input,"%d",&amp;size);

    if(size &lt; 1 || size &gt; 1000000)
    return 1;

    children = malloc(size * sizeof(int));

    if (children==NULL) 
    return 1; 

    for ( i = 0; i &lt; size; i++ ) 
    {
        fscanf(input, "%d",&amp;children[i]);
    }
    fclose(input);

    int total = 0;
    int temp_max = 0;
    for(i = size - 1; i &gt;= 0; i--) 
    {
        if(children[i] &gt; temp_max) {
            temp_max = children[i];
            total++;
        }
    }

    free(children);

    output = fopen("xxx.out", "w+");
        fprintf(output, "%d\n", total );
        fclose(output);

    //This is the end, my dear friend!
    exit(0);
}



The int size was declared in one step above and is the size of the dynamic array.
The int total calculate how many times a number is greater than another. Default value is zero cause, I believe that should be what is your opinion.
The array hasn't any zero values or negative values at all.


The text file has the following format:

Example (xxx.in) 1:

5
9 6 5 4 2


Example (xxx.in) 2:

6
8959 5594 595 2 949 9
int main() returns an int so instead of exit(0); write return 0; at the end of main, it is more natural. exit is more something you would use to panically exit a program. 

When you write the file xxx.out, you don't check if opening the file succeeded, it always good to check return values of all runtime functions like you do when you read the other file.

Initialize all variables when you declare them to avoid any surprises. You have in some cases declared the variables where you use them, that is good. You should do that with all variable declarations instead of at top of main.

Check return value of fscanf() to see if it correctly got a number. I personally prefer fgets/sscanf when reading from file/stdin but that is me.

Maybe you could separate the functionality into some smaller functions e.g. readFile, writeFile and calculateTotal. It is a good habit to learn because programs have a tendency to grow with time, although for this particular case it may be overkill.

Another good habit is to write "why" comments, but I guess in this particular case it is quite self-evident what it does, although maybe a mention of the file format expected in the header of the program would be helpful for another programmer. :)

You could add some error messages to stderr when something is wrong instead of silently quitting, it makes troubleshooting easier e.g. 

fprintf(stderr, "invalid size %d", size);