Array of structures - Time & Space Complexity
When working with an array of structures, it's important to know how the time to process them grows as the array gets bigger.
We want to understand how the number of operations changes when we access or loop through these structures.
Analyze the time complexity of the following code snippet.
struct Point {
int x;
int y;
};
void printPoints(struct Point points[], int n) {
for (int i = 0; i < n; i++) {
printf("(%d, %d)\n", points[i].x, points[i].y);
}
}
This code loops through an array of Point structures and prints each point's coordinates.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and printing each structure in the array.
- How many times: Exactly once for each element, so n times.
As the number of points increases, the total work grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print operations |
| 100 | 100 print operations |
| 1000 | 1000 print operations |
Pattern observation: Doubling the number of points doubles the work.
Time Complexity: O(n)
This means the time to process the array grows in a straight line with the number of elements.
[X] Wrong: "Accessing a structure inside an array is slower and adds extra loops."
[OK] Correct: Accessing each structure is done directly by index in one loop, so it does not add extra loops or slow down beyond the main loop.
Understanding how looping through arrays of structures scales helps you explain and write efficient code when handling collections of data in real projects.
"What if we nested another loop inside to compare each point with every other point? How would the time complexity change?"
Practice
Student in C?Solution
Step 1: Understand structure array declaration
To declare an array of structures, use the structure type followed by the array name and size in square brackets.Step 2: Apply correct syntax for array of structures
SinceStudentis the structure type,Student arr[5];declares an array of 5Studentstructures.Final Answer:
Student arr[5]; -> Option AQuick Check:
Array of structures = type name[size] [OK]
- Forgetting 'struct' keyword if not typedefed
- Declaring single structure instead of array
- Using wrong syntax like struct arr[5];
age member of the 3rd element in an array students of structures?Solution
Step 1: Understand zero-based indexing in arrays
Arrays in C start at index 0, so the 3rd element is at index 2.Step 2: Access structure member using dot operator
Usestudents[2].ageto access theagemember of the 3rd element.Final Answer:
students[2].age; -> Option DQuick Check:
3rd element = index 2, use dot for member [OK]
- Using index 3 instead of 2 for 3rd element
- Using arrow operator on array elements
- Accessing member as students.age[3]
typedef struct {
int id;
char name[10];
} Item;
Item items[2] = {{1, "Pen"}, {2, "Book"}};
printf("%s", items[1].name);What will be the output?
Solution
Step 1: Identify the array initialization
The arrayitemshas two elements: first with id=1, name="Pen"; second with id=2, name="Book".Step 2: Understand printf access
items[1].nameaccesses the second element's name, which is "Book".Final Answer:
Book -> Option BQuick Check:
items[1].name = "Book" [OK]
- Confusing index 0 and 1
- Expecting id to print instead of name
- Syntax errors in initialization
struct Point {
int x, y;
};
struct Point points[3];
points[0].x = 10;
points[0].y = 20;
points[1].x = 30;
points[1].y = 40;
points[2].x = 50;
points[2].y = 60;
printf("%d", points[3].x);Solution
Step 1: Check array size and indexing
The arraypointshas size 3, valid indices are 0, 1, 2.Step 2: Identify invalid access
Accessingpoints[3]is out of bounds and causes undefined behavior.Final Answer:
Accessing points[3] is out of bounds -> Option CQuick Check:
Array index must be less than size [OK]
- Using index equal to array size
- Ignoring array bounds errors
- Assuming array auto-expands
Employee with members int id; and float salary;. How do you write a function in C that takes an array of Employee and its size, then returns the average salary?Solution
Step 1: Loop through array elements correctly
Use a for loop from 0 to n-1 and access each element withemp[i].Step 2: Sum the salary member of each element
Access salary asemp[i].salaryand add to sum, then divide by n.Final Answer:
float averageSalary(Employee emp[], int n) { float sum=0; for(int i=0;i -> Option AQuick Check:
Loop + emp[i].salary sum / n = average [OK]
- Using emp->salary inside loop instead of emp[i].salary
- Accessing emp.salary instead of emp[i].salary
- Adding whole struct instead of member
Ready to prove it?
Pass the test and this pattern moves unknown
