0% found this document useful (0 votes)
5 views8 pages

Weekly Activity Calendar Program

Uploaded by

kesara bs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Weekly Activity Calendar Program

Uploaded by

kesara bs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define NUM_DAYS_IN_WEEK 7
int i;

// Structure to represent a day


typedef struct
{
char *acDayName; // Dynamically allocated string for the day name
int iDate; // Date of the day
char *acActivity; // Dynamically allocated string for the activity description
}DAYTYPE;

//Prototypes
void FreeCal(DAYTYPE *);
void DispCal(DAYTYPE *);
void ReadCal(DAYTYPE *);
DAYTYPE *CreateCal();

//Main function
int main()
{
// Create the calendar
DAYTYPE *weeklyCalendar = CreateCal();

// Read data from the keyboard


ReadCal(weeklyCalendar);

// Display the week's activity details


DispCal(weeklyCalendar);
// Free allocated memory
FreeCal(weeklyCalendar);

return 0;
}

// Createcalender function definition


DAYTYPE *CreateCal()
{
DAYTYPE *calendar = (DAYTYPE *)malloc(NUM_DAYS_IN_WEEK *
sizeof(DAYTYPE));

for( i = 0; i < NUM_DAYS_IN_WEEK; i++)


{
calendar[i].acDayName = NULL;
calendar[i].iDate = 0;
calendar[i].acActivity = NULL;
}

return calendar;
}
// Read Calender function definition
void ReadCal(DAYTYPE *calendar)
{
char Choice;
for( i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
printf("Do you want to enter details for day %d [Y/N]: ", i + 1);
scanf("%c", &Choice);
getchar();

if(tolower(Choice) == 'n')
continue;
printf("Day Name: ");
char nameBuffer[50];
scanf("%s", nameBuffer);
calendar[i].acDayName = strdup(nameBuffer); // Dynamically allocate and copy
the string

printf("Date: ");
scanf("%d", &calendar[i].iDate);

printf("Activity: ");
char activityBuffer[100];
scanf(" %[^\n]", activityBuffer); // Read the entire line, including spaces
calendar[i].acActivity = strdup(activityBuffer);

printf("\n");
getchar(); //remove trailing enter character in input buffer
}
}

// DisplayCalender function definition


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

// FreeCalender function definition


void FreeCal(DAYTYPE *calendar)
{
for( i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
free(calendar[i].acDayName);
free(calendar[i].acActivity);
}
free(calendar);
}

