Thursday, 25 April 2013

Program of Selection Sort Algorithm Implementation in C

Selection sort in c: c program for selection sort to sort numbers. This code implements selection sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.

#include <stdio.h>
 
int main()
{
   int array[100], n, c, d, position, swap;
 
   printf("Enter number of elements\n");
   scanf("%d", &n);
 
   printf("Enter %d integers\n", n);
 
   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);
 
   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;
 
      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }
 
   printf("Sorted list in ascending order:\n");
 
   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);
 
   return 0;
}

Program of Insertion Sort Algorithm Implementation in C

Insertion sort in c: c program for insertion sort to sort numbers. This code implements insertion sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.

/* insertion sort ascending order */
 
#include <stdio.h>
 
int main()
{
  int n, array[1000], c, d, t;
 
  printf("Enter number of elements\n");
  scanf("%d", &n);
 
  printf("Enter %d integers\n", n);
 
  for (c = 0; c < n; c++) {
    scanf("%d", &array[c]);
  }
 
  for (c = 1 ; c <= n - 1; c++) {
    d = c;
 
    while ( d > 0 && array[d] < array[d-1]) {
      t          = array[d];
      array[d]   = array[d-1];
      array[d-1] = t;
 
      d--;
    }
  }
 
  printf("Sorted list in ascending order:\n");
 
  for (c = 0; c <= n - 1; c++) {
    printf("%d\n", array[c]);
  }
 
  return 0;
}

Program of Bubble Sort in C Language Using Function

#include <stdio.h>
 
void bubble_sort(long [], long);
 
int main()
{
  long array[100], n, c, d, swap;
 
  printf("Enter number of elements\n");
  scanf("%ld", &n);
 
  printf("Enter %ld integers\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%ld", &array[c]);
 
  bubble_sort(array, n);
 
  printf("Sorted list in ascending order:\n");
 
  for ( c = 0 ; c < n ; c++ )
     printf("%ld\n", array[c]);
 
  return 0;
}
 
void bubble_sort(long list[], long n)
{
  long c, d, t;
 
  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (list[d] > list[d+1])
      {
        /* Swapping */
 
        t         = list[d];
        list[d]   = list[d+1];
        list[d+1] = t;
      }
    }
  }
}

Program for Bubble Sort in C

 C program for bubble sort: c programming code for bubble sort to sort 
numbers or arrange them in ascending order. You can easily modify it to 
print numbers in descending order.
 
/* Bubble sort code */
 
#include <stdio.h>
 
int main()
{
  int array[100], n, c, d, swap;
 
  printf("Enter number of elements\n");
  scanf("%d", &n);
 
  printf("Enter %d integers\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
 
  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }
 
  printf("Sorted list in ascending order:\n");
 
  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);
 
  return 0;
}

Tuesday, 2 April 2013

Program to Implement Iteration Method in C


//  EQN -> x*x*x - 2*x + 1 = 0

#include<stdio.h>
#include<conio.h>
#include<math.h>

#define EPS 0.00005
#define F(x) (x*x*x + 1)/2
#define f(x)  x*x*x - 2*x + 1

int n;

void iter();

void main()
{
clrscr();
printf("\n Solution by ITERATION METHOD ");
printf("\n\n Equation is -> x*x*x - 2*x + 1 = 0\n");
printf("\n Enter the no. of iterations ");
scanf("%d",&n);
iter();
getch();
}

void iter()
 {
 int i=0;
 float x1,x2,x0;
 float f1,f2,f0,error;
 for(x1=1; ;x1++)
    {
    f1=f(x1);
    if(f1>0)
      break;
    }
 for(x0=x1-1; ;x0--)
    {
    f0=f(x0);
    if(f0<0)
      break;
    }
 x2=(x0+x1)/2;
 printf("\n\n\t\t The 1 approximatrion to the root is : %f",x2);
 for(;i<n-1;i++)
    {
    f2=F(x2);
    printf("\n\n\t\t The %d approximatrion to the root is : %f",i+2,f2);
    x2=F(x2);
    error=fabs(f2-f1);
    if(error<EPS)
       break;
    f1=f2;
    }
 if(error>EPS)
   printf("\n\n\t NOTE :- The no. of iterations are not sufficient.");
 printf("\n\n\n\t\t -----------------------------------------------");
 printf("\n\t\t ROOT  = %.4f (Correct to 4 Decimal places)",f2);
 printf("\n\t\t -----------------------------------------------");
}

