Skip to main content
Added a variable that needs to be passed to an inner function, removed bad "&".
Source Link
user1118321
  • 12k
  • 1
  • 20
  • 46
#define RESULT_OK 1    // <- These can go at the top of the file 
#define RESULT_ERROR 0 // <- with the others
int printIntro (FILE** inputbestand, char bestandpad[100])
{
    int result = RESULT_OK;
    printf("Dit programma verwerkt de data uit verschillende enquetes.\n");
    printf("\n");
    printf("Voer de locatie van het bestand in.\n");
    scanf("%s", &bestandpadbestandpad);
    *inputbestand = fopen(bestandpad, "r" );

    if (*inputbestand == NULL)
    {
        printf("Bestand niet gevonden.");
        result = RESULT_ERROR;
    }
    else
    {
        result = RESULT_OK;
    }
    
    return result;
}

Although, perhaps a better function name would be getFileName() (or would it be getBestandpad()? I don't know Dutch for "get").

int main (void)
{
    FILE* inputbestand;
    int invoer[RIJ][KOLOM];
    char bestandpad[100];

    int result = printIntro(&inputbestand, bestandpad);
    if (result == RESULT_OK)
    {
        float uitgereikt;
        float ingeleverd;
        readHeader(inputbestand, &uitgereikt, &ingeleverd);
        readTable(inputbestand, invoer);
        fclose(inputbestand);

        printStatistics(uitgereikt, ingeleverd);
        printResults(invoer, ingeleverd);
    }

    return result;
}
void printResults(int invoer[RIJ][KOLOM], const float ingeleverd)
{
    printf("   \t  A\t B \t C \t D \t E \t cijfer \t opmerking\n\n");
    int rij, kolom;
    for (rij=0; rij<RIJ; rij++)
    {
        printf("vraag %d   " ,rij+1);

        for (kolom=0; kolom<KOLOM ; kolom++)
        {
            printf("%d       ", invoer[rij][kolom]);
            if (kolom==KOLOM-1) 
            {
                printf("%c \n", functie(invoer[rij], ingeleverd));
            }
        }
    }
}
#define RESULT_OK 1    // <- These can go at the top of the file 
#define RESULT_ERROR 0 // <- with the others
int printIntro (FILE** inputbestand, char bestandpad[100])
{
    int result = RESULT_OK;
    printf("Dit programma verwerkt de data uit verschillende enquetes.\n");
    printf("\n");
    printf("Voer de locatie van het bestand in.\n");
    scanf("%s", &bestandpad);
    *inputbestand = fopen(bestandpad, "r" );

    if (*inputbestand == NULL)
    {
        printf("Bestand niet gevonden.");
        result = RESULT_ERROR;
    }
    else
    {
        result = RESULT_OK;
    }
    
    return result;
}
int main (void)
{
    FILE* inputbestand;
    int invoer[RIJ][KOLOM];
    char bestandpad[100];

    int result = printIntro(&inputbestand, bestandpad);
    if (result == RESULT_OK)
    {
        float uitgereikt;
        float ingeleverd;
        readHeader(inputbestand, &uitgereikt, &ingeleverd);
        readTable(inputbestand, invoer);
        fclose(inputbestand);

        printStatistics(uitgereikt, ingeleverd);
        printResults(invoer);
    }

    return result;
}
void printResults(int invoer[RIJ][KOLOM])
{
    printf("   \t  A\t B \t C \t D \t E \t cijfer \t opmerking\n\n");
    int rij, kolom;
    for (rij=0; rij<RIJ; rij++)
    {
        printf("vraag %d   " ,rij+1);

        for (kolom=0; kolom<KOLOM ; kolom++)
        {
            printf("%d       ", invoer[rij][kolom]);
            if (kolom==KOLOM-1) 
            {
                printf("%c \n", functie(invoer[rij]));
            }
        }
    }
}
#define RESULT_OK 1    // <- These can go at the top of the file 
#define RESULT_ERROR 0 // <- with the others
int printIntro (FILE** inputbestand, char bestandpad[100])
{
    int result = RESULT_OK;
    printf("Dit programma verwerkt de data uit verschillende enquetes.\n");
    printf("\n");
    printf("Voer de locatie van het bestand in.\n");
    scanf("%s", bestandpad);
    *inputbestand = fopen(bestandpad, "r" );

    if (*inputbestand == NULL)
    {
        printf("Bestand niet gevonden.");
        result = RESULT_ERROR;
    }
    else
    {
        result = RESULT_OK;
    }
    
    return result;
}

Although, perhaps a better function name would be getFileName() (or would it be getBestandpad()? I don't know Dutch for "get").

int main (void)
{
    FILE* inputbestand;
    int invoer[RIJ][KOLOM];
    char bestandpad[100];

    int result = printIntro(&inputbestand, bestandpad);
    if (result == RESULT_OK)
    {
        float uitgereikt;
        float ingeleverd;
        readHeader(inputbestand, &uitgereikt, &ingeleverd);
        readTable(inputbestand, invoer);
        fclose(inputbestand);

        printStatistics(uitgereikt, ingeleverd);
        printResults(invoer, ingeleverd);
    }

    return result;
}
void printResults(int invoer[RIJ][KOLOM], const float ingeleverd)
{
    printf("   \t  A\t B \t C \t D \t E \t cijfer \t opmerking\n\n");
    int rij, kolom;
    for (rij=0; rij<RIJ; rij++)
    {
        printf("vraag %d   " ,rij+1);

        for (kolom=0; kolom<KOLOM ; kolom++)
        {
            printf("%d       ", invoer[rij][kolom]);
            if (kolom==KOLOM-1) 
            {
                printf("%c \n", functie(invoer[rij], ingeleverd));
            }
        }
    }
}
Source Link
user1118321
  • 12k
  • 1
  • 20
  • 46

I would think about breaking this up into more functions that do specific things. For example, I would break out the reading of the header information (the number of tests passed and the number returned) into one function, and the reading of the actual test data into another function:

void readHeader (FILE* inputbestand, float* uitgereikt, float* ingeleverd)
{
    fscanf(inputbestand, "%f", uitgereikt);
    fscanf(inputbestand, "%f", ingeleverd);
}

This brings up a question I have for you - why is ingeleverd a global variable? In fact, why do you have any global variables? They all look like they belong inside the main() function to me.

In any event, reading the table of data would then look something like this:

void readTable (FILE* inputbestand, int invoer[RIJ][KOLOM])
{
    //------- overige data inlezen in een array van 5 bij 6.
    int rij, kolom;
    for (rij = 0; rij < RIJ; rij++)
    {
        for (kolom = 0; kolom < KOLOM; kolom++)
        {
            fscanf(inputbestand, "%d", &invoer[rij][kolom]);
        }
    }
}

For what it's worth, I like to put spaces around operators (like comparison operators - <, >, etc.), as I find it much easier to read. That's just a style decision and might not suit you, but that's what I prefer.

I would also move the output into various functions. The introductory text could go into a function like this:

#define RESULT_OK 1    // <- These can go at the top of the file 
#define RESULT_ERROR 0 // <- with the others
int printIntro (FILE** inputbestand, char bestandpad[100])
{
    int result = RESULT_OK;
    printf("Dit programma verwerkt de data uit verschillende enquetes.\n");
    printf("\n");
    printf("Voer de locatie van het bestand in.\n");
    scanf("%s", &bestandpad);
    *inputbestand = fopen(bestandpad, "r" );

    if (*inputbestand == NULL)
    {
        printf("Bestand niet gevonden.");
        result = RESULT_ERROR;
    }
    else
    {
        result = RESULT_OK;
    }
    
    return result;
}

Since you were originally returning 0 from main() when the file could not be read, you need to return any errors from the above function and use that in main() to decide what to return. Which reminds me - you aren't returning a value on success in your main() function. So let's look at that next. After breaking out the various functions, it will look something like this:

int main (void)
{
    FILE* inputbestand;
    int invoer[RIJ][KOLOM];
    char bestandpad[100];

    int result = printIntro(&inputbestand, bestandpad);
    if (result == RESULT_OK)
    {
        float uitgereikt;
        float ingeleverd;
        readHeader(inputbestand, &uitgereikt, &ingeleverd);
        readTable(inputbestand, invoer);
        fclose(inputbestand);

        printStatistics(uitgereikt, ingeleverd);
        printResults(invoer);
    }

    return result;
}

The function printStatistics() should print out the information about how many tests were passed and how many were returned:

void printStatistics (const float uitgereikt, const float ingeleverd)
{
    printf("Aantal uitgereikte evaluaties: %0.f\n" , uitgereikt);
    printf("Aantal ingeleverde evaluaties: %0.f\n" , ingeleverd);
    
    float percentage = (ingeleverd/uitgereikt) *100 ;
    printf("Response is: %.1f %% \n\n" , percentage);
}

Likewise, printResults() should print out the resulting table:

void printResults(int invoer[RIJ][KOLOM])
{
    printf("   \t  A\t B \t C \t D \t E \t cijfer \t opmerking\n\n");
    int rij, kolom;
    for (rij=0; rij<RIJ; rij++)
    {
        printf("vraag %d   " ,rij+1);

        for (kolom=0; kolom<KOLOM ; kolom++)
        {
            printf("%d       ", invoer[rij][kolom]);
            if (kolom==KOLOM-1) 
            {
                printf("%c \n", functie(invoer[rij]));
            }
        }
    }
}

It's not clear to me what the functie() function is doing. It looks like it's supposed to calculate an average, but it doesn't look to me like it's actually doing that. Also, it returns a float, but it's being used as a character in the printf() statement where it's called. That's almost certainly incorrect. And you've listed 3.0 and 3.5 as the breakpoints between fail, warning, and pass, but in the example, all the grades are 2.4, but marked as "pass". So I'd double-check to make sure you understand the requirement there.

In addition to the above, you should know that there are various ways that reading from the file could fail. You could have truncated data, or there might be the wrong kind of data in the file. I'm not sure if your teacher wants you to worry about that yet, or not. If you want to, you can check the return value of fscanf() and make sure that it read the argument you expected.