Data Structure lab

Program 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>

#define DAYS 7

// Structure definition
typedef struct {
    char *dayName;
    int date;
    char *activity;
} Day;

// Function declarations
Day* create();
void read(Day *calendar);
void display(Day *calendar);

int main() {
    Day *calendar = create();
    read(calendar);
    display(calendar);

    // Free allocated memory
    for (int i = 0; i < DAYS; i++) {
        free(calendar[i].dayName);
        free(calendar[i].activity);
    }
    free(calendar);

    return 0;
}

// Create dynamically allocated array
Day* create() {
    return (Day *)malloc(DAYS * sizeof(Day));
}

// Read input from user
void read(Day *calendar) {
    char buffer[100];
    for (int i = 0; i < DAYS; i++) {
        printf("Enter name of Day %d: ", i + 1);
        scanf("%s", buffer);
        calendar[i].dayName = strdup(buffer);

        printf("Enter date for %s: ", calendar[i].dayName);
        scanf("%d", &calendar[i].date);

        printf("Enter activity for %s: ", calendar[i].dayName);
        getchar(); // Consume newline
        fgets(buffer, sizeof(buffer), stdin);
        buffer[strcspn(buffer, "\n")] = 0; // Remove newline
        calendar[i].activity = strdup(buffer);
    }
}

// Display the calendar
void display(Day *calendar) {
    printf("\n--- Weekly Activity Report ---\n");
    for (int i = 0; i < DAYS; i++) {
        printf("Day %d: %s, Date: %d, Activity: %s\n",
               i + 1, calendar[i].dayName, calendar[i].date, calendar[i].activity);
    }
}


               


  

Output :-

Enter name of Day 1: Sunday
Enter date for Sunday: 1
Enter activity for Sunday: Cooking
Enter name of Day 2: Monday
Enter date for Monday: 2
Enter activity for Monday: Go to College
Enter name of Day 3: Tuesday
Enter date for Tuesday: 3
Enter activity for Tuesday: Attending  Classess
Enter name of Day 4: Wednesday
Enter date for Wednesday: 4
Enter activity for Wednesday: Attending Labs
Enter name of Day 5: Thursday
Enter date for Thursday: 5
Enter activity for Thursday: Reading        
Enter name of Day 6: Friday
Enter date for Friday: 6
Enter activity for Friday: Gaming      
Enter name of Day 7: Saturday
Enter date for Saturday: 7
Enter activity for Saturday: NSS Activity

--- Weekly Activity Report ---
Day 1: Sunday, Date: 1, Activity: Cooking
Day 2: Monday, Date: 2, Activity: Go to College
Day 3: Tuesday, Date: 3, Activity: Attending  Classess
Day 4: Wednesday, Date: 4, Activity: Attending Labs
Day 5: Thursday, Date: 5, Activity: Reading
Day 6: Friday, Date: 6, Activity: Gaming
Day 7: Saturday, Date: 7, Activity: NSS Activity




  
Comments

Replay !

0 Comments

Share Your Thoughts

Please enter your name
Please enter a valid email
Password must be at least 6 characters
Please enter your comment
Email Verification Required
We've sent a 6-digit verification code to . Please enter the code below to verify your email address.
Email Verified Successfully!
Your email has been verified. Would you like to proceed with posting your comment?

Type "YES" to confirm and post your comment, or click Cancel to skip posting.