Saturday, 28 September 2013

wap in c to find the real root of the equation x*x*x-4*x-9 by using BISECTION METHOD.

#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#define EPS 0.00000005
#define f(x) x*x*x-4*x-9
void bisect();
int count=1,n;
float root=1;
void main()
{
clrscr():
 printf("\n solution by BISECTION METHOD \n");
 printf("\n equation is ");
 printf("\n \t\t\t x*x*x-4x-9=0\n\n");
 printf("enter the number of iterations:");
 scanf("%d",&n);
 bisect();
getch();
}
void bisect()
{
 float x0,x1,x2,f0,f1,f2;
 int i=0;
 for(x2=1;;x2++)
 {
  f2=f(x2);
  if(f2>0)
  {
   break;
  }
 }
 for(x1=x2-1;;x2--)
 {
  f1=f(x1);
  if(f1<0)
  {
   break;
  }
 }
 for(;count<=n;count++)
 {
  x0=(x1+x2)/2.0;
  f0=f(x0);
  if(f0==0)
  {
   root=x0;
  }
  if(f0*f1<0)
  {
   x2=x0;
  }
  else
  {
   x1=x0;
   f1=f0;
  }
  printf("\n\t\t iteration %d",count);
  printf("\t  :\t  %f",x0);
  if(fabs((x1-x2)/x1)<EPS)
  {
   printf("\n\t\t--------------------------------------------");
   printf("\n\t\t      root=%f",x0);
   printf("\n\t\t   iteration=%d\n",count);
   printf("\t\t----------------------------------------------");
   exit(0);
  }
 }
 printf("\n\t\t---------------------------------------------------");
 printf("\n\t\t\t root=%7.4f",x0);
 printf("\n\t\t\t iteration = %d\n",count-1);
 printf("\t\t------------------------------------------------------------------------------------------------------------");
 }

wap in c to find the real root of the equation xlog(x)-1.2=0 by using BISECTION METHOD.

#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#define EPS 0.00000005
#define f(x) (x)*log10(x)-1.2
void bisect();
int count=1,n;
float root=1;
void main()
{
clrscr();
 printf("\n solution by BISECTION METHOD \n");
 printf("\n equation is ");
 printf("\n \t\t\t x*log(x)-1.2=0\n\n");
 printf("enter the number of iterations:");
 scanf("%d",&n);
 bisect();
getch();
}
void bisect()
{
 float x0,x1,x2,f0,f1,f2;
 int i=0;
 for(x2=1;;x2++)
 {
  f2=f(x2);
  if(f2>0)
  {
   break;
  }
 }
 for(x1=x2-1;;x2--)
 {
  f1=f(x1);
  if(f1<0)
  {
   break;
  }
 }
 for(;count<=n;count++)
 {
  x0=(x1+x2)/2.0;
  f0=f(x0);
  if(f0==0)
  {
   root=x0;
  }
  if(f0*f1<0)
  {
   x2=x0;
  }
  else
  {
   x1=x0;
   f1=f0;
  }
  printf("\n\t\t iteration %d",count);
  printf("\t  :\t  %f",x0);
  if(fabs((x1-x2)/x1)<EPS)
  {
   printf("\n\t\t--------------------------------------------");
   printf("\n\t\t      root=%f",x0);
   printf("\n\t\t   iteration=%d\n",count);
   printf("\t\t----------------------------------------------");
   exit(0);
  }
 }
 printf("\n\t\t---------------------------------------------------");
 printf("\n\t\t\t root=%7.4f",x0);
 printf("\n\t\t\t iteration = %d\n",count-1);
 printf("\t\t------------------------------------------------------------------------------------------------------------");
 }

wap in c to find the real root of the equation x*x*x-x-1=0 by using BISECTION METHOD.

#include<stdio.h>
#include<math.h>
#include<conio.h>
#define EPS 0.00000005
#define f(x) x*x*x-x-1
void bisect();
int count=1,n;
float root=1;
void main()
{
clrscr();
 printf("\n solution by BISECTION METHOD \n");
 printf("\n equation is ");
 printf("\n \t\t\t x*x*x-x-1=0\n\n");
 printf("enter the number of iterations:");
 scanf("%d",&n);
 bisect();
getch();
}
void bisect()
{
 float x0,x1,x2,f0,f1,f2;
 int i=0;
 for(x2=1;;x2++)
 {
  f2=f(x2);
  if(f2>0)
  {
   break;
  }
 }
 for(x1=x2-1;;x2--)
 {
  f1=f(x1);
  if(f1<0)
  {
   break;
  }
 }
 for(;count<=n;count++)
 {
  x0=(x1+x2)/2.0;
  f0=f(x0);
  if(f0==0)
  {
   root=x0;
  }
  if(f0*f1<0)
  {
   x2=x0;
  }
  else
  {
   x1=x0;
   f1=f0;
  }
  printf("\n\t\t iteration %d",count);
  printf("\t  :\t  %f",x0);
  if(fabs((x1-x2)/x1)<EPS)
  {
   printf("\n\t\t--------------------------------------------");
   printf("\n\t\t      root=%f",x0);
   printf("\n\t\t   iteration=%d\n",count);
   printf("\t\t----------------------------------------------");
   exit(0);
  }
 }
 printf("\n\t\t---------------------------------------------------");
 printf("\n\t\t\t root=%7.4f",x0);
 printf("\n\t\t\t iteration = %d\n",count-1);
 printf("\t------------------------------------------------------------------------------------------------------------");
 }

Wednesday, 25 September 2013

WAP TO STORE DATA OF SPARSE MATRIX.

#include<stdio.h>
main()
{
int arr[100][100],arr1[100][3],i,j,m,n,a=1,b=0;
printf("enter the no of columns and rows\n");
scanf("%d%d",&m,&n);
arr1[0][0]=m;arr1[0][1]=n;
printf("enter the elements of the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
if(arr[i][j]!=0)
{
arr1[a][b]=i;
arr1[a][b+1]=j;
arr1[a][b+2]=arr[i][j];
a++;
}
}
arr1[0][2]=a-1;
printf("the sparse matrix is as follows\n");
for(i=0;i<a;i++){
for(j=0;j<3;j++)
printf("%d\t",arr1[i][j]);
printf("\n");}
}