0% found this document useful (0 votes)
13 views

Lab program 1

ds lab program1

Uploaded by

nehacg33
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab program 1

ds lab program1

Uploaded by

nehacg33
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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>

#include <string.h>

// Structure to represent a day in the calendar

struct Day {

char *dayName;

int date, month, year;

char *activity;

};

// Function to create a calendar

struct Day* createCalendar(int numDays) {

struct Day *calendar = (struct Day*)malloc(numDays * sizeof(struct Day));

if (calendar == NULL) {

printf("Memory allocation failed\n");

exit(1);

for (int i = 0; i < numDays; i++) {

calendar[i].dayName = (char*)malloc(20 * sizeof(char)); // Assuming day names are not longer than
20 characters

1
calendar[i].activity = (char*)malloc(100 * sizeof(char)); // Assuming activity descriptions are not
longer than 100 characters

return calendar;

// Function to read data for a day from the keyboard

void readDay(struct Day *day) {

printf("Enter day name: ");

scanf("%s", day->dayName);

printf("Enter date: ");

scanf("%d%d%d", &(day->date),&(day->month),&(day->year));

printf("Enter activity description: ");

scanf(" %[^\n]", day->activity);

// Function to display the calendar

void displayCalendar(struct Day *calendar, int numDays) {

printf("\nWeek's Activity Details:\n");

for (int i = 0; i < numDays; i++) {

printf("Day: %s\n", calendar[i].dayName);

printf("Date: %d%d%d\n", calendar[i].date, calendar[i].month, calendar[i].year);

printf("Activity: %s\n", calendar[i].activity);

printf("\n");

2
int main() {

int numDays;

printf("Enter the number of days in the week: ");

scanf("%d", &numDays);

struct Day *calendar = createCalendar(numDays);

for (int i = 0; i < numDays; i++) {

printf("Enter details for Day %d:\n", i + 1);

readDay(&calendar[i]);

displayCalendar(calendar, numDays);

// Free allocated memory

for (int i = 0; i < numDays; i++) {

free(calendar[i].dayName);

free(calendar[i].activity);

free(calendar);

return 0;

3
OUTPUT

You might also like