- 2072 Chaitra · 8 marks
College records with nested date structure
Short answer
Define a nested structure date inside college. Read 10 sets using scanf with &c[i].estDate.day, &c[i].estDate.month, &c[i].estDate.year. Display in a table with date formatted using %02d/%02d/%-6d.
Write a program to read structure "college" having name, estDate and location where estDate is another structure having day, month and year as members. Display the records of 10 colleges.
Design. The structure date (day, month, year) is defined first. The structure college contains name, estDate (a struct date, the nested structure) and location. An array of 10 colleges is read in a loop; the nested members are reached with two dots, such as c[i].estDate.year. The records are then displayed in a table.
#include <stdio.h>
#define N 10
struct date {
int day, month, year;
};
struct college {
char name[40];
struct date estDate; /* nested structure */
char location[30];
};
int main(void)
{
struct college c[N];
for (int i = 0; i < N; i++) {
printf("College %d name: ", i + 1);
scanf("%39s", c[i].name);
printf("Established date (day month year): ");
scanf("%d %d %d", &c[i].estDate.day, &c[i].estDate.month, &c[i].estDate.year);
printf("Location: ");
scanf("%29s", c[i].location);
}
printf("\n%-12s %-12s %s\n", "Name", "Est. date", "Location");
for (int i = 0; i < N; i++)
printf("%-12s %02d/%02d/%-6d %s\n", c[i].name, c[i].estDate.day,
c[i].estDate.month, c[i].estDate.year, c[i].location);
return 0;
}
Sample run (10 colleges entered; only the output after the input is shown)
Input (entered one after another): Pulchowk 5 10 1984 Lalitpur Thapathali 9 2 2011 Kathmandu Purwanchal 25 8 2010 Dharan Paschimanchal 21 7 1993 Pokhara Kathmandu 4 8 1981 Kavre Pokhara 27 7 2007 Pokhara Himalaya 20 1 2008 Lalitpur Janakpur 9 12 1994 Dhanusha Nepal 19 2 2000 Banepa Eastern 1 1 1981 Biratnagar
Name Est. date Location
Pulchowk 05/10/1984 Lalitpur
Thapathali 09/02/2011 Kathmandu
Purwanchal 25/08/2010 Dharan
Paschimanchal 21/07/1993 Pokhara
Kathmandu 04/08/1981 Kavre
Pokhara 27/07/2007 Pokhara
Himalaya 20/01/2008 Lalitpur
Janakpur 09/12/1994 Dhanusha
Nepal 19/02/2000 Banepa
Eastern 01/01/1981 Biratnagar
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Display records sorted by name from file · Computer Programming, chapter 10
- Read records until end; show nth record · Computer Programming, chapter 10 · asked 2×
- Height difference using nested structure · Computer Programming, chapter 8
- Student records with two functions · Computer Programming, chapter 8
- Sending structure members to a function · Computer Programming, chapter 8
- Insert a string into another string · Computer Programming, chapter 7
- Date of birth validation with exceptions · Object Oriented Programming, chapter 10
- File stream; student binary file program · Object Oriented Programming, chapter 8
- Student records file with CRN search · Object Oriented Programming, chapter 8
- Write and display 10 employees using file · Object Oriented Programming, chapter 8