0% found this document useful (0 votes)
2 views3 pages

Program 1

The document outlines a C programming assignment for creating a weekly calendar using a dynamically allocated array of structures. Each structure contains fields for the day's name, date, and activity description, with functions to create, read, and display the calendar. Memory management is emphasized, with proper allocation and deallocation of resources to prevent memory leaks.

Uploaded by

007007pradees
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)
2 views3 pages

Program 1

The document outlines a C programming assignment for creating a weekly calendar using a dynamically allocated array of structures. Each structure contains fields for the day's name, date, and activity description, with functions to create, read, and display the calendar. Memory management is emphasized, with proper allocation and deallocation of resources to prevent memory leaks.

Uploaded by

007007pradees
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

DATA STRUCTURES LABORATORY - BCSL305

Program 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.

#include <stdio.h>
#include <stdlib.h>

#define DAYS 7
#define NAME_MAX 20
#define DESC_MAX 100

typedef struct {
char *name; // dynamically allocated string
int date; // integer date
char *description; // dynamically allocated string
} Day;

// Allocate memory for the 7 Day records and their strings


void create(Day *week, int size) {
for (int i = 0; i < size; i++) {
week[i].name = (char *)malloc(NAME_MAX * sizeof(char));
week[i].description = (char *)malloc(DESC_MAX * sizeof(char));
if (!week[i].name || !week[i].description) {
printf("Memory allocation failed.\n");
// Free anything already allocated before exiting
for (int j = 0; j <= i; j++) {
free(week[j].name);
free(week[j].description);
}
exit(1);
}
}
}

// Read week data from keyboard (handles spaces in description)


void read(Day *week, int size) {
for (int i = 0; i < size; i++) {
printf("Enter name of day %d (e.g., Monday): ", i + 1);
scanf(" %19s", week[i].name); // limit to avoid
overflow

printf("Enter date (number): ");


scanf("%d", &week[i].date);

printf("Enter activity/description: ");


scanf(" %99[^\n]", week[i].description); // read full line
incl. spaces
}
}

// Print the week's activity report


void display(const Day *week, int size) {
printf("\nWeek's Activity Details:\n");
for (int i = 0; i < size; i++) {
printf("Day: %s, Date: %d, Activity: %s\n",
week[i].name, week[i].date, week[i].description);
}
}
int main(void) {
Day *week = (Day *)malloc(DAYS * sizeof(Day)); // dynamically
create array
if (!week) {
printf("Memory allocation failed.\n");
return 1;
}

create(week, DAYS); // allocate strings


read(week, DAYS); // take input
display(week, DAYS); // show report

// Free all allocated memory


for (int i = 0; i < DAYS; i++) {
free(week[i].name);
free(week[i].description);
}
free(week);

return 0;
}

You might also like