0% found this document useful (0 votes)
0 views

assignemnt1

The document explains the concept of polynomials, detailing their structure and representation. It includes a C program for polynomial multiplication, outlining functions for inserting, adding, multiplying, and viewing polynomials, as well as memory management. Additionally, it provides a brief HTML snippet for a calculator interface.

Uploaded by

Srivatsa N
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)
0 views

assignemnt1

The document explains the concept of polynomials, detailing their structure and representation. It includes a C program for polynomial multiplication, outlining functions for inserting, adding, multiplying, and viewing polynomials, as well as memory management. Additionally, it provides a brief HTML snippet for a calculator interface.

Uploaded by

Srivatsa N
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
You are on page 1/ 11

| 1JS21CS154

SUDEEPA DJ

POLYNOMIAL
A mathematical expression with more than two algebraic terms is called a polynomial. It is the
-accumulation of numerous words with various powers of the same variable. A polynomial is
represented by the notation p(x). There must be at least one variable in a polynomial expression
representation, and usually there are also constants and positive constants. The equation is
written as a1xn+ a2xn1+a3xn2+..........+anx0, where x is the variable, a1, a2, a3,.........., an are
the coefficients, and n is the degree of the polynomial. A real number must be used for the
coefficients.

1. Design, develop and Implement polynomial multiplication program in C


#include<stdio.h>
#include<stdlib.h>
struct node {
int coefficient, exponent;
struct node *next;
};
struct node *hPtr1, *hPtr2, *hPtr3;
struct node * buildNode(int coefficient, int exponent) {
struct node *ptr = (struct node *) malloc(sizeof (struct node));
ptr->coefficient = coefficient;
ptr->exponent = exponent;
ptr->next = NULL;
return ptr;
}
void polynomial_insert(struct node ** myNode, int coefficient, int exponent)
{
struct node *lPtr, *pPtr, *qPtr = *myNode;
lPtr = buildNode(coefficient, exponent);
if (*myNode == NULL || (*myNode)->exponent < exponent)
{
*myNode = lPtr;
(*myNode)->next = qPtr;
return;
}
while (qPtr)
P a g e 1 | 11
| 1JS21CS154
SUDEEPA DJ

{
pPtr = qPtr;
qPtr = qPtr->next;
if (!qPtr) {
pPtr->next = lPtr;
break;
}
else if ((exponent < pPtr->exponent) && (exponent > qPtr->exponent))
{
lPtr->next = qPtr;
pPtr->next = lPtr;
break ;
}
}
return;
}
void polynomial_add(struct node **n1, int coefficient, int exponent)
{
struct node *x = NULL, *temp = *n1;
if (*n1 == NULL || (*n1)->exponent < exponent)
{
*n1 = x = buildNode(coefficient, exponent);
(*n1)->next = temp;
} else
{
while (temp)
{
if (temp->exponent == exponent)
{
temp->coefficient = temp->coefficient + coefficient;
return;
}
if (temp->exponent > exponent && (!temp->next || temp->next->exponent < exponent))
P a g e 2 | 11
| 1JS21CS154
SUDEEPA DJ

{
x = buildNode(coefficient, exponent);
x->next = temp->next;
temp->next = x;
return;
}
temp = temp->next;
}
x->next = NULL;
temp->next = x;
}
}
void polynomial_multiply(struct node **n1, struct node *n2, struct node *n3)
{
struct node * temp;
int coefficient, exponent;
temp = n3;
if (!n2 && !n3)
return;
if (!n2)
{
*n1 = n3;
}
else if (!n3)
{
*n1 = n2;
}
else
{
while (n2)
{
while (n3)
{
P a g e 3 | 11
| 1JS21CS154
SUDEEPA DJ

coefficient = n2->coefficient * n3->coefficient;


exponent = n2->exponent + n3->exponent;
n3 = n3->next;
polynomial_add(n1, coefficient, exponent);
}
n3 = temp;
n2 = n2->next;
}
}
return;
}
struct node * polynomial_deleteList(struct node *ptr)
{
struct node *temp;
while (ptr){
temp = ptr->next;
free(ptr);
ptr = temp;
}
return NULL;
}
void polynomial_view(struct node *ptr)
{
int i = 0;
int flag=0;
while (ptr) {
if(ptr->exponent != 0 || ptr->exponent != 1 ){
if(ptr->coefficient > 0 && flag==0 ){
printf("%dx^%d", ptr->coefficient,ptr->exponent);
flag++;
}
else if (ptr->coefficient > 0 && flag==1 )
printf("+%dx^%d", ptr->coefficient,ptr->exponent);
P a g e 4 | 11
| 1JS21CS154
SUDEEPA DJ

else if(ptr->coefficient < 0)


printf("%dx^%d", ptr->coefficient,ptr->exponent);
}
else if (ptr->exponent == 0){
if(ptr->coefficient > 0 && flag==0 ){
printf("%d", ptr->coefficient);
flag++;
}
else if (ptr->coefficient > 0 && flag==1 )
printf("+%d", ptr->coefficient);
else if(ptr->coefficient < 0)
printf("%d", ptr->coefficient);
}
else if( ptr->exponent == 1 ){
if(ptr->coefficient > 0 && flag==0 ){
printf("%dx", ptr->coefficient);
flag++;
}
else if (ptr->coefficient > 0 && flag==1 )
printf("+%dx", ptr->coefficient);
else if(ptr->coefficient < 0)
printf("%dx", ptr->coefficient);
}
ptr = ptr->next;
i++;
}
printf("\n");
return;
}

