0% found this document useful (0 votes)
315 views5 pages

C Program: Weekly Calendar Planner

The program develops a calendar using a dynamically created array of structures to store details of each day such as the name, date and activity. It defines functions to create and allocate memory for the calendar, read user input, display the calendar details and free the allocated memory. The main function gets the number of days as input, declares the calendar array, calls the functions to populate, display and free the calendar.

Uploaded by

Akhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
315 views5 pages

C Program: Weekly Calendar Planner

The program develops a calendar using a dynamically created array of structures to store details of each day such as the name, date and activity. It defines functions to create and allocate memory for the calendar, read user input, display the calendar details and free the allocated memory. The main function gets the number of days as input, declares the calendar array, calls the functions to populate, display and free the calendar.

Uploaded by

Akhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

You might also like