QUESTION 05
What is an array of structures?
2 marksAnswer
It is an array whose elements share one structure type. a[i] selects a record and a[i].member selects one member of that record.
QUESTION 05
It is an array whose elements share one structure type. a[i] selects a record and a[i].member selects one member of that record.
QUESTION 06
An array of structures stores several records of the same kind in contiguous array storage.
struct Student { int roll; int marks; };
struct Student students[3] = {{101, 70}, {102, 85}, {103, 80}};students[1] is the second complete record. students[1].marks selects its mark, 85. A loop from index 0 through 2 can process every record. Each structure element contains its own members; changing one record’s mark does not change another record.
For marks 70, 85 and 80, best starts at 0, changes to 1 on seeing 85 and remains 1 for 80. The chosen roll is 102. With a strict greater-than comparison, an equal later mark does not replace the first maximum.
A single member access is O(1). Finding the largest mark scans n records and takes O(n) time with O(1) auxiliary space. The input records themselves require O(n) storage. Keep the active count within the actual array capacity; a structure member does not remove normal C array-bound rules.
/* CodeBhavya | Data Structures AR25 | Learning example */
#include <stdio.h>
struct Student { int roll, marks; };
int main(void)
{
struct Student s[3] = {{101, 70}, {102, 85}, {103, 80}};
int best = 0;
for (int i = 1; i < 3; ++i) { if (s[i].marks > s[best].marks) { best = i; } }
printf("Best roll = %d, marks = %d\n", s[best].roll, s[best].marks);
return 0;
}The example uses values in the code.
Best roll = 102, marks = 85