EXPERIMENT: 1
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.
ABOUT THE EXPERIMENT:
The program is a C-language application that uses dynamically created arrays and structures with
dynamically allocated strings to manage a weekly calendar. Users can create, read, and display
data for each day of the week, including the day name and activity description. The program does
not include an "exit" option but effectively demonstrates dynamic memory allocation and struct-
based data management for a flexible calendar system.
ALGORITHM:
Step 1: Start.
Step 2: Define a structure Day with three fields dayName, date, and activity
Step 3:Write a create function to create a day by dynamically allocating memory
for the dayName and activity.
Step 4: Write a read function to read data for all 7 days using the create function.
Step 5: Write a display function to print the calendar's contents.
Step 6: In the main function, declare an array of Day structures for the calendar.
Step 7: Read data for each day using the read function.
Step 8: Display the calendar using the display function.
Step 9 : Free dynamically allocated memory to prevent memory leaks.
Step 10: Stop
PROGRAM CODE:
#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[]) {
for (int 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[]) {
printf("\nDay\tDate\tActivity\n");
for (int i = 0; i < 7; i++) {
printf("%s\t%d\t%s\n", calendar[i].dayName, calendar[i].date, calendar[i].activity);
}
}
int main() {
// 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 (int 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