-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path050_array_of_structs.c
74 lines (64 loc) · 2.08 KB
/
050_array_of_structs.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
#include <stdio.h>
#include <string.h>
#include <math.h>
struct student {
char name[50];
int ID_number;
float agg_marks;
};
int main() {
int num_of_students;
printf("Enter the number of students: ");
scanf("%d", &num_of_students);
getchar();
printf("\n");
struct student stud_arr[num_of_students];
// getting user_input
for (int count = 0; count < num_of_students; count++) {
printf("Enter student #%d's name: ", count + 1);
scanf("%[^\n]", stud_arr[count].name);
getchar();
printf("Enter student #%d's ID number: ", count + 1);
scanf("%d", &stud_arr[count].ID_number);
printf("Enter student #%d's aggregate marks: ", count + 1);
scanf("%f", &stud_arr[count].agg_marks);
printf("\n");
getchar();
}
// printing user_input in form of a table
int length_of_max_name = 0;
for (int count = 0; count < num_of_students; count++) {
if (strlen(stud_arr[count].name) > length_of_max_name) {
length_of_max_name = strlen(stud_arr[count].name);
}
}
printf("length_of_max_name = %d\n", length_of_max_name);
for (int counter = 0; counter < (int) (length_of_max_name - strlen("Name")); counter++) {
printf(" ");
}
printf("Name");
for (int counter = 0; counter < (int) (length_of_max_name - strlen("ID number")); counter++) {
printf(" ");
}
printf("ID number");
for (int counter = 0; counter < 6; counter++) {
printf(" ");
}
printf("Aggregate Marks");
printf("\n");
for (int count = 0; count < num_of_students; count++) {
for (int counter = 0; counter < (int) (length_of_max_name - strlen(stud_arr[count].name)); counter++) {
printf(" ");
}
printf("%s", stud_arr[count].name);
for (int counter = 0; counter < (int) (length_of_max_name + strlen("ID number") - log10(stud_arr[count].ID_number) + 1); counter++) {
printf(" ");
}
printf("%i", stud_arr[count].ID_number);
for (int counter = 0; counter < (int) (strlen(" Aggregate Marks") - log10(stud_arr[count].agg_marks) + 1); counter++) {
printf(" ");
}
printf("%f", stud_arr[count].agg_marks);
printf("\n");
}
}