ADVANCED CODING ASSIGNMENT -6
K CHANDRASEKHAR
VU21CSEN0100404
6. Write a program to implement routine
management system using C.
#CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TASKS 100
#define MAX_LENGTH 100
struct Task {
int id;
char description[MAX_LENGTH];
};
void addTask(struct Task tasks[], int *taskCount);
void viewTasks(struct Task tasks[], int taskCount);
void deleteTask(struct Task tasks[], int *taskCount);
int main() {
struct Task tasks[MAX_TASKS];
int taskCount = 0;
int choice;
while (1) {
// Display menu
printf("\n=== Routine Management System ===\n");
printf("1. Add Task\n");
printf("2. View All Tasks\n");
printf("3. Delete Task\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
// Handle user choice
switch (choice) {
case 1:
addTask(tasks, &taskCount);
break;
case 2:
viewTasks(tasks, taskCount);
break;
case 3:
deleteTask(tasks, &taskCount);
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
void addTask(struct Task tasks[], int *taskCount) {
if (*taskCount >= MAX_TASKS) {
printf("Error: Task list is full.\n");
return;
}
struct Task newTask;
[Link] = *taskCount + 1;
printf("Enter the description of the task: ");
fgets([Link], MAX_LENGTH, stdin);
[Link][strcspn([Link], "\n")] = 0;
tasks[*taskCount] = newTask;
(*taskCount)++;
printf("Task added successfully.\n");
}
void viewTasks(struct Task tasks[], int taskCount) {
if (taskCount == 0) {
printf("No tasks available.\n");
return;
}
printf("\n--- All Tasks ---\n");
for (int i = 0; i < taskCount; i++) {
printf("Task ID: %d\n", tasks[i].id);
printf("Description: %s\n\n", tasks[i].description);
}
}
void deleteTask(struct Task tasks[], int *taskCount) {
if (*taskCount == 0) {
printf("No tasks to delete.\n");
return;
}
int id;
printf("Enter the task ID to delete: ");
scanf("%d", &id);
getchar();
int found = 0;
for (int i = 0; i < *taskCount; i++) {
if (tasks[i].id == id) {
found = 1;
for (int j = i; j < *taskCount - 1; j++) {
tasks[j] = tasks[j + 1];
}
(*taskCount)--;
printf("Task deleted successfully.\n");
break;
}
}
if (!found) {
printf("Task ID %d not found.\n", id);
}
}
OUTPUT: