Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

PROGRAM TO EALUATE A POLYNOMIAL WITH N NO.OF PROCESSES USING LOOP SPLITING

Posted By: Mena Schmidt     Category: C Programming     Views: 4134

PROGRAM TO EALUATE A POLYNOMIAL WITH N NO.OF PROCESSES USING LOOP SPLITING.

Code for PROGRAM TO EALUATE A POLYNOMIAL WITH N NO.OF PROCESSES USING LOOP SPLITING in C Programming

#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
  
Share: 



Mena Schmidt
Mena Schmidt author of PROGRAM TO EALUATE A POLYNOMIAL WITH N NO.OF PROCESSES USING LOOP SPLITING is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!