0
0
Cprogramming~20 mins

Why structures are needed - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of code using structures for grouping data
What is the output of this C code that uses a structure to group related data?
C
#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point p1 = {3, 4};
    printf("x = %d, y = %d\n", p1.x, p1.y);
    return 0;
}
Ax = 0, y = 0
Bx = 3, y = 4
Cx = 4, y = 3
DCompilation error
Attempts:
2 left
💡 Hint
Look at how the structure fields are initialized and accessed.
🧠 Conceptual
intermediate
1:30remaining
Why use structures instead of separate variables?
Why are structures useful in C programming compared to using separate variables for related data?
AStructures prevent any changes to the data once set.
BStructures make the program run faster by using less memory than separate variables.
CStructures automatically sort data in ascending order.
DStructures allow grouping related data under one name, making code organized and easier to manage.
Attempts:
2 left
💡 Hint
Think about how grouping data helps in real life, like putting all your keys on one keyring.
🔧 Debug
advanced
2:00remaining
Identify the error in structure usage
What error will this code produce when compiled?
C
#include <stdio.h>
#include <string.h>

struct Person {
    char name[20];
    int age;
};

int main() {
    struct Person p1;
    p1.name = "Alice";
    p1.age = 30;
    printf("Name: %s, Age: %d\n", p1.name, p1.age);
    return 0;
}
ACompilation error: assignment to array is not allowed
BCompilation error: missing semicolon after struct
COutput: Name: Alice, Age: 30
DRuntime error: segmentation fault
Attempts:
2 left
💡 Hint
In C, arrays cannot be assigned directly with = operator.
Predict Output
advanced
2:00remaining
Output of nested structures
What is the output of this code using nested structures?
C
#include <stdio.h>

struct Date {
    int day;
    int month;
    int year;
};

struct Event {
    char title[20];
    struct Date date;
};

int main() {
    struct Event e = {"Meeting", {15, 8, 2024}};
    printf("%s on %d/%d/%d\n", e.title, e.date.day, e.date.month, e.date.year);
    return 0;
}
ACompilation error due to nested structure initialization
BMeeting on 8/15/2024
CMeeting on 15/8/2024
DMeeting on 0/0/0
Attempts:
2 left
💡 Hint
Look at how the nested structure is initialized inside the outer structure.
🧠 Conceptual
expert
2:30remaining
Why structures improve program design
Which statement best explains why structures are important for program design in C?
AStructures allow grouping different types of data into one unit, making programs easier to understand and maintain.
BStructures automatically optimize memory usage better than arrays.
CStructures enforce strict data privacy and prevent unauthorized access.
DStructures replace the need for functions by storing code inside them.
Attempts:
2 left
💡 Hint
Think about how grouping related data helps programmers keep code clear and organized.