CREATE() READ() DISPLAY()

  A dynamically Created array / STRING

create(), read() and display(); 

QUESTION:

 

Develop a Program in C for the following:

  1. 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).

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

  PROGRAM:

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

#define MAX_INPUT 100

// Structure to hold one day's details
struct Day {
    char* day_name;
    int date;
    char* activity;
};

// Function to create a calendar (array of 7 Day structures)
struct Day* create() {
    struct Day* calendar = (struct Day*)malloc(7 * sizeof(struct Day));
    if (calendar == NULL) {
        perror("Memory allocation failed for calendar");
        exit(1);
    }
    return calendar;
}

// Function to read data from user and populate calendar
void read(struct Day* calendar) {
    char temp[MAX_INPUT];
    for (int i = 0; i < 7; i++) {
        printf("Enter details for Day %d:\n", i + 1);

        printf("Enter day name: ");
        fgets(temp, MAX_INPUT, stdin);
        temp[strcspn(temp, "\n")] = 0; // Remove newline
        calendar[i].day_name = strdup(temp);

        printf("Enter date (as number): ");
        scanf("%d", &calendar[i].date);
        getchar(); // consume newline after number

        printf("Enter activity: ");
        fgets(temp, MAX_INPUT, stdin);
        temp[strcspn(temp, "\n")] = 0; // Remove newline
        calendar[i].activity = strdup(temp);

        printf("\n");
    }
}

// Function to display calendar data
void display(struct Day* calendar) {
    printf("---- Weekly Activity Report ----\n\n");
    for (int i = 0; i < 7; i++) {
        printf("Day: %s (Date: %d)\n", calendar[i].day_name, calendar[i].date);
        printf("Activity: %s\n\n", calendar[i].activity);
    }
}

// Function to free dynamically allocated memory
void freeCalendar(struct Day* calendar) {
    for (int i = 0; i < 7; i++) {
        free(calendar[i].day_name);
        free(calendar[i].activity);
    }
    free(calendar);
}

// Main function
int main() {
    struct Day* weekCalendar = create();
    read(weekCalendar);
    display(weekCalendar);
    freeCalendar(weekCalendar);
    return 0;
}

 

Output


ceciot@cec-IOT:~/Desktop$ gcc calander.c -o abcd

ceciot@cec-IOT:~/Desktop$ ./abcd

Enter details for Day 1:

Enter day name: mon

Enter date (as number): 1

Enter activity: study

 

Enter details for Day 2:

Enter day name: tue

Enter date (as number): 2

Enter activity: games


Enter details for Day 3:

Enter day name: wed

Enter date (as number): 3

Enter activity: social work


Enter details for Day 4:

Enter day name: thu

Enter date (as number): 4

Enter activity: helping mom


Enter details for Day 5:

Enter day name: fri

Enter date (as number): 5

Enter activity: sleeping


Enter details for Day 6:

Enter day name: sat

Enter date (as number): 6

Enter activity: reading books


Enter details for Day 7:

Enter day name: sunday

Enter date (as number): halt

Enter activity: 

--------------------------------------------------------------------------------------- 

---- Weekly Activity Report ----

Day: mon (Date: 1)

Activity: study

Day: tue (Date: 2)

Activity: games

Day: wed (Date: 3)

Activity: social work

Day: thu (Date: 4)

Activity: helping mom

Day: fri (Date: 5)

Activity: sleeping

Day: sat (Date: 6)

Activity: reading books

Day: sunday (Date: 0)

Activity: alt

ceciot@cec-IOT:~/Desktop$


No comments:

Post a Comment