Program to Implement Newton Raphson Method to solve eqn. in C


 // EQN -> 3*x - cos(x) - 1 = 0

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>

// Formulae Declaration
#define f(x)   3*x - cos(x) - 1
#define df(x)  3+sin(x)
int n;

void NEW_RAP();

void main()
{
clrscr();
printf("\n Solution by NEWTON RAPHSON METHOD ");
printf("\n\n Equation is -> 3*x - cos(x) - 1 = 0\n");
printf("\n Enter the no. of iterations ");
scanf("%d",&n);
NEW_RAP();
getch();
}

void NEW_RAP()
 {
 float x1,x0;
 float f1,f0;
 float df0;
 int i=1,itr;
 float EPS,error;

 /* finding an approximate Root of a given equation, having +ve value*/
 for(x1=0.0;;x1+=0.01)
    {
    f1=f(x1);
    if(f1>0)
      break;
    }
 itr=n;
 x0=x1-0.01;
 f0=f(x0);
 printf("\n Enter the Maximum possible error: ");
 scanf("%f",&EPS);
 if(fabs(f0)>f1)
   printf("\n\t\t The root is near to %.4f\n\n\n",x1);
 if(f1>fabs(x0))
   printf("\n\t\t The root is near to %.4f\n\n\n",x0);
 x0=(x0+x1)/2;
 for(;i<=itr;i++)
    {
    f0=f(x0);
    df0=df(x0);
    x1=x0-(f0/df0);
    printf("\n\t\t The %d approximation to the root is : %f",i,x1);
    error=fabs(x1-x0);
    if(error<EPS)
      break;
    x0=x1;
    }
 if(error>EPS)
   {
   printf("\n\n\t NOTE :-");
   printf(" The No. of Iterartions are not sufficient. ");
   }
 printf("\n\n\n\t\t----------------------------------------------------");
 printf("\n\t\t ROOT  = %.4f ",x1);
 printf("\n\t\t----------------------------------------------------");
}

Program to Implement Regula Falsi (False Position) Method in C


   // EQN -> 3*x + sin(x) - exp(x) = 0


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>

// Formulae Declaration
#define EPS 0.00005
#define f(x)  3*x + sin(x) - exp(x)
int n;

void FAL_POS();

void main()
{
clrscr();
printf("\n Solution by ITERATION METHOD ");
printf("\n\n Equation is -> x*x*x - 2*x + 1 = 0\n");
printf("\n Enter the no. of iterations ");
scanf("%d",&n);
FAL_POS();
getch();
}

void FAL_POS()
 {
 long float x1,x2,x0;
 float f1,f2,f0;
 int itr=n,i;
 for(x1=0.0;;)
    {
    f1=f(x1);
    if(f1>0)
      break;
    else
      x1=x1+0.1;
    }
 x0=x1-0.1;
 f0=f(x0);
 printf("\n\t\t----------------------------------------------------");
 printf("\n \t\tITERATION\t x2\t\t\t   F(X)\n");
 printf("\n\t\t----------------------------------------------------\n");
 for(i=0;i<itr;i++)
    {
    x2=x0-((x1-x0)/(f1-f0))*f0;
    f2=f(x2);
    if(f0*f2>0)
      {
      x1=x2;
      f1=f2;
      }
    else
      {
      x0=x2;
      f0=f2;
      }
    if(fabs(f(2))>EPS)
      printf("\n\t\t %d \t\t %f \t\t %f \n",i+1,x2,f2);
    }
 printf("\n\n\n\t\t----------------------------------------------------");
 printf("\n\t\t ROOT  = %f ",x2);
 printf("\n\t\t----------------------------------------------------");
}

Program of Gauss Elimination Method in C

