User:Dpsh1/sandbox

BRESENHAM::-- void main { int dx,dy,x,y,p,x1,y1,x2,y2; int gd,gm; clrscr; printf("\n\n\tEnter the co-ordinates of first point : "); scanf("%d %d",&x1,&y1); printf("\n\n\tEnter the co-ordinates of second point : "); scanf("%d %d",&x2,&y2); dx = (x2 - x1); dy = (y2 - y1); p = 2 * (dy) - (dx); x = x1; y = y1; detectgraph(&gd,&gm); initgraph(&gd,&gm,"e:\\tc\\bgi"); putpixel(x,y,WHITE); while(x <= x2) { if(p < 0) { x=x+1; y=y; p = p + 2 * (dy); } else { x=x+1; y=y+1; p = p + 2 * (dy - dx); } putpixel(x,y,WHITE); } getch; closegraph;
 * 1) include 
 * 2) include 
 * 3) include 

DDA::-- void lineDDA(int, int, int, int); void main { int x1, y1, xn, yn; int gd = DETECT, gm; initgraph(&gd, &gm, ""); printf("Enter the starting coordinates of line: "); scanf("%d %d", &x1, &y1); printf("Enter the ending coordinates of line: "); scanf("%d %d", &xn, &yn); lineDDA(x1, y1, xn, yn); getch; } void lineDDA(int x1, int y1, int xn, int yn) { int dx, dy, m, i; m = (yn-y1)/(xn-x1); for (i=x1; i<=xn; i++) { if (m <= 1) { dx = 1; dy = m * dx; } else { dy = 1; dx = dy / m; } x1 = x1 + dx; y1 = y1 + dy; putpixel(x1, y1, RED); delay(20); }
 * 1) include 
 * 2) include 
 * 3) include 

MID POINT ELLIPSE::--
 * 1) include
 * 2) include
 * 3) include

void ellipse(int xc,int yc,int rx,int ry) {  int gm=DETECT,gd; int x, y, p;  clrscr; initgraph(&gm,&gd,"C:\\TC\\BGI"); x=0; y=ry; p=(ry*ry)-(rx*rx*ry)+((rx*rx)/4); while((2*x*ry*ry)<(2*y*rx*rx)) {       putpixel(xc+x,yc-y,WHITE); putpixel(xc-x,yc+y,WHITE); putpixel(xc+x,yc+y,WHITE); putpixel(xc-x,yc-y,WHITE);

if(p<0) {    x=x+1; p=p+(2*ry*ry*x)+(ry*ry); }       else {    x=x+1; y=y-1; p=p+(2*ry*ry*x+ry*ry)-(2*rx*rx*y); }  }   p=((float)x+0.5)*((float)x+0.5)*ry*ry+(y-1)*(y-1)*rx*rx-rx*rx*ry*ry;

while(y>=0) {       putpixel(xc+x,yc-y,WHITE); putpixel(xc-x,yc+y,WHITE); putpixel(xc+x,yc+y,WHITE); putpixel(xc-x,yc-y,WHITE);

if(p>0) {    y=y-1; p=p-(2*rx*rx*y)+(rx*rx);

}       else {    y=y-1; x=x+1; p=p+(2*ry*ry*x)-(2*rx*rx*y)-(rx*rx); }  }   getch; closegraph; }

void main {  int xc,yc,rx,ry; clrscr; printf("Enter Xc="); scanf("%d",&xc); printf("Enter Yc="); scanf("%d",&yc); printf("Enter Rx="); scanf("%d",&rx); printf("Enter Ry="); scanf("%d",&ry); ellipse(xc,yc,rx,ry); getch; }

