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 max Divide and Conquer Abstract Algorithm DAC(a,i,j) {
/*Cohen sutherland polygon clipping in c*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void leftclip();
void rightclip();
void topclip();
void bottomclip();
int gd=DETECT,gm,Xmin,Ymin,Xmax,Ymax,x1,y1;
void main()
{
int n,i=1,x[10],y[10];
initgraph(&gd,&gm,"c://tc//bgi");
printf("enter the Xmin,Ymin,Xmax,Ymax co ordinate of window port");
scanf("\n%d\t%d\t%d\t%d",&Xmin,&Ymin,&Xmax,&Ymax);
printf("enter the no. of side of polygon u want to enter");
scanf("\n%d",&n);
for(i=1;i<=n;i++)
{
printf("the co ordinates of sides are x[%d] y[%d]",i,i);
scanf("\n%d\t%d",&x[i],&y[i]);
}
cleardevice();
rectangle(Xmin,Ymin,Xmax,Ymax);
getch();
for(i=1;i<n;i++)
{ setcolor(i);
line(x[i],y[i],x[i+1],y[i+1]);
} setcolor(2);
line(x[1],y[1],x[n],y[n]);
getch();
leftclip();
rightclip();
topclip();
bottomclip();
getch();
}
void leftclip()
{
for(y1=0;y1<480;y1++)
{
for(x1=0;x1<Xmin;x1++)
{
putpixel(x1,y1,BLACK);
}}}
void rightclip()
{
for(y1=0;y1<480;y1++)
{
for(x1=Xmax+1;x1<640;x1++)
{
putpixel(x1,y1,BLACK);
}}}
void topclip()
{
for(x1=0;x1<640;x1++)
{
for(y1=0;y1<Ymin;y1++)
{
putpixel(x1,y1,BLACK);
}} }
void bottomclip()
{
for(x1=0;x1<640;x1++)
{
for(y1=Ymax+1;y1<480;y1++)
{
putpixel(x1,y1,BLACK);
}}}
Comments
Post a Comment