Monday, April 30, 2012

C Program To Implement Bisection Method Using Linked List

Hi friends,in this program i store the cofficients of the equation in the linked list and after reading the cofficient i find the solution using bisection method...carefully understand it and if you face a problem while reading  this program than share with me.



#include<stdio.h>
#include<conio.h>
struct node
{
       int info;
       struct node *next;
};
typedef struct node link;
link *front=NULL,*rear=NULL;
void equa(link **,link **,int);
float f(link*,float);
int main()
{
     char ch;
     int i,num,n;
     float x1,x2,x0;
     printf("\n1.cofficient enter");
     printf("\n2.exit:");
     do
     {
                      printf("\n Enter your choice:");
                      scanf("%d",&i);
                      switch(i)
                      {
                          case 1:
                               printf("\ Enter the number:");
                               scanf("%d",&num);   
                               equa(&rear,&front,num);
                               break;
                          case 2:
                               exit(0);
                               break;
                          default:
                                  printf("\n wrong choice:");
                      }           
                         
                               fflush(stdin);
                      printf("do you want to continue:");
                      scanf("%c",&ch);
     }
     while(ch=='y'||ch=='Y');
     printf("\n Enter the initia values from your choice:");
     scanf("%f %f",&x1,&x2);
     printf("\n Enter the number of iteration:");
     scanf("%d",&n);
     if(f(front,x1)*f(front,x2)<0)
     {
                                for(i=0;i<n;i++)
                                {
                                                x0=(x1+x2)/2.0;
                                                if(f(front,x0) * f(front,x1)<0)
                                                {
                                                                           x0=x2;
                                                }
                                                else if(f(front,x0) * f(front,x2)<0)
                                                {
                                                                           x0=x1;
                                                }
                                }
              printf("\n root is %f",x0);
     }
     else
     {
         printf("\n Roots are not posible:");
     }
     getch();
     return(0);
}
void equa(link **rr,link **ft,int num)
{
     link *ptr;
     ptr=(link*)malloc(sizeof(link));
     if(ptr!=NULL)
     {
                  ptr->info=num;
                  if(*ft==NULL)
                  {
                               *ft=ptr;
                               *rr=ptr;
                               ptr->next=NULL;
                  }
                  else
                  {
                               (*rr)->next=ptr;
                               *rr=ptr;
                               ptr->next=NULL;
                  }
     }
    
}
float f(link *ft,float x)                     
{
     link *temp;
     float sum=0;
     int i=3;
     temp=ft;
     while(temp!=NULL)
     {
                      if(i>=0)
                      {
                       sum=sum+(temp->info*pow(x,i));
                       i--;
                      }
                      temp=temp->next;
     }
     return(sum);
}
      



                   Thanks friends............               

No comments:

Post a Comment

Please Give Me Your Views

Popular Posts