1. Develop a Program in C for the following: a.
Declare a calendar as an array
of 7 elements (A dynamically Created array) to represent 7 days of a week. Each
Element of the array is a structure having three fields. The first field is the name
of the Day (A dynamically allocated String), The second field is the date of the
Day (A integer), the third field is the description of the activity for a particular
day (A dynamically allocated String).
b. Write functions create(), read() and display(); to create the calendar, to read
the data from the keyboard and to print weeks activity details report on screen.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure to represent a day
struct Day {
char *dayName;
int date;
char *activity;
};
// Function to create a day in the calendar
void create(struct Day calendar[], int index) {
char temp[50];
printf("Enter the name of the day: ");
scanf("%s", temp);
calendar[index].dayName = (char *)malloc(strlen(temp) + 1);
strcpy(calendar[index].dayName, temp);
printf("Enter the date of the day: ");
scanf("%d", &calendar[index].date);
printf("Enter the activity for the day: ");
scanf("%s", temp);
calendar[index].activity = (char *)malloc(strlen(temp) + 1);
strcpy(calendar[index].activity, temp);
}
// Function to read data from the keyboard for all days
void read(struct Day calendar[]) {
int i;
for (i = 0; i < 7; i++) {
printf("Enter details for Day %d:\n", i + 1);
create(calendar, i);
}
}
// Function to display the calendar
void display(struct Day calendar[]) {
int i;
printf("\nDay\tDate\tActivity\n");
for (i = 0; i < 7; i++) {
printf("%s\t%d\t%s\n", calendar[i].dayName, calendar[i].date,
calendar[i].activity);
}
}
int main() {
int i;
// Declare an array of Day structures
struct Day calendar[7];
// Read data for each day
read(calendar);
// Display the calendar
display(calendar);
// Free dynamically allocated memory
for (i = 0; i < 7; i++) {
free(calendar[i].dayName);
free(calendar[i].activity);
}
return 0;
}
Sample Output 1
Enter details for Day 1:
Enter the name of the day: Monday
Enter the date of the day: 1
Enter the activity for the day: Meeting
Enter details for Day 2:
Enter the name of the day: Tuesday
Enter the date of the day: 2
Enter the activity for the day: Exercise
Enter details for Day 3:
Enter the name of the day: Wednesday
Enter the date of the day: 3
Enter the activity for the day: Work
Enter details for Day 4:
Enter the name of the day: Thursday
Enter the date of the day: 4
Enter the activity for the day: Study
Enter details for Day 5:
Enter the name of the day: Friday
Enter the date of the day: 5
Enter the activity for the day: Party
Enter details for Day 6:
Enter the name of the day: Saturday
Enter the date of the day: 6
Enter the activity for the day: Relax
Enter details for Day 7:
Enter the name of the day: Sunday
Enter the date of the day: 7
Enter the activity for the day: Family time
Day Date Activity
Monday 1 Meeting
Tuesday 2 Exercise
Wednesday 3 Work
Thursday 4 Study
Friday 5 Party
Saturday 6 Relax
Sunday 7 Family time