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

Lab 1 Program

Uploaded by

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

Lab 1 Program

Uploaded by

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

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>

struct Day {
char* name;
struct dateofactivity{
int dd;
int mm;
int yy;
}date;
char* activity;
};

void create(struct Day* calendar) {


for (int i = 0; i < 7; i++) {
calendar[i].name = (char*)malloc(20 * sizeof(char));
calendar[i].activity = (char*)malloc(100 * sizeof(char));
}
}

void read(struct Day* calendar) {


for (int i = 0; i < 7; i++) {
printf("Enter the name of day %d: ", i + 1);
scanf("%s", calendar[i].name);
printf("Enter the date of day %d: ", i + 1);
scanf("%d%d%d", &calendar[i].date.dd,&calendar[i].date.mm,&calendar[i].date.yy);
printf("Enter the activity for day %d: ", i + 1);
scanf(" %[^\n]s", calendar[i].activity);
}
}

void display(struct Day* calendar) {


printf("\nCalendar:\n");
for (int i = 0; i < 7; i++) {
printf("Day %d: %s\n", i + 1, calendar[i].name);
printf("Date: %02d/%02d/%02d\n",
calendar[i].date.dd,calendar[i].date.mm,calendar[i].date.yy);
printf("Activity: %s\n\n", calendar[i].activity);
}
}

int main() {
struct Day* calendar = (struct Day*)malloc(7 * sizeof(struct Day));
create(calendar);
read(calendar);
display(calendar);

// Free dynamically allocated memory


for (int i = 0; i < 7; i++) {
free(calendar[i].name);
free(calendar[i].activity);
}
free(calendar);

return 0;
}

You might also like