Nptel Problem Solving Through Program In C assignment 10 Programig
p 1:-
#include<math.h>
int maxmitr;
do
{
if(fun(a)*fun(x)<0)
b=x;
else
a=x;
bisection(&x1,a,b,&itr);
if(fabs(x1-x)<allerr)
{
printf("Iteration Count=%d, root = %6.4f\n", itr, x1);
return 0;
}
x=x1;
}
while(itr<maxmitr);
return 1;
}
float fun(float x)
{
return(2*x*x*x-3*x-5);
}
void bisection(float *x,float a,float b,int *itr)
{
*x=(a+b)/2;
++(*itr);
}
p 2:-
float h, allerr=0.01;
for (itr=1; itr<=maxmitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
x0=x1;
}
printf("Root = %8.6f\n", x1);
return 1;
}
float f(float x)
{
return (x*x*x)-2*x-3;
}
float df (float x)
{
return 3*(x*x)-2;
}
p 3:-
void sort(int *a,int n)
{
int i,j;
int temp;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(*(a+j)>*(a+j+1))
{
temp=*(a+j);
*(a+j)=*(a+j+1);
*(a+j+1)=temp;
}
}
}
}
p 4:-
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
p 5:-
determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
* a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
if ( determinant == 0)
printf("The given matrix is not invertible\n");
else
printf("The given matrix is invertible\n");
return 0;
}