Lab-1 PPT Dsa-Bcsl305
Lab-1 PPT Dsa-Bcsl305
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.
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.