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

Dsa PGRM 1

Uploaded by

arvind22cse15
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)
11 views3 pages

Dsa PGRM 1

Uploaded by

arvind22cse15
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/ 3

#include <stdio.

h>

#include <stdlib.h>

//Define a structure named ‘Day’ to represent a Day’s detail

struct Day {

char *dayName; //Pointer to store the day name as a string

int date; //Integer to store the date

char *activity; //Pointer to store the activity of the day as a string

};

// Function to initialize the details of the day

void create(struct Day *day)

// Allocate memory for the dayName and activity strings

day->dayName = (char *)malloc(sizeof(char) * 20);

day->activity = (char *)malloc(sizeof(char) * 100);

//Prompt user to enter details

printf("Enter the day name: ");

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

printf("Enter the date: ");

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

printf("Enter the activity for the day: ");

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

//Function to read details for multiple days


// takes an array of Day structures ‘calendar’ and its size as input.

void read(struct Day *calendar, int size)

for (int i = 0; i < size; i++)

//Prompt user for details for each day

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

create(&calendar[i]);

//Function to display the details of all days of the week.

void display(struct Day *calendar, int size) {

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

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

//Display details for each day

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

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

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

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

printf("\n");

}
//Function to free the dynamically allocated memory for dayName and activity.

void freeMemory(struct Day *calendar, int size) {

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

free(calendar[i].dayName);

free(calendar[i].activity);

free(calendar); //free the memory for the array of Day structures.

int main() {

int size;

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

scanf("%d", &size);

//Allocate memory for the array of day structures

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

if (calendar == NULL) {

printf("Memory allocation failed. Exiting program.\n");

return 1;

read(calendar, size);

display(calendar, size);

freeMemory(calendar, size); // Free dynamically allocated memory

return 0;

You might also like