0% found this document useful (0 votes)
5 views2 pages

Program 1

Uploaded by

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

Program 1

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

struct calElement {
char *day;
int date;
char *activity;
};
struct calElement* create(int numDays) {
struct calElement *calendar;
calendar = (struct calElement *)malloc(sizeof(struct calElement) * numDays);
if (calendar == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
return calendar;
}
void read(struct calElement *calendar, int numDays) {
char tempDay[50], tempActivity[200];
int i;
for (i = 0; i < numDays; i++) {
printf("Enter details for day %d:\n", i + 1);
printf("Enter the name of the day: ");
scanf("%s", tempDay);
calendar[i].day = (char *)malloc(strlen(tempDay) + 1);
strcpy(calendar[i].day, tempDay);
printf("Enter the date: ");
scanf("%d", &calendar[i].date);
getchar();
printf("Enter the activity description: ");
fgets(tempActivity, sizeof(tempActivity), stdin);
tempActivity[strcspn(tempActivity, "\n")] = 0;
calendar[i].activity = (char *)malloc(strlen(tempActivity) + 1);
strcpy(calendar[i].activity, tempActivity);

printf("\n");
}
}
void display(struct calElement *calendar, int numDays) {
int i;
printf("\n\n------ Weekly Activity Report ------\n");
printf("Day\t\tDate\t\tActivity\n");
printf("--------------------------------------------------\n");
for (i = 0; i < numDays; i++) {
printf("%-10s\t%-5d\t%s\n", calendar[i].day, calendar[i].date,
calendar[i].activity);
}
}
void cleanup(struct calElement *calendar, int numDays) {
int i;
for (i = 0; i < numDays; i++) {
free(calendar[i].day);
free(calendar[i].activity);
}
free(calendar);
}
int main() {
int numDays;

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


scanf("%d", &numDays);
getchar();

if (numDays <= 0) {
printf("Invalid number of days.\n");
return 1;
}

struct calElement *calendar = create(numDays);


read(calendar, numDays);
display(calendar, numDays);
cleanup(calendar, numDays);
return 0;
}

You might also like