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

DS Lab 1A and 1B Program

The document describes an experiment to create a calendar using a dynamically allocated array of structures in C. It defines a struct containing name, date, and activity fields to represent each day. An array of this struct is initialized with sample data. Functions are then created to populate, read, and display the calendar data. The code provides examples of creating and displaying a calendar by getting input from the user or a file.

Uploaded by

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

DS Lab 1A and 1B Program

The document describes an experiment to create a calendar using a dynamically allocated array of structures in C. It defines a struct containing name, date, and activity fields to represent each day. An array of this struct is initialized with sample data. Functions are then created to populate, read, and display the calendar data. The code provides examples of creating and displaying a calendar by getting input from the user or a file.

Uploaded by

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

EXPERIMENT: 1

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

PROGRAM CODE:
#include <stdio.h>

#include <string.h>
// Define a structure to represent day
struct Day
{
char name[20];
int date;
char activity[100];
};
int main() {
// Declare an array of 7 elements to represent the calendar struct Day
calendar[7];
// Initialize the calendar with sample data
strcpy(calendar[0].name, "Monday");
calendar[0].date = 1;
strcpy(calendar[0].activity, "Work from 9 AM to 5 PM");

strcpy(calendar[1].name, "Tuesday");
calendar[1].date = 2;
strcpy(calendar[1].activity, "Meeting at 10 AM");

strcpy(calendar[2].name, "Wednesday");
calendar[2].date = 3;
strcpy(calendar[2].activity, "Gym at 6 PM");
strcpy(calendar[3].name, "Thursday");
calendar[3].date = 4;
strcpy(calendar[3].activity, "Dinner with friends at 7 PM");

strcpy(calendar[4].name, "Friday");
calendar[4].date = 5;
strcpy(calendar[4].activity, "Movie night at 8 PM");

strcpy(calendar[5].name, "Saturday");
calendar[5].date = 6;
strcpy(calendar[5].activity, "Weekend getaway");

strcpy(calendar[6].name, "Sunday");
calendar[6].date = 7;
strcpy(calendar[6].activity, "Relax and recharge");

// Print the calendar printf("Calendar for the


week:\n"); for (int i = 0; i < 7; i++) {
printf("%s (Date: %d): %s\n", calendar[i].name, calendar[i].date, calendar[i].activity);
}
return 0;
}

SAMPLE OUTPUT 1:
Calendar for the week:
Monday (Date: 1): Work from 9 AM to 5 PM Tuesday
(Date: 2): Meeting at 10 AM
Wednesday (Date: 3): Gym at 6 PM
Thursday (Date: 4): Dinner with friends at 7 PM Friday
(Date: 5): Movie night at 8 PM
Saturday (Date: 6): Weekend getaway Sunday
(Date: 7): Relax and recharge
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.

PROGRAM CODE
#include <stdio.h>

#include <string.h>
// Define a structure to represent a day
struct Day {
char name[20];
int date;
char activity[100];
};
// Function to create the calendar void
create(struct Day calendar[7])
{
for (int i = 0; i < 7; i++)
{
printf("Enter details for %s:\n", calendar[i].name);

printf("Date: ");
scanf("%d", &calendar[i].date);
printf("Activity: ");
scanf(" %[^\n]", calendar[i].activity);
}
}

// Function to read data from the keyboard


void read(struct Day calendar[7])
{
FILE *file = fopen("calendar.txt", "r");
if (file == NULL)
{
printf("Error opening the file.\n"); return;
}

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


{
fscanf(file, "%d", &calendar[i].date);
fscanf(file, " %s", calendar[i].activity);
}

fclose(file);
}
// Function to display the calendar void
display(struct Day calendar[7])

{
printf("Calendar for the week:\n");
for (int i = 0; i < 7; i++) {
printf("%s (Date: %d): %s\n", calendar[i].name, calendar[i].date, calendar[i].activity);
}
}
int main()
{
struct Day calendar[7];

// Initialize the names of the days


strcpy(calendar[0].name, "Monday");
strcpy(calendar[1].name, "Tuesday");
strcpy(calendar[2].name, "Wednesday");
strcpy(calendar[3].name, "Thursday");
strcpy(calendar[4].name, "Friday");
strcpy(calendar[5].name, "Saturday");
strcpy(calendar[6].name, "Sunday");
int choice;
printf("1. Create Calendar\n");
printf("2. Read Calendar from File\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
create(calendar); break;
case 2:
read(calendar); break;
default:
printf("Invalid choice.\n"); return 1;
}

display(calendar);

return 0;
}
SAMPLE OUTPUT 1:
1. Create Calendar
2. Read Calendar from File Enter your
choice: 1 Enter details for Monday:
Date: 07/01/2001
Activity: Enter details for Tuesday: Date: JAVA
Activity: Enter details for Wednesday: Date:
PYTHON
Activity: Enter details for Thursday: Date: C/C+
+
Activity: Enter details for Friday: Date:
GAMING
Activity: Enter details for Saturday: Date:
APTITUDE
Activity: Enter details for Sunday: Date:
GENERAL KNOWLEDGE
Activity: Calendar for the week: Monday (Date:
7): /01/2001 Tuesday (Date: 1417934205): JAVA
Wednesday (Date: 32543): PYTHON Thursday
(Date: 0): C/C++
Friday (Date: 0): GAMING

Saturday (Date: 832): APTITUDE


Sunday (Date: 0): GENERAL KNOWLEDGE

You might also like