Singly Circular Linked List (SCLL)
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
NOTE:
The error you're seeing:
undefined reference to `pow'
is a linker error, not a compile-time error. It means the compiler found the declaration of pow()
(from math.h
), but the linker couldn't find its definition because you didn't link the math library during compilation.
Fix
When compiling a C program that uses math functions like pow()
, you must link with the math library by adding -lm
at the end of the gcc
command:
gcc scll.c -o scll -lm
The
-lm
tells the linker to link against the math library (libm.so
on Linux systems).
Example
gcc scll.c -o scll -lm
./scll
Summary
-
#include <math.h>
includes the function declarations. -
-lm
links the actual math library. -
Without
-lm
, math functions likepow()
,sqrt()
, etc., will cause undefined reference linker errors.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define COMPARE(x, y) ((x == y) ? 0 : ((x > y) ? 1 : -1))
struct node {
int coef;
int xexp, yexp, zexp;
struct node *link;
};
typedef struct node *NODE;
NODE getnode() {
NODE x = (NODE) malloc(sizeof(struct node));
if (x == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return x;
}
NODE attach(int coef, int xexp, int yexp, int zexp, NODE head) {
if (coef == 0) return head; // Skip 0 coefficient terms
NODE temp = getnode();
temp->coef = coef;
temp->xexp = xexp;
temp->yexp = yexp;
temp->zexp = zexp;
NODE cur = head;
while (cur->link != head)
cur = cur->link;
cur->link = temp;
temp->link = head;
return head;
}
NODE read_poly(NODE head) {
int n, coef, xexp, yexp, zexp;
printf("Enter the number of terms in the polynomial: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("\nTerm %d:\n", i);
printf("Coefficient: ");
scanf("%d", &coef);
printf("Powers of x, y, z: ");
scanf("%d %d %d", &xexp, &yexp, &zexp);
head = attach(coef, xexp, yexp, zexp, head);
}
return head;
}
void display(NODE head) {
NODE temp = head->link;
if (temp == head) {
printf("0\n");
return;
}
while (temp != head) {
printf("%+dx^%dy^%dz^%d ", temp->coef, temp->xexp, temp->yexp, temp->zexp);
temp = temp->link;
}
printf("\n");
}
int poly_evaluate(NODE head) {
int x, y, z;
double sum = 0;
printf("Enter the values of x, y, z: ");
scanf("%d %d %d", &x, &y, &z);
NODE temp = head->link;
while (temp != head) {
sum += temp->coef * pow(x, temp->xexp) * pow(y, temp->yexp) * pow(z, temp->zexp);
temp = temp->link;
}
return (int)sum;
}
int compare_terms(NODE a, NODE b) {
if (a->xexp != b->xexp)
return COMPARE(a->xexp, b->xexp);
if (a->yexp != b->yexp)
return COMPARE(a->yexp, b->yexp);
return COMPARE(a->zexp, b->zexp);
}
NODE poly_sum(NODE head1, NODE head2, NODE head3) {
NODE a = head1->link;
NODE b = head2->link;
while (a != head1 && b != head2) {
int cmp = compare_terms(a, b);
if (cmp == 0) {
int sum_coef = a->coef + b->coef;
head3 = attach(sum_coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
b = b->link;
} else if (cmp > 0) {
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
} else {
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
}
}
while (a != head1) {
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
}
while (b != head2) {
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
}
return head3;
}
int main() {
NODE head, head1, head2, head3;
int ch, res;
head = getnode(); head->link = head;
head1 = getnode(); head1->link = head1;
head2 = getnode(); head2->link = head2;
head3 = getnode(); head3->link = head3;
while (1) {
printf("\n===== Menu =====");
printf("\n1. Represent and Evaluate a Polynomial P(x,y,z)");
printf("\n2. Find the Sum of Two Polynomials POLY1(x,y,z) and POLY2(x,y,z)");
printf("\n3. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
head = getnode(); head->link = head; // Reset head
printf("\n--- Polynomial Evaluation ---\n");
head = read_poly(head);
printf("Polynomial: ");
display(head);
res = poly_evaluate(head);
printf("Evaluated result: %d\n", res);
break;
case 2:
head1 = getnode(); head1->link = head1;
head2 = getnode(); head2->link = head2;
head3 = getnode(); head3->link = head3;
printf("\n--- Enter POLY1 ---\n");
head1 = read_poly(head1);
printf("POLY1: ");
display(head1);
printf("\n--- Enter POLY2 ---\n");
head2 = read_poly(head2);
printf("POLY2: ");
display(head2);
head3 = poly_sum(head1, head2, head3);
printf("\nSum of POLY1 and POLY2: ");
display(head3);
break;
case 3:
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
OUTPUT:
ceciot@cec-IOT:~$ cd Desktop/
ceciot@cec-IOT:~/Desktop$ gcc scll.c -o scll -lm
ceciot@cec-IOT:~/Desktop$ ./scll
===== Menu =====
1. Represent and Evaluate a Polynomial P(x,y,z)
2. Find the Sum of Two Polynomials POLY1(x,y,z) and POLY2(x,y,z)
3. Exit
Enter your choice: 1
--- Polynomial Evaluation ---
Enter the number of terms in the polynomial: 3
Term 1:
Coefficient: 6
Powers of x, y, z: 2 2 1
Term 2:
Coefficient: -4
Powers of x, y, z: 0 1 5
Term 3:
Coefficient: 3
Powers of x, y, z: 1 1 1
Polynomial: +6x^2y^2z^1 -4x^0y^1z^5 +3x^1y^1z^1
Enter the values of x, y, z: 1 1 1
Evaluated result: 5
===== Menu =====
1. Represent and Evaluate a Polynomial P(x,y,z)
2. Find the Sum of Two Polynomials POLY1(x,y,z) and POLY2(x,y,z)
3. Exit
Enter your choice: 2
--- Enter POLY1 ---
Enter the number of terms in the polynomial: 2
Term 1:
Coefficient: 4
Powers of x, y, z: 1 1 1
Term 2:
Coefficient: 6
Powers of x, y, z: 1 1 1
POLY1: +4x^1y^1z^1 +6x^1y^1z^1
--- Enter POLY2 ---
Enter the number of terms in the polynomial: 2
Term 1:
Coefficient: 6
Powers of x, y, z: 1 1 1
Term 2:
Coefficient: 8
Powers of x, y, z: 1 1 1
POLY2: +6x^1y^1z^1 +8x^1y^1z^1
Sum of POLY1 and POLY2: +10x^1y^1z^1 +14x^1y^1z^1
===== Menu =====
1. Represent and Evaluate a Polynomial P(x,y,z)
2. Find the Sum of Two Polynomials POLY1(x,y,z) and POLY2(x,y,z)
3. Exit
Enter your choice:
No comments:
Post a Comment