Showing posts with label ALGO LAB. Show all posts
Showing posts with label ALGO LAB. Show all posts

Saturday, 8 March 2014

FLOOD FILL ALGORITHM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO IMPLEMENT FLOOD FILL ALGORITHM*/

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void fill_right(int x,int y)
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);
}
}
void fill_left(int x,int y)
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);
}
}
int main()
{
int x , y ,a[10][10];
int gd=DETECT, gm ,n,i;
initgraph(&gd,&gm,"c:\tc\bgi");
printf("Enter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\nEnter the cordinates of polygon :\n\n ");
for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
printf("\n\nEnter the seed pt. : ");
scanf("%d %d",&x,&y);
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
fill_right(x,y);
fill_left(x-1,y);
getch();
}

BOUNDARY FILL ALGORITHM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO IMPLELMENT BOUNDARY FILL ALGORITHM*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void colouring(int,int,int,int,int);
int main()
{
    int gd=DETECT,gm,x,y,a=0,b=0,c,tempx,tempy,left,right,top,bottom;
    initgraph(&gd,&gm,"C:\TC\BGI");
    printf("Enter the left, top, right & bottom coordinates of rectangle: ");
    scanf("%d %d %d %d",&left,&top,&right,&bottom);
    rectangle(left,top,right,bottom);
    printf("Enter the corresponding code of color to be filled");
    scanf("%d",&c);
    tempx=(right-left)/2;
    tempy=(bottom-top)/2;
    x=tempx+left;
    y=tempy+top;
    printf("%d %d",x,y);
    while(a!=tempx | b!=tempy)
    {printf("%d %d\n",a,b);
        colouring(x,y,a,b,c);
        if(a<=tempx)
        a++;
        if(b<=tempy)
        b++;
    }
    getch();
    closegraph();
}

void colouring(int x,int y,int a,int b,int c)
{
    int i,j;
    for(i=0; i<=a; i++)
    {
        for(j=0; j<=b; j++)
        {
            putpixel(x,y,c);
            putpixel(x-i,y,c);
            putpixel(x+i,y,c);
            putpixel(x,y-j,c);
            putpixel(x,y+j,c);
            putpixel(x-i,y-j,c);
            putpixel(x-i,y+j,c);
            putpixel(x+i,y-j,c);
            putpixel(x+i,y+j,c);
        }
    }
}

3D TRANSFORMATION AND ROTATION

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-3D TRANSFORMATION AND ROTATION*/



#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

#include<graphics.h>




void draw();

void rotate();

int x12,x22,y12,y22,mx,my,depth;

int main()

{


    int gd=DETECT,gm,c;

    initgraph(&gd,&gm,"C:\tc\bgi");

    printf("\n3D Transformation Rotating\n\n");

    printf("\nEnter 1st top value(x12,y12):");

    scanf("%d%d",&x12,&y12);

    printf("Enter right bottom value(x2,y2):");

    scanf("%d%d",&x22,&y22);

    depth=(x22-x12)/4;

    mx=(x12+x22)/2;

    my=(y12+y22)/2;

    draw();

    getch();

    cleardevice();

    rotate();

    getch();

}



void draw()

{

    bar3d(x12,y12,x22,y22,depth,1);

}



void rotate()

{

    float t;

    int a1,b1,a2,b2,dep;

    printf("Enter the angle to rotate=");

    scanf("%f",&t);

    t=t*(3.14/180);

    a1=mx+(x12-mx)*cos(t)-(y12-my)*sin(t);

    a2=mx+(x22-mx)*cos(t)-(y22-my)*sin(t);

    b1=my+(x12-mx)*sin(t)-(y12-my)*cos(t);

    b2=my+(x22-mx)*sin(t)-(y22-my)*cos(t);

    if(a2>a1)

       dep=(a2-a1)/4;

    else

      dep=(a1-a2)/4;

    bar3d(a1,b1,a2,b2,dep,1);

    setcolor(5);

    //draw();

}


Thursday, 19 September 2013

FIND MAX AND MIN SIMULTANEOUSLY C PROGRAM


/*CSEMATTER.BLOGSPOT.IN

 Program:C Program to find maximum and minimum simultaneously*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,arr[20],max,min,i,j;
clrscr();
printf("enter the no. of elemnets to be enterd");
scanf("%d",&n);
//entering data in array
printf("enter the elements");
for(i=1;i<=n;i++)
scanf("%d",&arr[i]);
// assigning last element in max and min
max=arr[1];
min=arr[1];
//comparing the elements with max and min
 for(i=2;i<n;i++)
 {
 if(arr[i]> max)
 {
  max=arr[i];
 }
 if(arr[i]< min)
 {
 min=arr[i];
 }
//printing the elements
printf("minimum element is %d",min);
printf("\nmaximum element is %d",max);
getch();
}
/*OUTPUT:
enter the no. of elemnts to be enterd 6
enter the elements
15   6    2   18   5  78
minimum element is 2
maximum element 78
*/

Wednesday, 4 September 2013

STRASSEN'S MATRIX MULTIPLICATION C PROGRAM



/*CSEMATTER.BLOGSPOT.IN
PROGRAM:C PROGRAM FOR STRASSEN'S MATRIX MULTIPLICATION*/

#include<stdio.h>
#include<conio.h>
void main()
{
int arr1[2][2],arr2[2][2],i,j,p1,p2,p3;
int p4,p5,p6,p7,r,s,t,u,a,b,c,d,e,f,g,h;
clrscr();
printf("enter the elements of matrix1\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("enter the value of a[%d][%d]",i,j);
scanf("%d",&arr1[i][j]);
}}
printf("enter the elements of matrix2\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("enter the value of a[%d][%d]",i,j);
scanf("%d",&arr2[i][j]);
}}
a=arr1[0][0];
b=arr1[0][1];
c=arr1[1][0];
d=arr1[1][1];
e=arr2[0][0];
f=arr2[0][1];
g=arr2[1][0];
h=arr2[1][1];
p1=(a*f)-(a*h);
p2=(a+b)*h;
p3=(c+d)*e;
p4=(g-e)*d;
p5=(a+d)*(e+h);
p6=(b-d)*(g+h);
p7=(a-c)*(e+f);
r=p5+p4-p2+p6;
s=p1+p2;
t=p3+p4;
u=p5+p1-p3-p7;
printf("%d\t %d\n %d \t %d",r,s,t,u);
getch();
}
/*OUTPUT
enter the elements of matrix1
enter the value of a[0][0] 1
enter the value of a[0][1] 3
enter the value of a[1][0] 5
enter the value of a[1][1] 7
enter the elements of matrix2
enter the value of a[0][0] 8
enter the value of a[0][1] 4
enter the value of a[1][0] 6
enter the value of a[1][1] 2
 26   10
 82   34
*/

SELECTION SORT C PROGRAM


/*CSEMATTER.BLOGSPOT.IN

Program: C program to implement selection sort*/

#include<stdio.h>
#include<conio.h>
void main( )
{
int arr[10];
int i, j, temp,n ;
printf("enter the no. of inputs");
scanf("%d",&n);
printf("enter the numbers");
for(i=0;i<n;i++)
{scanf("%d",&arr[i]);}
for ( i = 0 ; i < n-1 ; i++ )
{for ( j = i + 1 ; j < n ; j++ )
{
if ( arr[i] > arr[j] )
{
temp = arr[i] ;
       arr[i] = arr[j] ;
arr[j] = temp ;
}
} }
 printf ( "Array after sorting:\n") ;
for ( i = 0 ; i < n ; i++ )
printf ( "%d\t", arr[i] ) ;
}
/*Output
enter the no. of inputs 5
enter the numbers
12  4   67  1   90
Array after sorting:
1   4   12  67  90*/

QUICK SORT C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM NAME:PROGRAM IN C FOR QUICK SORT*/
#include<stdio.h>
#include<conio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
void main()
{
int a[30],n,i;
clrscr();
printf("enter the no. of element:");

scanf("%d",&n);
printf("enter the element");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

quick_sort(a,0,n-1);
printf("\sorted elements\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void quick_sort(int a[],int p,int r)
{int q;
if(p<r)
{
q=partition(a,p,r);
quick_sort(a,p,q-1);
quick_sort(a,q+1,r);}
}

int partition(int a[],int p,int r)
{
int x;
int i,temp,j;
i=p-1;
x=a[r];
for(j=p;j<r-1;j++)
{
if(a[j]<=x)
{
i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;}
temp=a[i+1];
a[i+1]=a[r];
a[r]=a[i+1];}
return(i+1);
}

MERGE SORT C PROGRAM



/*CSEMATTER.BLOGSPOT.IN
PROGRAM:C program to implement merge sort algorithm*/
#include<stdio.h>
#include<conio.h>
void mergesort(int[], int, int);
void merge(int[], int, int, int);
void main()
{
int n,i,arr[20];
clrscr();
printf("Enter the no. of elements of array");
scanf("%d",&n);
printf("Enter the elements of array");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
mergesort(arr,0,n-1);
printf("Sorted array is:");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
getch();
}

void mergesort(int arr[],int p,int r)
{
int q;
if(p<r)
{
q=(p+r)/2;
mergesort(arr,p,q);
mergesort(arr,q+1,r);
merge(arr,p,q,r);
}
}

void merge(int arr[], int p, int q, int r)
{
int n1,n2,l[20],r1[20],i,j,k;
n1=q-p+1;
n2=r-q;
for(i=0;i<n1;i++)
{
l[i]=arr[p+i-1];
}
for(i=0;i<n2;i++)
{
r1[i]=arr[q+i];
}
l[n1]=1/0;
r1[n2]=1/0;
i=1;
j=1;
for(k=p;k<r;k++)
{
if(l[i]>r1[j])
{
arr[k]=l[i];
i++;
}
else
{
arr[k]=r1[j];
j++;
}
}
}

LONGEST COMMON SUBSEQUENCE C PROGRAM



/*CSEMATTER.BLOGSPOT.IN
PROGRAM:C PROGRAM FOR LCS*/

#include<stdio.h>
#include<conio.h>
void print_lcs(char b[20][20],char x[],int,int);
int main()
{
char arr[10],arr1[10];
char b[20][20];
int i,j,k,num,num1,c[20][20];
printf("enter the values to be entered for 1st");
scanf("%d",&num);
 printf("enter the string x\n");
 for(i=1;i<=num;i++)
 scanf("%s",&arr[i]);
 printf("enter the values to be entered for 2nd");
 scanf("%d",&num1);
 printf("enter the string y\n");
  for(i=1;i<=num1S;i++)
  scanf("%s",&arr1[i]);
   for(i=1;i<=num;i++)
   c[i][0]=0;
for(j=0;j<=num1;j++)
c[0][j]=0;
  for(i=1;i<=num;i++)
   {
    for(j=1;j<=num1;j++)
     {
     if(arr[i]==arr1[j])
     {c[i][j]=c[i-1][j-1]+1;
      b[i][j]='d'; // d is for diagonal
     }
     else if(c[i-1][j]>=c[i][j-1])
     {c[i][j]=c[i-1][j];
     b[i][j]='u'; // u is for up
     }
   else
     {c[i][j]=c[i][j-1];
     b[i][j]='s'; // s is for right side
     }
     }
  }
    for(i=0;i<=num;i++)
    {
    for(j=0;j<=num1;j++)
     {
      printf("\t%d",c[i][j]);
     }
     printf("\n");
    }
printf("\n the notation matrix is:\n");
 for(i=1;i<=num;i++)
 {
  for(j=1;j<=num1;j++)
  {
   printf("\t%c",b[i][j]);
  }
  printf("\n");
 }
  printf("\nthe mateched string \n");
  print_lcs(b,arr,num,num1);

   getch();
}

void print_lcs(char e[20][20],char x[10],int i,int j)
{
 if(i==0 || j==0)
 {
  return ;
 }
 if( e[i][j]=='d')
 {
 printf("\t%c",x[i]);
 print_lcs(e,x,i-1,j-1);
 }
 else if (e[i][j]=='u')
{
print_lcs(e,x,i-1,j);
}
else
{
 print_lcs(e,x,i,j-1);
}
}

/*OUTPUT:

enter the values to be entered for 1st7
enter the string x
a
b
c
b
d
a
b
enter the values to be entered for 2nd6
enter the string Y
b
d
c
a
b
a

        0       0       0       0       0       0       0
        0       0       0       0       1       1       1
        0       1       1       1       1       2       2
        0       1       1       2       2       2       2
        0       1       1       2       2       3       3
        0       1       2       2       2       3       3
        0       1       2       2       3       3       4
        0       1       2       2       3       4       4

 the notation matrix is:
        u       u       u       d       s       d
        d       s       s       u       d       s
        u       u       d       s       u       u
        d       u       u       u       d       s
        u       d       u       u       u       u
        u       u       u       d       u       d
        d       u       u       u       d       u

the mateched string
        a       b       c       b
*/

HEAP SORT C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
Program:C program to implement heap sort*/

#include<stdio.h>
#include<conio.h>
void heapsort(int[],int);
void maxheap(int[],int);
void heapify(int[],int);
void main()
{
 int n,i,a[50];
 printf("Enter the no. to be input:\n");
 scanf("%d",&n);
 printf("Enter the elements:\n");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 heapsort(a,n);
 printf("\nThe Sorted Elements Are:");
 for(i=0;i<n;i++)
  printf("\t%d",a[i]);
 getch();
}
void heapsort(int a[],int n)
{
 int i,t;
 maxheap(a,n);
 for(i=n-1;i>0;i--)
 {
  t = a[0];
  a[0] = a[i];
  a[i] = t;
  heapify(a,i);
 }
}
void maxheap(int a[],int n)
{
 int k,i,j,item;
 for(k=1;k<n;k++)
 {
  item = a[k];
  i = k;
  j = (i-1)/2;
 while((i>0)&&(item>a[j]))
  {
   a[i] = a[j];
   i = j;
   j = (i-1)/2;
  }
  a[i] = item;
 }
}

void heapify(int a[],int n)
{
 int i,j,item;

 j = 0;
 item = a[j];
 i = 2*j+1;

 while(i<=n-1)
 {
  if(i+1 <= n-1)
   if(a[i] <a[i+1])
    i++;
  if(item<a[i])
  {
   a[j] = a[i];
   j = i;
   i = 2*j+1;
  }
  else
   break;
 }
 a[j] = item;
}
/*Output
Enter the no. to be input: 5
Enter the elements:
12 34 7 9 22
The Sorted Elements Are:
7 9 12 22 34*/

FRACTIONAL KNAPSACK C PROGRAM



/*CSEMATTER.BLOGSPOT.IN

Program:C Program to implement fractional kanpsack*/


#include<stdio.h>
#include<conio.h>
int main()
{
    float min(float ,float);
    int i,j,n;
    float p[20],w[20],v[20],sol[20],amt,W,temp,sum_sol=0.0;
    printf("Enter the no. of entities ");
    scanf("%d",&n);
    printf("Enter the no. of entities of each items");
    for(i=0; i<n; i++)
        scanf("%f",&w[i]);
    printf("Enter respective costs of entities");
    for(i=0; i<n; i++)
        scanf("%f",&v[i]);
    printf("Enter the capacity of knapsack: ");
    scanf("%f",&W);

    for(i=0; i<n; i++)
        p[i]=v[i]/w[i];
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(p[i]<p[j])
            {
                temp=p[i];
                p[i]=p[j];
                p[j]=temp;

                temp=w[i];
                w[i]=w[j];
                w[j]=temp;
            }
        }
    }
    i=0;
    while(W>0)
    {
        amt=min(W,w[i]);
        sol[i]=amt;
        W=W-amt;
        i++;
    }
    printf("Solution array is :");
    for(i=0; i<n; i++)
        printf("%f \t",sol[i]);
        for(i=0;i<n;i++)
        sum_sol=sum_sol+sol[i];
        printf("\n Hence maximum profit entity is: %f",sum_sol);
    getch();
}

