READ() WRITE() DISPLAY()
EX NO 1 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.
Key Components:
struct Activity
: Holds adate
(as string) and anactivity
description.Functions:
create()
andread()
– Both take input from the user for each day's date and activity. (They are effectively the same in this code.)display()
– Shows the stored calendar entries.
main()
:Asks the user how many days to store.
Prompts for input method (create or read).
Displays all entered activities.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure to represent a day's activity
struct Activity {
char date[11]; // Date in the format "YYYY-MM-DD"
char activity[100]; // Activity description
};
// Function to create a calendar
void create(struct Activity calendar[], int size) {
for (int i = 0; i < size; i++) {
printf("Enter activity details for Day %d (YYYY-MM-DD): ", i + 1);
scanf("%s", calendar[i].date);
printf("Enter activity details for Day %d: ", i + 1);
getchar(); // Consume newline left by previous scanf
fgets(calendar[i].activity, sizeof(calendar[i].activity), stdin);
}
}
// Function to read data from the keyboard
void read(struct Activity calendar[], int size) {
for (int i = 0; i < size; i++) {
printf("Enter activity details for Day %d (YYYY-MM-DD): ", i + 1);
scanf("%s", calendar[i].date);
printf("Enter activity details for Day %d: ", i + 1);
getchar(); // Consume newline left by previous scanf
fgets(calendar[i].activity, sizeof(calendar[i].activity), stdin);
}
}
// Function to display a weekly activity details report
void display(struct Activity calendar[], int size) {
printf("Weekly Activity Details Report:\n");
for (int i = 0; i < size; i++) {
printf("Day %d: Date: %s\n", i + 1, calendar[i].date);
printf("Activity: %s", calendar[i].activity);
}
}
int main() {
int size;
printf("Enter the number of days in the calendar: ");
scanf("%d", &size);
struct Activity calendar[size];
// Create or read data based on your choice
int choice;
printf("Choose an option:\n");
printf("1. Create Calendar\n");
printf("2. Read Calendar\n");
scanf("%d", &choice);
if (choice == 1) {
create(calendar, size);
} else if (choice == 2) {
read(calendar, size);
} else {
printf("Invalid choice\n");
return 1;
}
// Display the weekly activity details report
display(calendar, size);
return 0;
}
OUTPUT:
No comments:
Post a Comment