/*Gauss Elimination */
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define MAX 10
void main()
{
int i,j,n,k;
float mat[MAX][MAX],x[MAX],temp,pivot,sum=0;
clrscr();
printf("\t\t\t GAUSS ELIMINITION METHOD\n");
printf("-------------------------------------------------------------------\n");
printf("Enter No of Equtions : ");
scanf("%d",&n);
printf("Enter Coefficients of Eqution \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%f",&mat[i][j]);
printf("Enter Constant value\n");
for(i=1;i<=n;i++)
{
scanf("%f",&mat[i][n+1]);
x[i]=mat[i][n+1];
}
for(i=2;i<=n;i++)
{
for(j=i;j<=n;j++)
{
pivot=mat[j][i-1]/mat[i-1][i-1];
for(k=i-1;k<=n+1;k++)
mat[j][k]=mat[j][k]-pivot*mat[i-1][k];
}
}
printf("Eliminated matrix as :- \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
printf("\t%.2f",mat[i][j]);
printf("\n");
}
for(i=1;i<=n;i++)
{
if(mat[i][i]==0)
{
printf("Since diagonal element become zero\n Hence solution is not possible\n");
exit(1);
}
}
printf("Solution : \n");
for(i=0;i {
sum=0;
for(j=n;j>n-i;j--)
sum=sum+mat[n-i][j];

x[n-i]=(mat[n-i][n+1]-sum*x[n])/mat[n-i][n-i];
printf("X%d = %4.2f\n",n-i,x[n-i]);
}

getch();
}

OUTPUT

GAUSS ELIMINITION METHOD
--------------------------------------------------------------------Enter No of Equtions : 3
Enter Coefficients of Eqution
4 3 -2
1 1 1
3 -2 1
Enter Constant value
5 3 2
Eliminated matrix as :-
4.00 3.00 -2.00 5.00
0.00 0.25 1.50 1.75
0.00 0.00 28.00 28.00
Solution :
X3 = 1.00
X2 = 1.00
X1 = 1.00

Program of Bisection Method in C Programming

#include<stdio.h>
#include <math.h>
#include<conio.h>
#define ESP 0.001
#define F(x) (x)*(x)*(x) + (x)*(x) + (x) + 7
void main()
{
  int i = 1;
  float x0,x1,x2;
  double f1,f2,f0,t;
  clrscr();
  printf("\nEnter the value of x0: ");
  scanf("%f",&x0);
 
  printf("\nEnter the value of x1: ");
  scanf("%f",&x1);
  printf("\n__________________________________________________________________\n");
  printf("\nIteration\t x0\t       x1\t x2\t   f0\t   f1\t   f2");
  printf("\n___________________________________________________________________\n");
  do
  {
  x2=(x0+x1)/2;
  f0=F(x0);
  f1=F(x1);
  f2=F(x2);
  printf("\n%d %f %f %f %lf %lf %lf", i, x0,x1,x2,f0,f1,f2);
  if(f0*f2<0)
   {
    x1=x2;
   }
   else
   {
    x0=x2;
   }
   i++;
  }while(fabs(f2)>ESP);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",x2);
getch();
}
 
/*
 
OUTPUT
---------


Enter the value of x0: -2

Enter the value of x0: -1

Enter the value of x1: -2

__________________________________________________________

    x0         x1        x2        f0       f1      f2
__________________________________________________________

-1.000000 -2.000000 -1.500000 -5.000000 2.000000 -1.750000
-1.500000 -2.000000 -1.750000 -1.750000 2.000000  0.062500
-1.500000 -1.750000 -1.625000 -1.750000 0.062500 -0.859375
-1.625000 -1.750000 -1.687500 -0.859375 0.062500 -0.402344
-1.687500 -1.750000 -1.718750 -0.402344 0.062500 -0.170898
-1.718750 -1.750000 -1.734375 -0.170898 0.062500 -0.054443
-1.734375 -1.750000 -1.742188 -0.054443 0.062500  0.003967
-1.734375 -1.742188 -1.738281 -0.054443 0.003967 -0.025253
-1.738281 -1.742188 -1.740234 -0.025253 0.003967 -0.010647
-1.740234 -1.742188 -1.741211 -0.010647 0.003967 -0.003341
-1.741211 -1.742188 -1.741699 -0.003341 0.003967  0.000313
__________________________________________________________


App.root = -1.741699


*/