PSUC Lab 12 — Composite Data Structures
Active Logic: Structures (struct)
Master user-defined composite data types in C: struct definition, member access operator (.), arrow operator (->), array of structures, structure sorting, and date arithmetic.
Structure Memory & Member Rules
| Concept | Syntax / Rule | Key Trap / Pitfall |
|---|---|---|
| Member Access | p1.name or ptr->name | Use . for structure variable, -> for pointer! |
| Nested Struct | p1.doj.day | Inner structure member accessed sequentially |
| Structure Swap | temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; | Entire structure assignment copies all members at once! |
| Date Addition | Add days, rollover month if d > daysInMonth | Leap year February has 29 days! |
🎯 Learning Philosophy: Active Logic Building
Test your structure logic across 3 Interactive Learning Modes:
- 🧠Tab 1 (Logic & Hints): Struct layout & member access.
- 🧩 Tab 2 (Logic Puzzle): Complete missing dot/arrow operators & test live date calculators!
- 💻 Tab 3 (C Solution): Reference verified C code solutions.
Solve structure logic puzzles before peeking at C reference code!
Exercise 1 of 4
Personal Information Structure (struct Personal)
Defines nested structure containing personal name, date of joining (day, month, year), and salary.
Nested Structure Definition
struct Date { int day, month, year; };struct Personal { char name[50]; struct Date doj; float salary; };- Accessing DOJ:
p1.doj.day
Interactive Logic Hints
💡 Hint 1: Can a structure contain another structure as member? ▼
Yes! This is called Nested Structure!
🧩 Complete Member Access Operators
struct Personal p1;
scanf("%s", p1name);
scanf("%d", &p1dojday);
scanf("%s", p1name);
scanf("%d", &p1dojday);
Select statements above and click Check!
👤 Personal Profile Generator
Profile summary...
personal_struct.cReference C Code
#include <stdio.h>
struct Date { int day, month, year; };
struct Personal {
char name[50];
struct Date doj;
float salary;
};
int main() {
struct Personal p1 = {"Abhay Bisht", {15, 8, 2024}, 75000.0};
printf("Name: %s\n", p1.name);
printf("DOJ: %02d/%02d/%d\n", p1.doj.day, p1.doj.month, p1.doj.year);
printf("Salary: Rs. %.2f\n", p1.salary);
return 0;
}
Exercise 2 of 4
Student Array Sorted by Total Marks
Sorts an array of student structures in decreasing order of total marks.
Structure Array Sorting
- Compare totals:
if (s[j].total < s[j + 1].total) - Swap entire structures:
struct Student temp = s[j]; s[j] = s[j+1]; s[j+1] = temp;
Interactive Logic Hints
💡 Hint 1: Can we swap whole struct variables with single = sign? ▼
YES! C supports direct assignment
struct_a = struct_b! All members copy automatically!🧩 Complete Decreasing Order Swap
if (s[j].total s[j + 1].total) {
struct Student temp = s[j];
s[j] = s[];
s[j + 1] = ;
}
struct Student temp = s[j];
s[j] = s[];
s[j + 1] = ;
}
Select statements above and click Check!
🎓 Student Merit Rank Generator
Sorted Merit List...
student_sort.cReference C Code
#include <stdio.h>
struct Student {
int roll; char name[50]; float total;
};
int main() {
struct Student s[3] = {
{101, "Rahul", 245.5}, {102, "Priya", 289.0}, {103, "Amit", 210.0}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2 - i; j++) {
if (s[j].total < s[j + 1].total) {
struct Student temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
printf("Merit List:\n");
for (int i = 0; i < 3; i++) {
printf("Rank %d: %s (Total: %.1f)\n", i + 1, s[i].name, s[i].total);
}
return 0;
}
Exercise 3 of 4
Employee Net Salary & Filter
Computes Net Salary (Net = Basic + DA + HRA) and filters employees earning above average.
Salary Breakdown Formula
DA = basic * 0.52|HRA = basic * 0.15netSalary = basic + DA + HRA
Interactive Logic Hints
💡 Hint 1: How to filter above-average earners? ▼
Compute total company salary average first, then loop again:
if (e[i].net > avgNet) print(e[i]);🧩 Complete Net Salary Calculation
e[i].da = e[i].basic * ;
e[i].hra = e[i].basic * ;
e[i].net = e[i].basic e[i].da + e[i].hra;
e[i].hra = e[i].basic * ;
e[i].net = e[i].basic e[i].da + e[i].hra;
Select statements above and click Check!
💼 Employee Net Salary Visualizer
Net salary breakdown...
emp_salary.cReference C Code
#include <stdio.h>
struct Employee {
char name[50]; float basic, da, hra, net;
};
int main() {
struct Employee e[3] = {
{"Suresh", 30000}, {"Ramesh", 45000}, {"Mahesh", 50000}
};
float totalNet = 0;
for (int i = 0; i < 3; i++) {
e[i].da = e[i].basic * 0.52;
e[i].hra = e[i].basic * 0.15;
e[i].net = e[i].basic + e[i].da + e[i].hra;
totalNet += e[i].net;
}
float avgNet = totalNet / 3.0;
printf("Avg Net Salary = Rs. %.2f\nAbove Avg Earners:\n", avgNet);
for (int i = 0; i < 3; i++) {
if (e[i].net > avgNet) printf("%s: Rs. %.2f\n", e[i].name, e[i].net);
}
return 0;
}
Exercise 4 of 4
Date Addition Algorithm (+45 Days)
Adds 45 days to current date and handles month/year rollovers accurately.
Month Rollover Logic
- Add 45:
d.day += 45; - Loop:
while (d.day > getDaysInMonth(d.month, d.year)) - Deduct month days:
d.day -= daysInMonth; d.month++; - Year rollover:
if (d.month > 12) { d.month = 1; d.year++; }
Interactive Logic Hints
💡 Hint 1: What about Leap Years? ▼
February has 29 days if
year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)!🧩 Complete Date Addition Rollover Loop
d.day += 45;
while (d.day > daysInMonth(d.month, d.year)) {
d.day daysInMonth(d.month, d.year);
d.month;
if (d.month > 12) { d.month = 1; d.year; }
}
while (d.day > daysInMonth(d.month, d.year)) {
d.day daysInMonth(d.month, d.year);
d.month;
if (d.month > 12) { d.month = 1; d.year; }
}
Select statements above and click Check!
📅 Date Addition (+45 Days) Calculator
Date addition result...
date_add.cReference C Code
#include <stdio.h>
#include <stdbool.h>
struct Date { int day, month, year; };
bool isLeap(int y) { return (y%400==0 || (y%4==0 && y%100!=0)); }
int daysInMonth(int m, int y) {
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (m == 2 && isLeap(y)) return 29;
return days[m];
}
int main() {
struct Date d = {15, 8, 2024};
d.day += 45;
while (d.day > daysInMonth(d.month, d.year)) {
d.day -= daysInMonth(d.month, d.year);
d.month++;
if (d.month > 12) { d.month = 1; d.year++; }
}
printf("New Date (+45 days): %02d/%02d/%d\n", d.day, d.month, d.year);
return 0;
}
Knowledge Check
Test Your Structures Mastery
Question 1 of 5
Loading question...