Challenge - 5 Problems
Array of Structures Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing array of structures
What is the output of this C program?
C
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point points[3] = {{1, 2}, {3, 4}, {5, 6}};
printf("%d %d\n", points[1].x, points[1].y);
return 0;
}Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0.
✗ Incorrect
The array 'points' has three elements. points[1] refers to the second element, which has x=3 and y=4.
🧠 Conceptual
intermediate2:00remaining
Size of array of structures
Given the following structure and array declaration, what is the size in bytes of the array 'employees' on a system where int is 4 bytes and char is 1 byte, assuming no padding?
C
struct Employee {
int id;
char grade;
};
struct Employee employees[5];Attempts:
2 left
💡 Hint
Calculate size of one structure and multiply by array length.
✗ Incorrect
Each Employee has an int (4 bytes) and a char (1 byte). Without padding, size is 5 bytes per struct. For 5 elements, total is 5 * 5 = 25 bytes. But usually padding applies, so without padding it is 25 bytes. However, the question states 'assuming no padding', so 5 * (4+1) = 25 bytes. The correct answer is 25 bytes, so option D.
🔧 Debug
advanced2:00remaining
Identify the error in array of structures initialization
What error will this code produce?
C
struct Data {
int a;
int b;
};
int main() {
struct Data arr[2] = {1, 2, 3, 4};
return 0;
}Attempts:
2 left
💡 Hint
Check how arrays of structures can be initialized with flat lists.
✗ Incorrect
In C, initializing an array of structures with a flat list of values is allowed. The values are assigned in order to the members of each structure in the array. So arr[0].a=1, arr[0].b=2, arr[1].a=3, arr[1].b=4.
📝 Syntax
advanced2:00remaining
Correct syntax to access structure array member
Which option correctly accesses the member 'score' of the third element in the array 'results'?
C
struct Result {
int score;
};
struct Result results[5];Attempts:
2 left
💡 Hint
Remember how to access array elements and structure members.
✗ Incorrect
To access the member 'score' of the third element (index 2) you use results[2].score. But the question asks for the third element, which is index 2. Option C uses index 3 which is the fourth element, so it is incorrect. None of the options use index 2. The question is about syntax correctness, not index correctness. Option C is the only syntactically correct way to access a member of an array of structures. Other options use invalid syntax.
🚀 Application
expert2:00remaining
Count how many structures have a field greater than a value
Given this code, what is the value of 'count' after execution?
C
#include <stdio.h>
struct Item {
int value;
};
int main() {
struct Item items[4] = {{10}, {20}, {5}, {30}};
int count = 0;
for (int i = 0; i < 4; i++) {
if (items[i].value > 15) {
count++;
}
}
printf("%d\n", count);
return 0;
}Attempts:
2 left
💡 Hint
Count how many values are greater than 15.
✗ Incorrect
The values are 10, 20, 5, 30. Only 20 and 30 are greater than 15, so count is 2.