-2

My code is meant to two create polynomials and either add, subtract or multiply them. When I run either of the options it creates both polynomials,prints out the first polynomial and then has a segmentation fault. It seems to occur between show(polys1) and the printf statement and I can't figure out why. Any help would be greatly appreciated. I have spent a while reading through similiar problems but could not find any that explained it for my program.

poly.h

#if ! defined(POLY_H)

#define POLY_H
struct link{
   int *coeff;
   int pow;
   int degree;
   }
llink;
void create(struct link *node);
void polyadd(struct link *poly1,struct link *poly2,struct link *poly);
void polysub(struct link *poly1,struct link *poly2,struct link *poly);
void polymul(struct link *n1, struct link *n2, struct link *n);
void show(struct link *node);
#endif

polyMain.c

#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
#include "poly.h"
struct link *poly1s=NULL,*poly2s=NULL,*polys=NULL;
  int main()
  {
  int op;
  char ch;
  do{
  poly1s=(struct link *)malloc(sizeof(struct link));
  poly2s=(struct link *)malloc(sizeof(struct link));
  polys=(struct link *)malloc(sizeof(struct link));
  printf("\n\nWhat do you want to do?\n1.Addition\n2.Subtraction\n3.Multiplication\n0.Exit\nEnter your choice:");
  scanf("%d",&op);
  switch(op)
  {
        case 1:
            printf("\n\nenter 1st polynomial:");
            create(poly1s);
            printf("\n\nenter 2nd polynomial:");
            create(poly2s);
            printf("\n1st Polynomial:\t");
            show(poly1s);
        printf("\n2nd Polynomial:\t");
            show(poly2s);
            polyadd(poly1s,poly2s,polys);
            printf("\nAdded polynomial:\t");
            show(polys);
            break;
        case 2:
            printf("\n\nenter 1st polynomial:\t");
            create(poly1s);
            printf("\n\nenter 2nd polynomial:\t");
            create(poly2s);
            printf("\n\n1st Polynomial:\t");
            show(poly1s);
            printf("\n\n2nd Polynomial:\t");
            show(poly2s);
            polysub(poly1s,poly2s,polys);
            printf("\n\nSubtracted polynomial:\t");
            show(polys);
            break;  
        case 3:
            printf("\n\nenter 1st polynomial:");
            create(poly1s);
            printf("\n\nenter 2nd polynomial:");
            create(poly2s);
            printf("\n\n1st Polynomial:\t");
            show(poly1s);
            printf("\n\n2nd Polynomial:\t");
            show(poly2s);
            polymul(poly1s,poly2s,polys);
            printf("\n\nMultiplied polynomial:\t");
            show(polys);
            break;
        }

 printf("\n Want to continue? Y/N:");
  ch=getchar();
  }
  while(op);
return 0;
}

poly.c

#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
#include "poly.h"

void create(struct link *node)
{
  printf("\n\nenter the degree of the polynomial:");
  scanf("%d",&node->degree);
  node->coeff=malloc((node->degree)*sizeof(int));
  int i;
  for(i = 0;i<node->degree;i++)
  {
  printf("\n\nenter coeff:");
  scanf("%d",&node->coeff[i]);
  }
  printf("\nenter power:");
  scanf("%d",&node->pow);
}

void show(struct link *node)
{
int i;
printf("%dx^%d",node->coeff[0],node->pow);
for(i = 1;i<node->degree;i++)
printf(" + %dx^%d",node->coeff[i],node->pow);
}

void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
  if(poly1->pow>poly2->pow)
  {
   poly->pow=poly1->pow;
   int i;
  for(i = 0;i<poly1->degree;i++)
   poly->coeff[i]=poly1->coeff[i];
   }
  else if(poly1->pow<poly2->pow)
  {
   poly->pow=poly2->pow;
   int i;
  for(i = 0;i<poly2->degree;i++)
   poly->coeff[i]=poly2->coeff[i];
   }
  else
  {
   poly->pow=poly1->pow;
   int j = poly1->degree-poly2->degree;
   int k;
   if(j > 0)
   k = poly1->degree;
   else k = poly2->degree;
   int i;
  for(i = 0;i<k;i++)
   {
   if(i<poly1->degree && i<poly2->degree)
   poly->coeff[i]=poly1->coeff[i]+poly2->coeff[i];
   else if(i<poly1->degree)
   poly->coeff[i]=poly1->coeff[i];
   else if(i<poly2->degree)
   poly->coeff[i]=poly2->coeff[i];
   }
   }
}

void polysub(struct link *poly1,struct link *poly2,struct link *poly)
{
  if(poly1->pow>poly2->pow)
  {
   poly->pow=poly1->pow;
   int i;
  for(i = 0;i<poly1->degree;i++)
   poly->coeff[i]=poly1->coeff[i];
   }
  else if(poly1->pow<poly2->pow)
  {
   poly->pow=poly2->pow;
   int i;
  for(i = 0;i<poly2->degree;i++)
   poly->coeff[i]=poly2->coeff[i];
   }
  else
  {
   poly->pow=poly1->pow;
   int j = poly1->degree-poly2->degree;
   int k;
   if(j > 0)
   k = poly1->degree;
   else k = poly2->degree;
   int i;
  for(i = 0;i<k;i++)
   {
   if(i<poly1->degree && i<poly2->degree)
   poly->coeff[i]=poly1->coeff[i]-poly2->coeff[i];
   else if(i<poly1->degree)
   poly->coeff[i]=poly1->coeff[i];
   else if(i<poly2->degree)
   poly->coeff[i]=poly2->coeff[i];
   }
   }
}

void polymul(struct link *n1, struct link *n2, struct link *n)
{
    struct link * n2beg=n2;

            while (n1) 
            {
                    struct link * temp=(struct link *)malloc(sizeof(struct link));  
                    n2=n2beg;
                    while (n2) 
                    {
                int j = n1->degree-n2->degree;
                    int k;
                if(j > 0)
                    k = n1->degree;
                    else k = n2->degree;
                int i;
                    for(i = 0;i<k;i++)
                    {
                if(i<n1->degree && i<n2->degree)
                    temp->coeff[i] = n1->coeff[i] * n2->coeff[i];
                else if(i<n1->degree)
                temp->coeff[i]=n1->coeff[i];
                else if(i<n2->degree)
                temp->coeff[i]=n2->coeff[i];
                }
                            temp->pow = n1->pow + n2->pow;
                    }
                    polyadd(temp,n,n);
                    free(temp);
            }
}
Sean Ryan
  • 37
  • 5

1 Answers1

0

I think the problem occurs because you haven't allocated any memory for coeff of polys before using

poly->coeff[i]= /*something*/

I haven't checked your whole code yet. So I don't know what you were trying to do. Try allocating some memory using malloc before using coeff of polys.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83