forked from CPRO-Session1/Assignment6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct.c
More file actions
65 lines (65 loc) · 1.41 KB
/
struct.c
File metadata and controls
65 lines (65 loc) · 1.41 KB
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
/*Lloyd Page*/
/*5 student structues in an array, repeatable*/
#include<stdio.h>
typedef struct
{
char name[100];
int age;
int scores[5];
}student;
int main()
{
char y;
do
{
int size;
char handler[100];
while(1)
{
printf("Enter a group size\n");
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%d",&size))
break;
printf("Enter a valid input\n");
}/*Santizes input for size*/
student group[size];
for(int i=0;i<size;i++)
{
printf("Enter student%d's name\n",i+1);
fgets(group[i].name,sizeof(group[i].name),stdin);
while(1)
{
printf("Enter student%d's age\n",i+1);
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%d",&group[i].age))
break;
printf("Enter a valid input\n");
}
for(int j=0;j<sizeof(group[i].scores)/4;j++)
{
while(1)
{
printf("Enter a student%d's grade for test%d\n",i+1,j+1);
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%d",&group[i].scores[j]))
break;
printf("Enter a valid number\n");
}
}
}
for(int i=0;i<size;i++)
{
float avg=0;
for(int j=0;j<sizeof(group[i].scores)/4;j++)
{
avg+=group[i].scores[j];
}
avg=avg/((float)(sizeof(group[i].scores))/4);
printf("Name: %s Age: %d Average score: %g\n",group[i].name,group[i].age,avg);
}
printf("Run again?(y/n)");
y=getchar();
fgets(handler,sizeof(handler),stdin);
}while(y=='y'||y=='Y');
return 0;
}