/* Output

[root@localhost 2022batchDSLab]# cc 1_Calender.c


[root@localhost 2022batchDSLab]# ./[Link]
Do you want to enter details for day 1 [Y/N]: y
Day Name: Monday
Date: 27102023
Activity: CSI work

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


Day Name: Tuesday
Date: 28102023
Activity: FDP conduction

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


Day Name: Wednesday
Date: 29102023
Activity: Proposal Writeup

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


Day Name: Thursday
Date: 30102023
Activity: Data Analysis

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


Day Name: Friday
Date: 31102023
Activity: Article Review

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


Day Name: Saturday
Date: 01112023
Activity: Week Off Enjoy

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


Day Name: Sunday
Date: 02112023
Activity: Enjoy

Week's Activity Details:


Day 1:
Day Name: Monday
Date: 27102023
Activity: CSI work

Day 2:
Day Name: Tuesday
Date: 28102023
Activity: FDP conduction
Day 3:
Day Name: Wednesday
Date: 29102023
Activity: Proposal Writeup

Day 4:
Day Name: Thursday
Date: 30102023
Activity: Data Analysis

Day 5:
Day Name: Friday
Date: 31102023
Activity: Article Review

Day 6:
Day Name: Saturday
Date: 1112023
Activity: Week Off Enjoy

Day 7:
Day Name: Sunday
Date: 2112023
Activity: Enjoy

[root@localhost 2022batchDSLab]# ./[Link]


Do you want to enter details for day 1 [Y/N]: y
Day Name: Wednesday
Date: 01112023
Activity: Article Review

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


Do you want to enter details for day 3 [Y/N]: n
Do you want to enter details for day 4 [Y/N]: n
Do you want to enter details for day 5 [Y/N]: n
Do you want to enter details for day 6 [Y/N]: n
Do you want to enter details for day 7 [Y/N]: y
Day Name: sunday
Date: 07112023
Activity: Enjoy

Week's Activity Details:


Day 1:
Day Name: Wednesday
Date: 1112023
Activity: Article Review

Day 2:
No Activity

Day 3:
No Activity

Day 4:
No Activity

Day 5:
No Activity

Day 6:
No Activity

Day 7:
Day Name: sunday
Date: 7112023
Activity: Enjoy

Common questions

Powered by AI

Structs in this program offer an organized way to group different data types together to represent a day in the calendar. Each DAYTYPE struct holds related data points such as day name, date, and activity as a single logical unit. This encapsulation simplifies handling collections of days within the weekly calendar and supports dynamic memory allocation by working as a container for diverse data types .

If 'FreeCal' is not implemented or called, it would lead to memory leaks as the dynamically allocated memory for day names and activities would not be deallocated after the calendar's use. This non-freed memory could accumulate with prolonged use or repeated program execution, exhaust system memory resources, and potentially degrade system performance over time .

To handle invalid date inputs robustly in the ReadCal function, input validation can be added by checking if the input is within acceptable bounds (e.g., the expected range of a date format). An additional 'while' loop could be implemented to prompt re-entry of the date until a valid input is received. Alternatively, 'scanf' can be enhanced with condition checks that account for formatting errors .

The program is structured to simulate a weekly calendar using a custom data type called DAYTYPE. This structure contains dynamically allocated strings for day names and activity descriptions, and an integer for the date. The program follows a typical lifecycle of dynamic memory allocation, user input processing, data display, and memory deallocation. It uses functions like CreateCal for allocation, ReadCal for collecting user input, DispCal to display the details, and FreeCal to free allocated memory .

Removing the trailing newline character after 'scanf' is necessary because 'scanf' doesn't consume the newline character left in the input buffer when reading input. This leftover character could interfere with subsequent inputs, especially when reading strings, potentially causing unwanted results or errors in reading user input .

To improve user interaction and usability, the program could incorporate clearer prompts, supportive error messages, and a more intuitive command interface such as confirming entries or showing an overview of inputs before submission. Handling irregular user inputs more gracefully and offering corrections can also be enhanced. Additionally, enhancing the loop construct to immediately account for buffer streaming would reduce input lag or confusion .

The 'continue' statement in 'ReadCal' function is used to skip the current iteration of the loop if the user opts not to enter details for a specific day (chooses 'n'). This prevents any further processing for that day's data in the current loop iteration, thereby effectively skipping over days where no activity information is provided by the user .

The program manages dynamic strings using the 'strdup' function, which allocates sufficient memory to store the strings of day names and activities. This ensures that each string is stored independently even if the source buffer is later overwritten or freed. Furthermore, these dynamically allocated strings are freed using 'free' in the FreeCal function to prevent memory leaks .

Memory management is crucial in this program as it dynamically allocates and deallocates memory for the calendar entries. malloc() is used to allocate memory for an array of structures, while strdup() is used to allocate memory for strings representing day names and activities. This is important to avoid memory leaks, which is why FreeCal is implemented to free up memory used by these strings and the DAYTYPE array itself .

The program uses 'scanf' for input, which implicitly includes potential buffer overflow risks if not carefully managed. Specifically, it reads day names and activities into local buffers without size restrictions. Although a proper space pattern ' %[^ ]' is used for activities to capture spaces, mitigating overflow risks should include limiting input length. The program is susceptible to buffer overflows and unauthorized memory access especially for acDayName and acActivity fields .

You might also like