float min(float W,float a)
{
    if(W>a)
        return a;
    else
        return W;
}

/*OUTPUT:
Enter the no. of entities 5
Enter the no. of entities of each items5
10
20
30
40
Enter respective costs of entities30
20
100
90
160
Enter the capacity of knapsack: 60
Solution array is :5.000000     20.000000       35.000000       0.000000      0.000000
 Hence maximum profit entity is: 60.000000  */

0/1 KNAPSACK C PROGRAM


/*CSEMATTER.BLOGSPOT.IN

Program:C Program to implement 0/1 Knapsack*/


#include<stdio.h>
#include<conio.h>
int main()
{
    int i,j,n,w[20],v[20],W,c[10][10];
    printf("Enter the no. of entities ");
    scanf("%d",&n);
    printf("Enter the no. of entities of each items");
    for(i=0; i<n; i++)
        scanf("%d",&w[i]);
    printf("Enter respective costs of entities");
    for(i=0; i<n; i++)
        scanf("%d",&v[i]);
    printf("Enter the capacity of knapsack: ");
    scanf("%d",&W);

    for(i=0;i<=W;i++)
    c[0][i]=0;
    for(i=1;i<=n;i++)
    c[i][0]=0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=W;j++)
        {
            if(w[i]<=j)
            {
                if((v[i]+c[i-1][j-w[i]])>c[i-1][j])
                c[i][j]=v[i]+c[i-1][j-w[i]];
                else
                {c[i][j]=c[i-1][j];}
            }
            else
            {c[i][j]=c[i-1][j];}
        }
    }
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=W;j++)
        {
            printf("%d \t",c[i][j]);
        }
        printf("\n");
    }
    printf("Value of maximum profit entity for knapsack is: %d",c[n][W]);
    getch();
}

