Array as Member of Structure in C
Last updated on July 27, 2020
Since the beginning of this chapter, we have already been using arrays as members inside structures. Nevertheless, let's discuss it one more time. For example:
1 2 3 4 5 6 | struct student
{
char name[20];
int roll_no;
float marks;
};
|
The student
structure defined above has a member name
which is an array of 20 characters.
Let's create another structure called student to store name, roll no and marks of 5 subjects.
1 2 3 4 5 6 | struct student
{
char name[20];
int roll_no;
float marks[5];
};
|
If student_1
is a variable of type struct student
then:
student_1.marks[0]
- refers to the marks in the first subject
student_1.marks[1]
- refers to the marks in the second subject
and so on. Similarly, if arr_student[10]
is an array of type struct student
then:
arr_student[0].marks[0]
- refers to the marks of first student in the first subject arr_student[1].marks[2]
- refers to the marks of the second student in the third subject
and so on.
The following program asks the user to enter name, roll no and marks in 2 subjects and calculates the average marks of each student.
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 | #include<stdio.h>
#include<string.h>
#define MAX 2
#define SUBJECTS 2
struct student
{
char name[20];
int roll_no;
float marks[SUBJECTS];
};
int main()
{
struct student arr_student[MAX];
int i, j;
float sum = 0;
for(i = 0; i < MAX; i++ )
{
printf("\nEnter details of student %d\n\n", i+1);
printf("Enter name: ");
scanf("%s", arr_student[i].name);
printf("Enter roll no: ");
scanf("%d", &arr_student[i].roll_no);
for(j = 0; j < SUBJECTS; j++)
{
printf("Enter marks: ");
scanf("%f", &arr_student[i].marks[j]);
}
}
printf("\n");
printf("Name\tRoll no\tAverage\n\n");
for(i = 0; i < MAX; i++ )
{
sum = 0;
for(j = 0; j < SUBJECTS; j++)
{
sum += arr_student[i].marks[j];
}
printf("%s\t%d\t%.2f\n",
arr_student[i].name, arr_student[i].roll_no, sum/SUBJECTS);
}
// signal to operating system program ran fine
return 0;
}
|
Expected Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Enter details of student 1
Enter name: Rick
Enter roll no: 1
Enter marks: 34
Enter marks: 65
Enter details of student 2
Enter name: Tim
Enter roll no: 2
Enter marks: 35
Enter marks: 85
Name Roll no Average
Rick 1 49.50
Tim 2 60.00
|
How it works:
In line 3 and 4, we have declared two symbolic constants MAX
and SUBJECTS
which controls the number of students and subjects respectively.
In lines 6-11, we have declared a structure student which have three members namely name
, roll_no
and marks
.
In line 15, we have declared an array of structures arr_student
of size MAX
.
In line 16, we have declared two int
variables i
, j
to control loops.
In line 17, we have declared a float
variable sum
and initialized it to 0
. This variable will be used to accumulate marks of a particular student.
In line 19-34, we have a for loop which asks the user to enter the details of the student. Inside this for loop, we have a nested for loop which asks the user to enter the marks obtained by the students in various subjects.
In line 40-50, we have another for loop whose job is to print the details of the student. Notice that after each iteration the sum
is reinitialized to 0
, this is necessary otherwise we will not get the correct answer. The nested for loop is used to accumulate the marks of a particular student in the variable sum. At last the print statement in line 48, prints all the details of the student.
Load Comments