1.
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.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NUM_DAYS_IN_WEEK 7
// 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;
void fnFreeCal(DAYTYPE *);
void fnDispCal(DAYTYPE *);
void fnReadCal(DAYTYPE *);
DAYTYPE *fnCreateCal();
int main()
{
// Create the calendar
DAYTYPE *weeklyCalendar = fnCreateCal();
// Read data from the keyboard
fnReadCal(weeklyCalendar);
// Display the week's activity details
fnDispCal(weeklyCalendar);
// Free allocated memory
fnFreeCal(weeklyCalendar);
return 0;
}
DAYTYPE *fnCreateCal()
{
DAYTYPE *calendar = (DAYTYPE *)malloc(NUM_DAYS_IN_WEEK * sizeof(DAYTYPE));
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
calendar[i].acDayName = NULL;
calendar[i].iDate = 0;
calendar[i].acActivity = NULL;
}
return calendar;
}
void fnReadCal(DAYTYPE *calendar)
{
char cChoice;
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
printf("Do you want to enter details for day %d [Y/N]: ", i + 1);
scanf("%c", &cChoice); getchar();
if(tolower(cChoice) == '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
}
}
void fnDispCal(DAYTYPE *calendar)
{
printf("nWeek's Activity Details:n");
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
printf("Day %d:n", i + 1);
if(calendar[i].iDate == 0)
{
printf("No Activitynn");
continue;
}
printf(" Day Name: %sn", calendar[i].acDayName);
printf(" Date: %dn", calendar[i].iDate);
printf(" Activity: %snn", calendar[i].acActivity);
}
}
void fnFreeCal(DAYTYPE *calendar)
{
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
free(calendar[i].acDayName);
free(calendar[i].acActivity);
}
free(calendar);
}
Output:
Do you want to enter details for day 1 [Y/N]: N
Do you want to enter details for day 2 [Y/N]: Y
Day Name: Monday
Date: 10
Activity: Meeting with Chairman.
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]: Y
Day Name: Thursday
Date: 13
Activity: Product Survey
Do you want to enter details for day 6 [Y/N]: Y
Day Name: Friday
Date: 14
Activity: Budget Breakdown and Planning
Do you want to enter details for day 7 [Y/N]: Y
Day Name: Saturday
Date: 15
Activity: Outing with family
Week's Activity Details:
Day 1:
No Activity
Day 2:
Day Name: Monday
Date: 10
Activity: Meeting with Chairman.
Day 3:
No Activity
Day 4:
No Activity
Day 5:
Day Name: Thursday
Date: 13
Activity: Product Survey
Day 6:
Day Name: Friday
Date: 14
Activity: Budget Breakdown and Planning
Day 7:
Day Name: Saturday
Date: 15
Activity: Outing with family
9. Develop a Program in C for the following operationson Singly Circular Linked List
(SCLL)
with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result
in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
// Node structure for a term in the polynomial
struct PolyTerm{
int coefficient;
int pow_x;
int pow_y;
int pow_z;
struct PolyTerm* next;
};
typedef struct PolyTerm* POLYPTR;
POLYPTR fnInsertTerm(POLYPTR poly, int coef, int pow_x, int pow_y, int pow_z)
{
POLYPTR cur;
POLYPTR newNode = (POLYPTR)malloc(sizeof(struct PolyTerm));
newNode->coefficient = coef;
newNode->pow_x = pow_x;
newNode->pow_y = pow_y;
newNode->pow_z = pow_z;
newNode->next = NULL;
cur = poly;
while(cur->next != poly)
{
cur = cur->next;
}
cur->next = newNode;
newNode->next = poly;
return poly;
}
void fnDispPolynomial(POLYPTR poly)
{
if (poly->next == poly)
{
printf("Polynomial is empty.n");
return;
}
POLYPTR cur = poly->next;
do
{
printf("%dx^%dy^%dz^%d ", cur->coefficient, cur->pow_x, cur->pow_y,
cur->pow_z);
cur = cur->next;
if (cur != poly)
{
printf("+ ");
}
} while (cur != poly);
printf("n");
}
int fnEvaluatePolynomial(POLYPTR poly, int x, int y, int z)
{
int result = 0;
if (poly->next == poly)
{
return result;
}
POLYPTR cur = poly->next;
do
{
int termValue = cur->coefficient;
termValue *= pow(x, cur->pow_x);
termValue *= pow(y, cur->pow_y);
termValue *= pow(z, cur->pow_z);
result += termValue;
cur = cur->next;
} while (cur != poly);
return result;
}
bool fnMatchTerm(POLYPTR p1, POLYPTR p2)
{
bool bMatches = true;
if(p1->pow_x != p2->pow_x)
bMatches = false;
if(p1->pow_y != p2->pow_y)
bMatches = false;
if(p1->pow_z != p2->pow_z)
bMatches = false;
return bMatches;
}
POLYPTR fnAddPolynomials(POLYPTR poly1, POLYPTR poly2, POLYPTR polySum)
{
POLYPTR cur1 = poly1->next;
POLYPTR cur2 = poly2->next;
do
{
polySum = fnInsertTerm(polySum, cur1->coefficient, cur1->pow_x, cur1-
>pow_y, cur1->pow_z);
cur1 = cur1->next;
}while(cur1 != poly1);
do
{
cur1 = polySum->next;
bool bMatchFound = false;
do
{
if(fnMatchTerm(cur1, cur2))
{
cur1->coefficient += cur2->coefficient;
bMatchFound = true;
break;
}
cur1 = cur1->next;
}while(cur1 != polySum);
if(!bMatchFound)
{
polySum = fnInsertTerm(polySum, cur2->coefficient, cur2->pow_x,
cur2->pow_y, cur2->pow_z);
}
cur2 = cur2->next;
}while(cur2 != poly2);
return polySum;
int main()
{
POLYPTR poly1 = (POLYPTR)malloc(sizeof(struct PolyTerm));
poly1->next = poly1;
POLYPTR poly2 = (POLYPTR)malloc(sizeof(struct PolyTerm));
poly2->next = poly2;
POLYPTR polySum = (POLYPTR)malloc(sizeof(struct PolyTerm));
polySum->next = polySum;
// Represent and evaluate the polynomial P(x, y, z) = 6x^2y^2z - 4yz^5 +
3x^3yz + 2xy^5z - 2xyz^3
poly1 = fnInsertTerm(poly1, 6, 2, 2, 1);
poly1 = fnInsertTerm(poly1, 4, 0, 1, 5);
poly1 = fnInsertTerm(poly1, 3, 3, 1, 1);
poly1 = fnInsertTerm(poly1, 2, 1, 5, 1);
poly1 = fnInsertTerm(poly1, 2, 1, 1, 3);
// Display the polynomial P(x, y, z)
printf("POLY1(x, y, z) = ");
fnDispPolynomial(poly1);
// Read and evaluate the second polynomial POLY2(x, y, z)
// Represent the polynomial P(x, y, z) = xyz + 4x^3yz
poly2 = fnInsertTerm(poly2, 1, 1, 1, 1); // Example term
poly2 = fnInsertTerm(poly2, 4, 3, 1, 1);
// Display the second polynomial POLY2(x, y, z)
printf("POLY2(x, y, z) = ");
fnDispPolynomial(poly2);
// Add POLY1(x, y, z) and POLY2(x, y, z) and store the result in
POLYSUM(x, y, z)
polySum = fnAddPolynomials(poly1, poly2, polySum);
// Display the sum POLYSUM(x, y, z)
printf("nPOLYSUM(x, y, z) = ");
fnDispPolynomial(polySum);
// Evaluate POLYSUM(x, y, z) for specific values
int x = 1, y = 2, z = 3;
int iRes = fnEvaluatePolynomial(polySum, x, y, z);
printf("nResult of POLYSUM(%d, %d, %d): %dn", x, y, z, iRes);
return 0;
}
Output:
POLY1(x, y, z) = 6x^2y^2z^1 + 4x^0y^1z^5 + 3x^3y^1z^1 + 2x^1y^5z^1 +
2x^1y^1z^3
POLY2(x, y, z) = 1x^1y^1z^1 + 4x^3y^1z^1
POLYSUM(x, y, z) = 6x^2y^2z^1 + 4x^0y^1z^5 + 7x^3y^1z^1 + 2x^1y^5z^1 +
2x^1y^1z^3 + 1x^1y^1z^1
Result of POLYSUM(1, 2, 3): 2364