/*OUTPUT:
Enter the no. of entities 4
Enter the no. of entities of each items2
3
4
5
Enter respective costs of entities3
4
5
6
Enter the capacity of knapsack: 5
0       0       0       0       0       0
0       0       3       3       3       3
0       0       3       4       4       7
0       0       3       4       5       7
0       0       3       4       5       7
Value of maximum profit entity for knapsack is: 7   */

Kth SMALLEST NUMBER C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM:C program to find Kth smallest number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,arr[20],i,j,temp,find;
clrscr();
printf("enter the no. of elemnets to be enterd");
scanf("%d",&n);
//entering data in array
 printf("enter the elements");
 for(i=0;i<n;i++)
 scanf("%d",&arr[i]);
//performing binary sort
  for(i=0;i<n;i++)
  {
  for(j=0;j<n-i;j++)
  {
  if(arr[j]>arr[j+1])
  {temp=arr[j];
  arr[j]=arr[j+1];
  arr[j+1]=temp;}}}
//input the term to be find
printf("enter the Kth minimum element to be found\n");
scanf("%d",&find);
//checking the existence of index
 if(find<0 || find >n)
 printf("index not exist");
else
//printing the output
printf("element is %d",arr[find-1]);
getch();
}
/*OUTPUT:
enter the no. of elemnts to be enterd 6
enter the elements
5  2  28  0   7  6
enter the Kth minimum element to be found
3
element is 5*/