MIDPOINT CIRCLE::-- void pixel(int xc,int yc,int x,int y); void main {           int gd=DETECT,gm,xc,yc,r,x,y,Pk; clrscr; initgraph(&gd,&gm,"c:\\turboc3\\bgi "); printf("*** Mid-Point Subdivision algorithm of circle ***\n"); printf("Enter the value of Xc\t"); scanf("%d",&xc); printf("Enter the value of Yc \t"); scanf("%d",&yc); printf("Enter the Radius of circle\t"); scanf("%d",&r); x=0; y=r; Pk=1-r; pixel(xc,yc,x,y); while(x<y) {                       if(Pk<0) {                                   x=x+1; Pk=Pk+(2*x)+1; }                       else {                                   x=x+1; y=y-1; Pk=Pk+(2*x)-(2*y)+1; }                       pixel(xc,yc,x,y); }           getch; closegraph; } void pixel(int xc,int yc,int x,int y) { putpixel(xc+x,yc+y,7); putpixel(xc+y,yc+x,7); putpixel(xc-y,yc+x,7); putpixel(xc-x,yc+y,7); putpixel(xc-x,yc-y,7); putpixel(xc-y,yc-x,7); putpixel(xc+y,yc-x,7); putpixel(xc+x,yc-y,7); }
 * 1) include
 * 2) include
 * 3) include

DAA BINARY SEARCH:

int main {  int c, first, last, middle, n, search, array[100]; 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]); printf("Enter value to find\n"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2; while (first <= last) { if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d found at location %d.\n", search, middle+1); break; }     else last = middle - 1; middle = (first + last)/2; }  if (first > last) printf("Not found! %d is not present in the list.\n", search); return 0; COUNTING SORT:
 * 1) include 

void counting_sort(int A[], int k, int n) { int i, j;   int B[15], C[100]; for (i = 0; i <= k; i++) C[i] = 0; for (j = 1; j <= n; j++) C[A[j]] = C[A[j]] + 1; for (i = 1; i <= k; i++) C[i] = C[i] + C[i-1]; for (j = n; j >= 1; j--) {       B[C[A[j]]] = A[j]; C[A[j]] = C[A[j]] - 1; }   printf("The Sorted array is : "); for (i = 1; i <= n; i++) printf("%d ", B[i]); } int main {   int n, k = 0, A[15], i;    printf("Enter the number of input : "); scanf("%d", &n); printf("\nEnter the elements to be sorted :\n"); for (i = 1; i <= n; i++) {       scanf("%d", &A[i]); if (A[i] > k) { k = A[i]; }   }    counting_sort(A, k, n); printf("\n"); return 0;
 * 1) include 

LINEAR SEARCH:

int main {  int array[100], search, c, n;   printf("Enter the number of elements in array\n"); scanf("%d",&n); printf("Enter %d integer(s)\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to search\n"); scanf("%d", &search); for (c = 0; c < n; c++) {     if (array[c] == search)     /* if required element found */ {        printf("%d is present at location %d.\n", search, c+1); break; }  }   if (c == n)      printf("%d is not present in array.\n", search); return 0;
 * 1) include 

MAX HEAP:

void maxheap(int [],int,int);    //program for implementing heap sort in c void buildmaxheap(int a[],int n) { int i; for(i=n/2;i>=1;i--) { maxheap(a,i,n); } } void maxheap(int a[],int i,int n) { int R,L,largest,t; L=2*i; R=2*i+1; if((L<=n) && (a[L]>a[i])) largest=L; else largest=i; if((R<=n) && (a[i]>a[largest])) largest=R; if(largest!=i) { t=a[i]; a[i]=a[largest]; a[largest]=t; maxheap(a,largest,n); } } void heapsort(int a[],int n) { int i,temp; buildmaxheap(a,n); for(i=n;i>=2;i--) { temp=a[1]; a[1]=a[i]; a[i]=temp; maxheap(a,1,i-1); } } void main { int a[50],i,n; clrscr; printf("Enter the size of array : "); scanf("%d",&n); printf("Enter the elements of array \n"); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } heapsort(a,n); printf("sorted array is \n"); for(i=1;i<=n;i++) { printf("%d\t",a[i]); } getch;
 * 1) include
 * 2) include<conio.h>

MERGE SORT:

