Program 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.
Program
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a day in the calendar
struct Day
char *name; // for dynamically allocating memory for name
int date;
char *activity; // for dynamically allocating memory for name
};
// Function to allocate memory Dynamically for each variable of structure
void create(struct Day calendar[], int size)
for (int i = 0; i < size; i++)
calendar[i].name = (char *)malloc(20 * sizeof(char));
calendar[i].activity = (char *)malloc(20 * sizeof(char));
}
// Function to read data from the keyboard
void read(struct Day calendar[], int size)
for (int i = 0; i < size; i++)
printf("Enter details for day %d:\n", i + 1);
printf("Enter day name: ");
scanf("%s", calendar[i].name);
printf("Enter date: ");
scanf("%d", &calendar[i].date);
printf("Enter activity: ");
scanf(" %[^\n]s", calendar[i].activity); //Space before %[^\n]s
// Function to display the calendar
void display(struct Day calendar[], int size)
printf("\n\nWeek's Activity Details Report:\n");
for (int i = 0; i < size; i++)
printf("Day %d:\t", i + 1);
printf("Day Name: %s\t", calendar[i].name);
printf("Date: %d\t", calendar[i].date);
printf("Activity: %s\n", calendar[i].activity);
// Function to free memory allocated for the calendar
void freeMemory(struct Day calendar[], int size)
for (int i = 0; i < size; i++)
free(calendar[i].name);
free(calendar[i].activity);
int main()
int size;
printf("Enter the number of days in the calendar: ");
scanf("%d", &size);
struct Day calendar[size]; // Declare array of structure as required by user
create(calendar, size); // Allocate Memory
if (calendar == NULL) //check if memory allocation failed
printf("Memory allocation failed.\n");
return 1;
}
read(calendar,size);
display(calendar, size); // Call the display function to print the calendar
freeMemory(calendar, size); // Free memory allocated for the calendar
return 0;
Output:
Enter the number of days in the calendar: 7
Enter details for day 1:
Enter day name: Monday Enter date: 01 Enter activity: Reading
Enter details for day 2:
Enter day name: Tuesday Enter date: 02 Enter activity: Writing
Enter details for day 3:
Enter day name: Wednesday Enter date: 03 Enter activity: Revision
Enter details for day 4:
Enter day name: Thursday Enter date: 04 Enter activity: Assignment
Enter details for day 5:
Enter day name: Friday Enter date: 05 Enter activity: Quiz
Enter details for day 6:
Enter day name: Saturday Enter date: 06 Enter activity: Weekend
Enter details for day 7:
Enter day name: Sunday Enter date: 07 Enter activity: Holiday
Week's Activity Details Report:
Day 1: Day Name: Monday Date: 1 Activity: Reading
Day 2: Day Name: Tuesday Date: 2 Activity: Writing
Day 3: Day Name: Wednesday Date: 3 Activity: Revision
Day 4: Day Name: Thursday Date: 4 Activity: Assignment
Day 5: Day Name: Friday Date: 5 Activity: Quiz
Day 6: Day Name: Saturday Date: 6 Activity: Weekend
Day 7: Day Name: Sunday Date: 7 Activity: Holiday