-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram1B.c
More file actions
95 lines (79 loc) · 3.01 KB
/
program1B.c
File metadata and controls
95 lines (79 loc) · 3.01 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
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
#include <stdio.h>
#define MAX_STUDENTS 5
#define NUM_SUBJECTS 3
// Structure definition for Student
struct Student {
int rollNo;
char name[50];
int semester;
int marks[NUM_SUBJECTS];
};
// Function prototypes
void enterDetails(struct Student *students, int numStudents);
void displayDetails(const struct Student *students, int numStudents);
void calculateTotals(const struct Student *students, int numStudents);
int main() {
struct Student students[MAX_STUDENTS];
// Enter details for 5 students
enterDetails(students, MAX_STUDENTS);
// Display details for 5 students
displayDetails(students, MAX_STUDENTS);
// Calculate and display student-wise and subject-wise total marks
calculateTotals(students, MAX_STUDENTS);
return 0;
}
// Function to enter details for 5 students using a pointer to structure
void enterDetails(struct Student *students, int numStudents) {
printf("Enter details for 5 students:\n");
for (int i = 0; i < numStudents; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Roll No: ");
scanf("%d", &(students[i].rollNo));
printf("Name: ");
scanf("%s", students[i].name);
printf("Semester: ");
scanf("%d", &(students[i].semester));
printf("Enter marks for 3 subjects:\n");
for (int j = 0; j < NUM_SUBJECTS; j++) {
printf("Subject %d: ", j + 1);
scanf("%d", &(students[i].marks[j]));
}
}
}
// Function to display details for 5 students using a pointer to structure
void displayDetails(const struct Student *students, int numStudents) {
printf("\nStudent details:\n");
for (int i = 0; i < numStudents; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Roll No: %d\n", students[i].rollNo);
printf("Name: %s\n", students[i].name);
printf("Semester: %d\n", students[i].semester);
printf("Marks in 3 subjects: ");
for (int j = 0; j < NUM_SUBJECTS; j++) {
printf("%d ", students[i].marks[j]);
}
printf("\n");
}
}
// Function to calculate and display student-wise and subject-wise total marks
void calculateTotals(const struct Student *students, int numStudents) {
printf("\nStudent-wise and Subject-wise Total Marks:\n");
for (int i = 0; i < numStudents; i++) {
int studentTotal = 0;
printf("\nStudent %d - %s:\n", i + 1, students[i].name);
// Calculate and display subject-wise total marks for each student
printf("Subject-wise Total Marks: ");
for (int j = 0; j < NUM_SUBJECTS; j++) {
int subjectTotal = 0;
for (int k = 0; k < numStudents; k++) {
subjectTotal += students[k].marks[j];
}
printf("Subject %d: %d ", j + 1, subjectTotal);
}
// Calculate and display student-wise total marks
for (int j = 0; j < NUM_SUBJECTS; j++) {
studentTotal += students[i].marks[j];
}
printf("\nTotal Marks for Student %d: %d\n", i + 1, studentTotal);
}
}