C Programming Blog

 
 
 
#include<stdio.h>
#include<math.h>
#include \"headfork.h\"
#include \"headshr.h\"
#include \"headsem.h\"

main()
{
    int coeff[5],x,id,nproc,p,sid,i;
    int *poly,sum,j;

    poly=(int *)shared(sizeof(int)*2,&sid);

    printf(\"Enter the X: \");
    scanf(\"%d\",&x);

    printf(\"Enter the maximum power :\");
    scanf(\"%d\",&p);

    printf(\"Enter the co-efficients : \");
    for(i=1;i<=p;i++)
    {
        printf(\"\\ncoeff[%d] : \",i);
        scanf(\"%d\",&coeff[i]);
    }

    printf(\"Enter the no.of proc :\");
    scanf(\"%d\",&nproc);

    id=p_fork(nproc);

    for(i=id+1;i<=p;i+=nproc)
    {
        *(poly+id)=coeff[p-i+1]*(pow(x,i));
    }

    p_join(nproc,id);
    sum=0;
    for(i=0;i<p;i++)
    {
        sum=sum + *(poly+i);
    }
    for(i=1,j=p;i<=p;i++,j--)
    {
        if(i==1)
            printf(\"a%d*x%d\",i,j);
        else
            printf(\" + a%d*x%d\",i,j);
    }
    printf(\"\\nPolynomial is : %d\",sum);
}

OUTPUT

knoppix@ttyp0[pp]$ cc poly_lps.c
knoppix@ttyp0[pp]$ ./a.out
Enter the X: 2
Enter the maximum power :3

Enter the co-efficients :
coeff[1] : 1
coeff[2] : 2
coeff[3] : 3

Enter the no.of proc :3
a1*x3 + a2*x2 + a3*x1
Polynomial is : 22

 
 
Didn't find what you were looking for? Find more on PROGRAM TO EALUATE A POLYNOMIAL WITH N NO.OF PROCESSES USING LOOP SPLITING