I want to thank you guys firstly for your previous comments and feedback. Really appreciate it. I am learning C with school at the moment, but I plan to be "fluent" in C at the end of august of this year.
For school I have to write the following program:
- Open up a file. The file is a *.txt one. It consists of 32 numbers. The first number corresponds to the number of tests distributed under the students. The second number is saying how many have returned the test. The other 30 numbers could be seperated into a 5 by 6 table. There were 6 question with each 5 possible answers. the numbers represent how many students have chosen the answer. The file is posted at the end of this post.
- Now I have to open this file and making a display like this:
number of passed tests: 20 Number of returned tests: 19 Response is 95%. A B C D E grade passed question 1 2 2 2 2 2 2.4 yes question 2 2 2 2 2 2 2.4 yes question 3 2 2 2 2 2 2.4 yes question 4 2 2 2 2 2 2.4 yes question 5 2 2 2 2 2 2.4 yes question 6 2 2 2 2 2 2.4 yes
Also note that the "comment" has three options; fail if grade<3.0 , warning if grade<3.5 and pass if higher than 3.5. The code I Have written is like this :
#include <stdio.h>
#define RIJ 6
#define KOLOM 5
int invoer[RIJ][KOLOM] , rij, kolom;
float ingeleverd ;
float functie(int* i ) ;
int main(void)
{
float uitgereikt=0 , percentage ;
char bestandpad[100]= "" ;
FILE *inputbestand;
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.");
return 0;
}
fscanf(inputbestand, "%f", &uitgereikt);
fscanf(inputbestand, "%f", &ingeleverd);
percentage = (ingeleverd/uitgereikt) *100 ;
//------- overige data inlezen in een array van 5 bij 6.
for (rij=0; rij<RIJ; rij++)
{
for (kolom=0; kolom<KOLOM ; kolom++)
{
fscanf(inputbestand, "%d", &invoer[rij][kolom]);
}
}
fclose(inputbestand);
printf("Aantal uitgereikte evaluaties: %0.f\n" , uitgereikt);
printf("Aantal ingeleverde evaluaties: %0.f\n" , ingeleverd);
printf("Response is: %.1f %% \n\n" , percentage);
printf(" \t A\t B \t C \t D \t E \t cijfer \t opmerking\n\n");
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]));
}
}
}
}
float functie(int* i )
{
int a ;
float gem, totaal;
for (a=0; a<5 ; a++)
{totaal= totaal+ ( i[a] * (a+1) ) ;}
gem = totaal/ingeleverd ;
if (gem<3.0) {printf("%.1f \t \t Voldoet niet\n", gem);}
else if (gem<3.5) {printf("%.1f \t \t opmerking\n", gem);}
else if (gem>3.5) {printf("%.1f \t \t Voldoet\n", gem);}
// return gem ;
}
It is working completely. I have one minor bug though. the results are not aligned properly. What do you guys think ? Except the bad indenting... I am also very sorry it is in Dutch. But I think you guys will understand alright..
the inputfile is like this:
20 19 1 2 4 5 7 0 1 8 7 3 8 5 4 2 0 2 2 4 11 0 2 3 3 5 6 0 0 4 12 3