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

pgm1 Array

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)
3 views

pgm1 Array

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

DATA STRUCTURES LABORATORY (BCSL305)

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>
#include <ctype.h>

#define NUM_DAYS_IN_WEEK 7
// Structure to represent a day
typedef struct
{
char *acDayName;
int iDate;
char *acActivity;
}DAYTYPE;

void FreeCal(DAYTYPE *);


void DispCal(DAYTYPE *);
void ReadCal(DAYTYPE *);
DAYTYPE *CreateCal();

void main()
{
DAYTYPE *weeklyCalendar = CreateCal();
ReadCal(weeklyCalendar);
DispCal(weeklyCalendar);
FreeCal(weeklyCalendar);
}

DAYTYPE *CreateCal()
{
DAYTYPE *calendar = (DAYTYPE *)malloc(NUM_DAYS_IN_WEEK *
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
calendar[i].acDayName = NULL;
calendar[i].iDate = 0;
calendar[i].acActivity = NULL;
}
return calendar;
}

Suresh Patel Page 1


DATA STRUCTURES LABORATORY (BCSL305)

void ReadCal(DAYTYPE *calendar)


{
char cChoice;
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
printf("\nDo you want to enter details for day %d [Y/N]: ", i + 1);
scanf("%c", &cChoice); getchar();
if(tolower(cChoice) == 'n')
continue;
printf("Day Name: ");
char nameBuffer[50];
scanf("%s", nameBuffer);
calendar[i].acDayName = strdup(nameBuffer);
printf("Date: ");
scanf("%d", &calendar[i].iDate);
printf("Activity: ");
char activityBuffer[100];
scanf(" %[^\n]", activityBuffer);
calendar[i].acActivity = strdup(activityBuffer);
printf("n");
getchar();
}
}

void DispCal(DAYTYPE *calendar)


{
printf("\nWeek's Activity Details:\n");
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
printf("Day %d:\n", i + 1);
if(calendar[i].iDate == 0)
{
printf("\nNo Activity \n");
continue;
}
printf(" Day Name: %s \n", calendar[i].acDayName);
printf(" Date: %d \n", calendar[i].iDate);
printf(" Activity: %s \n", calendar[i].acActivity);
}
}

void FreeCal(DAYTYPE *calendar)


{
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
free(calendar[i].acDayName);
free(calendar[i].acActivity);
}
free(calendar);
}
Suresh Patel Page 2
DATA STRUCTURES LABORATORY (BCSL305)

Run-1:

Do you want to enter details for day 1 [Y/N]: n


Do you want to enter details for day 2 [Y/N]: n

Week's Activity Details:


Day 1:

No Activity
Day 2:

No Activity

Run-2:

Do you want to enter details for day 1 [Y/N]: Y


Day Name: Monday
Date: 15022024
Activity: DS project

Do you want to enter details for day 2 [Y/N]: y


Day Name: Tuesday
Date: 16022024
Activity: C++ project

Week's Activity Details:


Day 1:
Day Name: Monday
Date: 15022024
Activity: DS project
Day 2:
Day Name: Tuesday
Date: 16022024
Activity: C++ project

Suresh Patel Page 3

You might also like