Divide and Conquer is a programming technique which makes the program more efficient to write. And this technique work on the concept of recursion to solve a problem step by step. Generally this technique work in three parts:- Divide:- Divide the problem into some subproblem. Conquer:- Conquer the subproblem by calling recursively until subproblem solved. Combine:- (Optional Step) Combine the subproblem solution. So, that we will get the final problem Solution. When the subproblems are large enough to solve recursively, we call the recursive case. Once the subproblem becomes small enough that we no longer recursive, we say that the recursion "bottom out" and that we have gotten down to the base case. Application of Divide and Conquer Quick Sort Strassen's algorithm for matrix multiplication Merge Sort Counting inversions Binary Search Finding Min and ...
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
void main()
{
again:
int gd=DETECT,gm,n;
double a[10][10],b[10][10],c[10][10];
int i,j,k,m,x1[10],y1[10],x[10],y[10],sx,sy;
char ch;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter the no of sides : ");
scanf("%d",&n);
if(n<3)
{
printf("\nPolygon can not form\n");
goto exit;
getch();
}
else
{
for(i=1;i<=n;i++)
{
printf("\nEnter the co-ordinates (x%d,y%d) : ",i,i);
scanf("%d%d",&x[i],&y[i]);
}
cleardevice();
for(i=1;i<n;i++)
{
setcolor(RED);
line(320+x[i],240-y[i],320+x[i+1],240-y[i+1]);
}
setcolor(RED);
line(320+x[1],240-y[1],320+x[n],240-y[n]);
}
getch();
//again:
printf("\nWhat you want to do : \n");
printf("\n1.Scaling");
printf("\n2.Translating");
printf("\n3.Reflection");
printf("\n4.Rotation");
printf("\n5.Shearing");
printf("\n6.Exit");
printf("\nPlease enter your choice : ");
scanf("%d",&m);
switch(m)
{
case 1:
{
printf("\nEnter the scaling points : ");
scanf("%d%d",&sx,&sy);
a[1][1]=sx;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=sy;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
int tx,ty;
printf("\nEnter the translation points : ");
scanf("%d%d",&tx,&ty);
a[1][1]=1;
a[1][2]=0;
a[1][3]=tx;
a[2][1]=0;
a[2][2]=1;
a[2][3]=ty;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 3:
{
int r;
printf("\nWhich direction");
printf("\n1.Reflection in x direction ");
printf("\n2.Reflection in y direction ");
printf("\n3.Reflection about origin");
printf("\nEnter choice : ");
scanf("%d",&r);
switch(r)
{
case 1:
{
a[1][1]=1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=-1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
a[1][1]=-1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 3:
{
a[1][1]=-1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=-1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
}
break;
}
case 4:
{
int q;
double s,o;
printf("\nEnter the angle : ");
scanf("%lf",&o);
s=(o*(3.14/180));
printf("\nWhich direction :");
printf("\n1.Clockwise direction ");
printf("\n2.Anticlockwise direction ");
printf("\nEnter choice : ");
scanf("%d",&q);
switch(q)
{
case 1:
{
a[1][1]=cos(s);
a[1][2]=sin(s);
a[1][3]=0;
a[2][1]=-sin(s);
a[2][2]=cos(s);
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
a[1][1]=cos(s);
a[1][2]=-sin(s);
a[1][3]=0;
a[2][1]=sin(s);
a[2][2]=cos(s);
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
}
break;
}
case 5:
{
int shx,shy;
printf("\nEnter the shearing points : ");
scanf("%d%d",&shx,­);
a[1][1]=1;
a[1][2]=shx;
a[1][3]=0;
a[2][1]=shy;
a[2][2]=1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
exit: case 6:
{
getch();
exit (0);
}
default :
{
printf("\nINVALID INPUT");
break;
}
}
for(i=1;i<=n;i++)
{
b[1][i]=x[i];
b[2][i]=y[i];
b[3][i]=1;
}
for(i=1;i<=3;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=0;
for(k=1;k<=3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=1;i<=n;i++)
{
x1[i]=c[1][i];
y1[i]=c[2][i];
}
for(i=1;i<n;i++)
{
setcolor(GREEN);
line(320+x1[i],240-y1[i],320+x1[i+1],240-y1[i+1]);
}
setcolor(GREEN);
line(320+x1[1],240-y1[1],320+x1[n],240-y1[n]);
getch();
printf("\nAre you want to continue :y/n ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
{
cleardevice();
goto again;
}
else
{
cleardevice();
goto exit;
}
getch();
}
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
void main()
{
again:
int gd=DETECT,gm,n;
double a[10][10],b[10][10],c[10][10];
int i,j,k,m,x1[10],y1[10],x[10],y[10],sx,sy;
char ch;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter the no of sides : ");
scanf("%d",&n);
if(n<3)
{
printf("\nPolygon can not form\n");
goto exit;
getch();
}
else
{
for(i=1;i<=n;i++)
{
printf("\nEnter the co-ordinates (x%d,y%d) : ",i,i);
scanf("%d%d",&x[i],&y[i]);
}
cleardevice();
for(i=1;i<n;i++)
{
setcolor(RED);
line(320+x[i],240-y[i],320+x[i+1],240-y[i+1]);
}
setcolor(RED);
line(320+x[1],240-y[1],320+x[n],240-y[n]);
}
getch();
//again:
printf("\nWhat you want to do : \n");
printf("\n1.Scaling");
printf("\n2.Translating");
printf("\n3.Reflection");
printf("\n4.Rotation");
printf("\n5.Shearing");
printf("\n6.Exit");
printf("\nPlease enter your choice : ");
scanf("%d",&m);
switch(m)
{
case 1:
{
printf("\nEnter the scaling points : ");
scanf("%d%d",&sx,&sy);
a[1][1]=sx;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=sy;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
int tx,ty;
printf("\nEnter the translation points : ");
scanf("%d%d",&tx,&ty);
a[1][1]=1;
a[1][2]=0;
a[1][3]=tx;
a[2][1]=0;
a[2][2]=1;
a[2][3]=ty;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 3:
{
int r;
printf("\nWhich direction");
printf("\n1.Reflection in x direction ");
printf("\n2.Reflection in y direction ");
printf("\n3.Reflection about origin");
printf("\nEnter choice : ");
scanf("%d",&r);
switch(r)
{
case 1:
{
a[1][1]=1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=-1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
a[1][1]=-1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 3:
{
a[1][1]=-1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=-1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
}
break;
}
case 4:
{
int q;
double s,o;
printf("\nEnter the angle : ");
scanf("%lf",&o);
s=(o*(3.14/180));
printf("\nWhich direction :");
printf("\n1.Clockwise direction ");
printf("\n2.Anticlockwise direction ");
printf("\nEnter choice : ");
scanf("%d",&q);
switch(q)
{
case 1:
{
a[1][1]=cos(s);
a[1][2]=sin(s);
a[1][3]=0;
a[2][1]=-sin(s);
a[2][2]=cos(s);
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
a[1][1]=cos(s);
a[1][2]=-sin(s);
a[1][3]=0;
a[2][1]=sin(s);
a[2][2]=cos(s);
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
}
break;
}
case 5:
{
int shx,shy;
printf("\nEnter the shearing points : ");
scanf("%d%d",&shx,­);
a[1][1]=1;
a[1][2]=shx;
a[1][3]=0;
a[2][1]=shy;
a[2][2]=1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
exit: case 6:
{
getch();
exit (0);
}
default :
{
printf("\nINVALID INPUT");
break;
}
}
for(i=1;i<=n;i++)
{
b[1][i]=x[i];
b[2][i]=y[i];
b[3][i]=1;
}
for(i=1;i<=3;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=0;
for(k=1;k<=3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=1;i<=n;i++)
{
x1[i]=c[1][i];
y1[i]=c[2][i];
}
for(i=1;i<n;i++)
{
setcolor(GREEN);
line(320+x1[i],240-y1[i],320+x1[i+1],240-y1[i+1]);
}
setcolor(GREEN);
line(320+x1[1],240-y1[1],320+x1[n],240-y1[n]);
getch();
printf("\nAre you want to continue :y/n ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
{
cleardevice();
goto again;
}
else
{
cleardevice();
goto exit;
}
getch();
}
Comments
Post a Comment