Program 01
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).
Solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure for a day
struct Day {
char *dayName;
int date;
char *activity;
};
// Function to initialize a day
struct Day initializeDay(char *name, int date, char *activity) {
struct Day day;
[Link] = strdup(name);
[Link] = date;
[Link] = strdup(activity);
return day;
}
// Function to display a day
void displayDay(struct Day *day) {
printf("%s - %d: %s\n", day->dayName, day->date, day->activity);
}
int main() {
// Number of days in a week
int numDays = 7;
// Declare a dynamically created array of days
struct Day *calendar = (struct Day *)malloc(numDays * sizeof(struct Day));
// Initialize each day in the calendar
calendar[0] = initializeDay("Sunday", 1, "Relaxing");
calendar[1] = initializeDay("Monday", 2, "Work");
calendar[2] = initializeDay("Tuesday", 3, "Meeting");
calendar[3] = initializeDay("Wednesday", 4, "Exercise");
calendar[4] = initializeDay("Thursday", 5, "Study");
calendar[5] = initializeDay("Friday", 6, "Socializing");
calendar[6] = initializeDay("Saturday", 7, "Family Time");
// Display the calendar
printf("Calendar:\n");
for (int i = 0; i < numDays; i++) {
displayDay(&calendar[i]);
}
// Free dynamically allocated memory
for (int i = 0; i < numDays; i++) {
free(calendar[i].dayName);
free(calendar[i].activity);
}
free(calendar);
return 0;
}
Output
Program 1b
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.
Solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure for a day
struct Day {
char *dayName;
int date;
char *activity;
};
// Function to initialize a day
struct Day initializeDay(char *name, int date, char *activity) {
struct Day day;
[Link] = strdup(name);
[Link] = date;
[Link] = strdup(activity);
return day;
}
// Function to create the calendar
struct Day *createCalendar(int numDays) {
struct Day *calendar = (struct Day *)malloc(numDays * sizeof(struct Day));
return calendar;
}
// Function to read data from the keyboard
void readData(struct Day *calendar, int numDays) {
for (int i = 0; i < numDays; i++) {
printf("Enter details for day %d:\n", i + 1);
// Dynamically allocate memory for day name and activity
char name[50];
char activity[100];
printf("Enter day name: ");
scanf("%s", name);
printf("Enter date: ");
scanf("%d", &calendar[i].date);
printf("Enter activity: ");
scanf("%s", activity);
calendar[i] = initializeDay(name, calendar[i].date, activity);
}
}
// Function to display the calendar
void displayCalendar(struct Day *calendar, int numDays) {
printf("\nCalendar:\n");
for (int i = 0; i < numDays; i++) {
printf("%s - %d: %s\n", calendar[i].dayName, calendar[i].date, calendar[i].activity);
}
}
// Function to free memory allocated for the calendar
void freeCalendar(struct Day *calendar, int numDays) {
for (int i = 0; i < numDays; i++) {
free(calendar[i].dayName);
free(calendar[i].activity);
}
free(calendar);
}
int main() {
// Number of days in a week
int numDays = 7;
// Create the calendar
struct Day *calendar = createCalendar(numDays);
// Read data from the keyboard
readData(calendar, numDays);
// Display the calendar
displayCalendar(calendar, numDays);
// Free dynamically allocated memory
freeCalendar(calendar, numDays);
return 0;
}
Output