0% found this document useful (0 votes)
41 views13 pages

Lab-1 PPT Dsa-Bcsl305

DSA Lab program-1 with description

Uploaded by

vandana u
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)
41 views13 pages

Lab-1 PPT Dsa-Bcsl305

DSA Lab program-1 with description

Uploaded by

vandana u
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/ 13

Data Structures Lab

BCSL305
VANDANA U
Assistant Professor
Dept. of AI-DS
SMVITM
Lab-1 : PROGRAM STATEMENT:
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.
About the experiment:
A dynamic array is an array whose size can be changed during runtime. Unlike static arrays, which have a fixed size that is
determined at compile time, dynamic arrays can be resized as needed.
Dynamic arrays are implemented using pointers and memory allocation functions. In C, the most commonly used memory
allocation functions are malloc(), calloc(), and realloc(). These functions allow for the allocation and deallocation of
memory during runtime, which is necessary for creating and manipulating dynamic arrays.
Advantages:
1.One of the main advantages is that they allow for better memory management. With static arrays, the size of the array
is fixed, which means that memory is allocated for the entire array at once. It can lead to wasted memory if the array is not
fully utilized.
2.With dynamic arrays, memory is only allocated as needed, which can lead to more efficient memory usage.
3.Dynamic arrays allow for the size of the array to be adjusted as needed, which can make programs more versatile and
adaptable.
Disadvantages:
1.One of the main disadvantages is that they can be more complex to implement than static arrays.
2.Dynamic arrays can also be slower than static arrays.
Creating Dynamic Arrays in C
The most commonly used memory allocation functions in C are malloc(), calloc(), and realloc(). Here is an example of how
to create a dynamic array using malloc():
1.int *arr;
2.int size = 10;
3.arr = (int*) malloc(size * sizeof(int));
Algorithm:
1. Initialization
▪ Define a structure day to hold:
- dayname: Name of the day (e.g., "Monday").
- d, m, y: Date (day, month, year).
- activitydescription: Description of the activity for that day.
2. Memory Allocation
• Allocate memory for an array of 7 day structures.
3. Create Days
• Define an array of day names: {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"}.
• For each day (0 to 6):
Allocate memory for dayname and copy the
corresponding day name into it.
Allocate memory for activitydescription (with a buffer size of 256).
ALGORITHM (CONTINUED…)
4. Read Input
• For each day (0 to 6).
• Prompt the user to enter the date in dd/mm/yy format and read it.
• Prompt the user to enter an activity description and read it using getline
(handling dynamic memory).
5. Display Information
• Print a table header: "Day", "Date", and "Activity".
• For each day (0 to 6):
Print the day name, date, and activity description.
6. Cleanup
• Free memory allocated for dayname and activitydescription for each day.
• Free the memory allocated for the array of day structures.
7.Exit Program
• Return 0 to indicate successful completion.
This line includes the standard library header file. It
#include <stdio.h> provides functions for memory allocation (malloc, free),
process control, and other utilities.
#include <stdlib.h>
This line includes the string handling library header file. It
#include <string.h> provides functions for manipulating strings, such as strdup,
int i; strlen, and strcmp.

global variable which is accessible throughout the file.

struct day This line declares a pointer to a character (char *). This
pointer will be used to dynamically allocate memory
{ for storing the name of the day (e.g., "Monday").

char *dayname; They represent the day, month, and year, respectively,
in a date. They will store the date information for a
int d, m, y; particular day.
char *activitydescription; This line declares a pointer to a character (char *). This
}; pointer will be used to dynamically allocate memory for
storing a description of the activity planned for the day. It
points to a string that describes the activity.
This is a parameter of the function. It is a pointer to
an array of struct day instances. This means
void create(struct day *calendar) calendar is expected to point to a block of memory
{ that holds multiple struct day elements.
char *dayname[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"}; This line defines an array of string literals where each
for (i = 0; i < 7; i++) string literal represents the name of a day of the week.
{ Accesses the dayname field of the i-th element
in the calendar array. Strdup is a function that
calendar[i].dayname = strdup(dayname[i]); allocates memory and copies the string
size_t bufferSize = 256; dayname[i] into that newly allocated space.

calendar[i].activitydescription = (char *)malloc(bufferSize * sizeof(char));


}
} This declares a variable bufferSize of type size_t and
Accesses the activity description field of initializes it to 256. This represents the size of the
the i-th element in the calendar array buffer allocated for activitydescription.
using malloc().
A pointer to an array of struct day elements. This function will use
void read(struct day *calendar) this pointer to access and modify the day structures in the array.

{ This loop is used to clear the


input buffer of any leftover
for (i = 0; i < 7; i++) newline characters (\n) that
might remain from the
{
previous scanf call. This
printf("Enter date for %s in dd/mm/yy: ", calendar[i].dayname); ensures that the subsequent
getline call starts with a clean
scanf("%d%d%d", &calendar[i].d, &calendar[i].m, &calendar[i].y); input buffer.
printf("Enter activity for %s: ", calendar[i].dayname);
while (getchar() != '\n') ;
Declares a variable bufferSize of type size_t and initializes it to 256.
size_t bufferSize = 256; This specifies the initial size of the buffer that will be used by getline.
getline(&calendar[i].activitydescription, &bufferSize, stdin);
}} Reads an entire line of input from the standard input (stdin) into
calendar[i].activitydescription. It dynamically allocates (or resizes) memory as needed
to hold the input line, adjusting the buffer size to accommodate the entire input.
void display(struct day *calendar)
{
printf("%-10s %-10s %-10s\n", "Day", "Date", "Activity");
for (i = 0; i < 7; ++i)
{
printf("%-10s %d/%d/%d\t%-10s\n", calendar[i].dayname, calendar[i].d, calendar[i].m,
calendar[i].y, calendar[i].activitydescription);
}
} "%-10s %d/%d/%d\T %-10s\n": The format string specifies how each day's
information should be printed:
%-10s: Prints the dayname of the i-th day, left-justified in a field of 10
characters.
int main() Declares a pointer calendar that will
{ point to an array of struct day elements.
struct day *calendar = (struct day *)malloc(7 * sizeof(struct day));
if (calendar == NULL)
{
fprintf(stderr, "Memory allocation failed\n"); Prints an error message to the standard error
return 1; stream (stderr) if memory allocation fails.
} - Calls the create function, passing the calendar pointer to initialize the
create(calendar); dayname and activitydescription fields for each day in the calendar.
read(calendar); - Calls the read function to read user input for the date and activity description
for each day in the calendar.
display(calendar); - Calls the display function to print the information of each day in the
for (int i = 0; i < 7; ++i) calendar.
{ - Frees the memory allocated for the dayname
free(calendar[i].dayname); field of the i-th element of the calendar array.
free(calendar[i].activitydescription); - Frees the memory allocated for the
} activitydescription field of the i-th element of
free(calendar); the calendar array.
return 0; } Frees the memory allocated for the calendar array itself.
Enter date for Monday in dd/mm/yy: 20 08 2024
Enter activity for Monday: Go out with friends
Enter date for Tuesday in dd/mm/yy: 21 08 2024
Enter activity for Tuesday: Go for shopping
Enter date for Wednesday in dd/mm/yy: 22 08 2024
Enter activity for Wednesday: Learn for the test
Enter date for Thursday in dd/mm/yy: 23 08 2024
Expected
Enter activity for Thursday: Eat outside
Enter date for Friday in dd/mm/yy: 24 08 2024
Output:

Enter activity for Friday: Go for a Run


Enter date for Saturday in dd/mm/yy: 25 08 2024
Enter activity for Saturday: Study Data Structures
Enter date for Sunday in dd/mm/yy: 26 08 2024
Enter activity for Sunday: Watch Documentary
Day Date Activity
Monday 20/8/2024 Go out with friends

Tuesday 21/8/2024 Go for shopping

Wednesday 22/8/2024 Learn for the test Expected


Thursday 23/8/2024 Eat outside Output:

Friday 24/8/2024 Go for a Run

Saturday 25/8/2024 Study Data Structures

Sunday 26/8/2024 Watch Documentary


THANK YOU

You might also like