Array of structures

Neue Frage »

Auf diesen Beitrag antworten »
Tergo Array of structures

Hey Leute, sitze gerade vor folgender Aufgabe:

Schreibe ein Programm, welche die Exam-Ergebnisse durch Benutzung eines 'Array of Structure' behandelt. Die Daten für ein Exam sind:
- Mark: {0.7 - 5.0}
- Exam identifier: {simple number, 6 digits}
- Matriculation-No.: {simple number, 6 digits}

Voraussetzungen:
- Das Programm fragt nach der Anzahl der Examen
- Deklariere eine Struktur 'exam' welche Mark, Exam-ID und Matr enthält
- Das Programm reserviert Speicher via 'malloc' in Abhängigkeit von der Anzahl der Examen
- Das Programm fragt für alle Examen alle Daten ab (Mark, Exam-ID, Matr)
- Das Programm determiniert die Anzahl der bestandenen Examen (Mark <= 4.0)


hier ist gerade der aktuelle Stand meines Codes:

code:
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:
57:
58:
59:

#include <stdio.h> 
#include <stdlib.h>
#define MAXMATRNUM   6  
#define MAXEXAMIDNUM    6

char passedTest(struct list database);

struct list
{                                   
    char Matr[MAXMATRNUM+1]; 
    char ExamID[MAXEXAMIDNUM+1]; 
    float mark[]; 
}; 

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(n=0; n<i; n++)
    {
        printf("Please enter the Matriculation Number %d",i);
        scanf("%c",&database[n].Matr);
        printf("Please enter the Exam-ID %d",i);
        scanf("%c",&database[n].ExamID);
        printf("Please enter the Mark %d",i);
        scanf("%f",&database[n].mark);
    }
    
    result=passedTest(struct database);
    
    printf("The number of passed exams is: %d", result);
    
    free (database);
    return EXIT_SUCCESS;
} 

char passedTest(struct list database)
{
    int num=0,n,i;
    
    for(n=0;n<i;n++)
    {
    if(database[n].mark<=4.0)
    num++;
    else
    continue;
    }
    return num;
}



Die Idee war, in der main-Funktion alle benötigten Werte abzufragen und in der Display-Funktion dann abzuschätzen, wieviele Examen 'bestanden' haben. Wie ihr seht, weiß ich aber dann nicht mehr weiter. Meine Frage nun: Wie kann ich Teile einer Struktur in Funktion auf ihre Wertigkeit testen?
 
Auf diesen Beitrag antworten »
Karlito

Ansatz war richtig, aber viele kleine Fehlerchen. Bei Fragen bitte Rückfragen.

code:
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;
}


Gruß,

Karlito
 
Neue Frage »
Antworten »


Verwandte Themen

Die Beliebtesten »
Die Größten »
Die Neuesten »