void mergesort(int a[],int i,int j); void merge(int a[],int i1,int j1,int i2,int j2); int main {   int a[30],n,i; printf("Enter no of elements:"); scanf("%d",&n); printf("Enter array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); mergesort(a,0,n-1); printf("\nSorted array is :"); for(i=0;i<n;i++) printf("%d ",a[i]); return 0; } void mergesort(int a[],int i,int j) { int mid; if(i<j) {       mid=(i+j)/2; mergesort(a,i,mid);       //left recursion mergesort(a,mid+1,j);   //right recursion merge(a,i,mid,mid+1,j);   //merging of two sorted sub-arrays } } void merge(int a[],int i1,int j1,int i2,int j2) {   int temp[50];    //array used for merging int i,j,k; i=i1;   //beginning of the first list j=i2;   //beginning of the second list k=0; while(i<=j1 && j<=j2)   //while elements in both lists {       if(a[i]<a[j]) temp[k++]=a[i++]; else temp[k++]=a[j++]; }   while(i<=j1)    //copy remaining elements of the first list temp[k++]=a[i++]; while(j<=j2)   //copy remaining elements of the second list temp[k++]=a[j++]; //Transfer elements from temp[] back to a[] for(i=i1,j=0;i<=j2;i++,j++) a[i]=temp[j]; }
 * 1) include<stdio.h>

QUICK SORT:


 * 1) include<stdio.h>

void quicksort(int [10],int,int);

int main{ int x[20],size,i;

printf("Enter size of the array: "); scanf("%d",&size);

printf("Enter %d elements: ",size); for(i=0;i<size;i++) scanf("%d",&x[i]);

quicksort(x,0,size-1);

printf("Sorted elements: "); for(i=0;i<size;i++) printf(" %d",x[i]);

return 0; }

void quicksort(int x[10],int first,int last){ int pivot,j,temp,i;

if(first<last){ pivot=first; i=first; j=last;

while(i<j){ while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j--; if(i<j){ temp=x[i]; x[i]=x[j]; x[j]=temp; }        }

temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last);

} }

Output: Enter size of the array: 5 Enter 5 elements: 3 8 0 1 2 Sorted elements: 0 1 2 3 8

PRIMS-

int a,b,u,v,n,i,j,ne=1; int visited[10]={0},min,mincost=0,cost[10][10]; void main { clrscr; printf("\n Enter the number of nodes:"); scanf("%d",&n); printf("\n Enter the adjacency matrix:\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) {  scanf("%d",&cost[i][j]); if(cost[i][j]==0) cost[i][j]=999; } visited[1]=1; printf("\n"); while(ne<n) { for(i=1,min=999;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]<min) if(visited[i]!=0) {     min=cost[i][j]; a=u=i; b=v=j; } if(visited[u]==0 || visited[v]==0) {  printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min); mincost+=min; visited[b]=1; } cost[a][b]=cost[b][a]=999; } printf("\n Minimun cost=%d",mincost); getch; }
 * 1) include<stdio.h>
 * 2) include<conio.h>

KRUSKALS-

int i,j,k,a,b,u,v,n,ne=1; int min,mincost=0,cost[9][9],parent[9]; int find(int); int uni(int,int); void main { clrscr; printf("\n\n\tImplementation of Kruskal's algorithm\n\n"); printf("\nEnter the no. of vertices\n"); scanf("%d",&n); printf("\nEnter the cost adjacency matrix\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) {  scanf("%d",&cost[i][j]); if(cost[i][j]==0) cost[i][j]=999; } } printf("\nThe edges of Minimum Cost Spanning Tree are\n\n"); while(ne<n) { for(i=1,min=999;i<=n;i++) {  for(j=1;j<=n;j++) {   if(cost[i][j]<min) {    min=cost[i][j]; a=u=i; b=v=j; }  }  }  u=find(u); v=find(v); if(uni(u,v)) {  printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min); mincost +=min; } cost[a][b]=cost[b][a]=999; } printf("\n\tMinimum cost = %d\n",mincost); getch; } int find(int i) { while(parent[i]) i=parent[i]; return i; } int uni(int i,int j) { if(i!=j) { parent[j]=i; return 1; } return 0; }
 * 1) include<stdio.h>
 * 2) include<conio.h>
 * 3) include<stdlib.h>