1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
|
#include <stdio.h>
#include <stdlib.h>
#define MAXMATRNUM 6
#define MAXEXAMIDNUM 6
struct list
{
char Matr[MAXMATRNUM+1];
char ExamID[MAXEXAMIDNUM+1];
float mark; //kein array, sondern einzelner wert!
};
char passedTest(struct list *database, int n); //oben war struct list noch nicht definiert, außerdem hier pointer verlangen und anzahl elemente
int main()
{
int n,result,i;
struct list *database;
printf("Please enter the number of exams: ");
scanf("%d", &n);
database = malloc(n * sizeof(struct list));
if (database == 0)
return EXIT_FAILURE;
for(i=0; i<n; i++) //i laufen lassen, nicht n!!
{
printf("Please enter the Matriculation Number %d: ",i); //bessere textausgabe
scanf("%s",(char *)&database[i].Matr); //cast, sonst type-warning, da char[7] != char* und index ist laufvariable, nicht n!
printf("Please enter the Exam-ID %d: ",i);
scanf("%s",(char *)&database[i].ExamID);
printf("Please enter the Mark %d: ", i);
scanf("%f",&database[i].mark);
}
result=passedTest(database, n); //länge des array mit übergeben, da sonst nicht bekannt wie lang array
printf("The number of passed exams is: %d\n", result);
free(database);
return EXIT_SUCCESS;
}
char passedTest(struct list *database, int n)
{
int num=0,i; //n nicht mehr notwendig, da parameter
for(i=0;i<n;i++) //hier auch i laufen lassen, nicht n
{
if(database[i].mark<=4.0) //ebenso index ist i
num++;
else
continue;
}
return num;
} |