int main (int argc, char *argv[])


{
int coefficient, exponent, i, n;
P a g e 5 | 11
| 1JS21CS154
SUDEEPA DJ

int count;
printf("Enter the number of coefficients in the multiplicand:");
scanf("%d",&count);
for(i=0;i<count;i++){
printf("Enter the coefficient part:");
scanf("%d", &coefficient);
printf("Enter the exponent part:");
scanf("%d",&exponent);
polynomial_insert(&hPtr1, coefficient, exponent);
}
printf("Enter the number of coefficients in the multiplier:");
scanf("%d",&count);
for(i=0;i<count;i++){
printf("Enter the coefficient part:");
scanf("%d", &coefficient);
printf("Enter the exponent part:");
scanf("%d",&exponent);
polynomial_insert(&hPtr2, coefficient, exponent);
}
printf("Polynomial Expression 1: ");
polynomial_view(hPtr1);
printf("Polynomial Expression 2: ");
polynomial_view(hPtr2);

polynomial_multiply(&hPtr3, hPtr1, hPtr2);

printf("Output:\n");
polynomial_view(hPtr3);
hPtr1 = polynomial_deleteList(hPtr1);
hPtr2 = polynomial_deleteList(hPtr2);
hPtr3 = polynomial_deleteList(hPtr3);
return 0;
}
P a g e 6 | 11
| 1JS21CS154
SUDEEPA DJ

Output

P a g e 7 | 11
| 1JS21CS154
SUDEEPA DJ

!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Calculator</title>
<meta name="viewport" content="width=device-width, initial-
scale=1" />
<link rel="stylesheet" type="text/css" media="screen"
href="main.css" />
<script src="main.js"></script>
</head>
<body>
<h1>Calculator</h1>
<form action="/" method="post">
<input type="text" name="n1" placeholder="number 1" />
<input type="text" name="n2" placeholder="number 2" />
<button type="submit" name="submit">Calculate</button>
</form>
</body>
</html>
if(n<(m-n))
{
printf("It is sparse matrix !");
a[0].row=r;
a[0].col=c;

}
else
{
printf("******************\n");
printf("It is not a sparse matrix,cannot add elements");
return 0;
}
printf("Enter the elements:\n");
for(int i=1; i<=a[0].row; i++)
{
for(int j=1; j<=a[0].col; j++)
P a g e 8 | 11
| 1JS21CS154
SUDEEPA DJ

{
scanf("%d", &ele);
if(ele)
{
a[currentA].row = i-1;
a[currentA].col = j-1;
a[currentA++].value = ele;
a[0].value++;
}
}
}
printf("The sparse matrix entered is :\n");
printf("Row\tCol\tValue\n");
for(int i=0; i<currentA; i++)
printf("%d\t%d\t%d\n",a[i].row,a[i].col,a[i].value);
transpose(a,b);
printf("The sparse matrix after transpose is :\n");
printf("Row\tCol\tValue\n");
for(int i=0; i<currentA; i++)
printf("%d\t%d\t%d\n",b[i].row,b[i].col,b[i].value);
return 0;
}
void transpose(terms a[], terms b[])
{
int n,i,j,currentb;
n = a[0].value;
b[0].row = a[0].col;
b[0].col = a[0].row;
b[0].value = n;
if(n>0)
P a g e 9 | 11
| 1JS21CS154
SUDEEPA DJ

{ currentb = 1;
for(i=0; i<a[0].col; i++)
for(j=1; j<=n; j++)
if(a[j].col == i)
{ b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++;} } }

OUTPUT

P a g e 10 | 11
| 1JS21CS154
SUDEEPA DJ

P a g e 11 | 11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy