-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex14_05.c
101 lines (87 loc) · 2.92 KB
/
ex14_05.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* pr_pset14_05.c
*
* Write a program that fits the following recipe:
* a. Externally define a name structure template with two members: a string
* to hold the first name and a string to hold the second name.
* b. Externally define a student structure template with three members:
* a name structure, a grade array to hold three floating-point scores,
* and a variable to hold the average of those three scores.
* c. Have the main() function declare an array of CSIZE (with CSIZE = 4)
* student structures and initialize the name portions to names of your choice.
* Use functions to perform the tasks described in parts d., e., f., and g.
* d. Interactively acquire scores for each student by prompting the user with
* a student name and a request for scores. Place the scores in the grade
* array portion of the appropriate structure. The required looping can be
* done in main() or in the function, as you prefer.
* e. Calculate the average score value for each structure and assign it
* to the proper member.
* f. Print the information in each structure.
* g. Print the class average for each of the numeric structure members.
*
* Created by gres.cher on 04/27/19.
*/
#include <stdio.h>
#define CSIZE 4
#define GRADES 3
struct names {
char * first;
char * last;
};
struct student {
struct names name;
float grade[GRADES];
float aver;
};
float avercalc(const struct student * pst);
void inscores(struct student * pst);
void printscores(const struct student * pst);
int main(void)
{
struct student class[CSIZE] = {
{{"Simon", "Posford"}},
{{"Richard", "James"}},
{{"Boris", "Blenn"}},
{{"Mathew", "Jonson"}}
};
for (int i = 0; i < CSIZE; i++)
inscores(&class[i]);
for (int i = 0; i < CSIZE; i++)
class[i].aver = avercalc(&class[i]);
for (int i = 0; i < CSIZE; i++)
printscores(&class[i]);
return 0;
}
// Calculate and return the average score value for a student structure
float avercalc(const struct student * pst)
{
float aver = 0.0;
for (int i = 0; i < GRADES; i++)
aver += pst->grade[i];
aver /= (float) GRADES;
return aver;
}
void inscores(struct student * pst)
{
// Print names and the number of scores
printf("Please, input %d scores for %s %s.\n",
GRADES, pst->name.first, pst->name.last);
// Input scores
for (int i = 0; i < GRADES; i++)
scanf("%f", &pst->grade[i]);
// Dispose rest of the line
while (getchar() != '\n')
continue;
}
// Print the information in each structure
void printscores(const struct student * pst)
{
// Print names
printf("%s %s:\t", pst->name.first, pst->name.last);
// Print scores
for (int i = 0; i < GRADES; i++)
printf("%.2f%s", pst->grade[i],
i + 1 == GRADES ? "\t" : " ");
// Print average
printf("avr: %.2f\n", pst->aver);
}