Posts

Showing posts from April 17, 2011

SCALING IN RECTANGLE

#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm; int x=20,y=20,a=50,b=50; initgraph(&gd,&gm,"c:\\tc\\bin"); while(!kbhit()) { for(i=3;i<10;i+=2) { setcolor(i); rectangle(x*i,y*i,a*i,b*i); } } getch(); }

PROGRAM FOR ROTATION IN COMPUTER GRAPHICS

//CONTRIBUTED BY PRAMOD SINGH ROTATION PROGRAM #include<conio.h> #include<iostream.h> #include<graphics.h> #include<math.h> #include<dos.h> void main() { int x1=100,x2=100,y1=100,y2=300,x12,y12,x22,y22; float a=1.57; int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); setcolor(5); line(x1,y1,x2,y2); while(!kbhit()) { for(float i=0;i<628;i+=0.1) { setcolor(5); x12=x1*cos(i)-y1*sin(i); x22=x2*cos(i)-y2*sin(i); y12=x1*sin(i)+y1*cos(i); y22=x2*sin(i)+y2*cos(i); line(x12,y12,x22,y22); delay(200); }} getch(); }

program of movement of rectangle(translation)

//pramod singh bisht #include<stdio.h> #include<conio.h> #include<graphics.h> void main() {  int gd=DETECT,gm;  initgraph(&gd,&gm,"c:\\tc\\bgi");  setcolor(2);  rectangle(20,20,50,50);  while(!kbhit())  {  for(i=1;i<100,i=i+35)  {  setcolor(4);  rectangle(x+i,y+i,a+i,b+i);  delay(100);  setcolor(6);  rectangle(x+i,y+i,a+i,b+i);  delay(100);  }}  getch();  closegraph(); }

computer graphics program of clock

// pramod singh bisht #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> void main() {  int gd=DETECT,gm;  initgraph(&gd,&gm,"c:\\tc\\bgi");  double s,m,n;  int a,b,c,d,e,f;  outtextxy(311,22,"XII");  outtextxy(313,450,"VI");  outtextxy(517,238,"III");  outtextxy(102,230,"IX");  setcolor(6);  circle(320,240,220);  for(s=0;m=3*M_PI-2,kbhit();s+M_PI/30,m+=M_PI/1800)  {  setcolor(5);  a=320+155*cos(5);  b=240+155*sin(5);  line(320,240,a,b);  setcolor(7);  c=320+192*cos(m);  d=240+192*sin(m);  line(320,240,c,d);  setcolor(8);  e=320+120*cos(n);  f=240+120*sin(n);  line(320,240,e,f);  delay(100);  setcolor(0);  line(320,240,a,b);  line(320,240,c,d);  line(320,240,e,f);  } getch();  closegraph();  }

PROGRAM FOR KRUSHKALS ALGORITHM

program for krushkals algorithm itprofessionalsrocks.blogspot.com #include<iostream.h> #include<conio.h> #define infinity 1000 int num_of_nodes; int cost[10][10]; int parent[10]; int solution[10][2]; int min_edge(int *,int *) ; int find(int); void unite(int,int); void main() { int i=0; int j=0; int k=0; int u=0; int v=0; int min_cost=0; int cost_of_uv=0; clrscr(); cout<<"\n enter the number of nodes in the graph:"; cin>>num_of_nodes; cout<<"\nn!!!!!!enter 1000 for a not conncted link!!!\n"; for(i=0;i<num_of_nodes;i++) { cost[i][j]=0; parent[i]=-1; for(j=(i+1);j<num_of_nodes;j++) { cout<<"\n enter the cost from node"<<i+1<<"to node"<<j+1<<""; cin>>cost[i][j] ; if(cost[i][j]>=infinity) cost[i][j]=infinity; } } i=0; while((i<num_of_nodes-1)&&(cost_of_uv!=infinity)) { cost_of_uv=min_edge(&u,&v); j=find(u);...

PROGRAM TO IMPLEMENT DEPTH FIRST SEARCH

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> #define max 100 void dfs(int n); void dfs_visit(int u); int color[max]; int d[max]; int f[max]; int time1; typedef struct vert* ver; struct vert { int ident; ver next; }cor; ver adj_list[max]; void main() { int n,i,j,k,s,u; ver temp,prev; clrscr(); printf("Enter no. of nodes"); scanf("%d",&n); for(i=0;i<=n;i++) adj_list[i]='\0' ; for(i=1;i<=n;i++) { prev=NULL; printf("enter no. of vertex connected to %d",i); scanf("%d",&j); for(k=0;k<j;k++) { temp=(ver)malloc(sizeof(cor)); temp->next=NULL; printf("Enter vertex no. connected to vertex %d",i); scanf("%d",&temp->ident); if(prev==NULL) adj_list[i]=temp; else prev->next=temp; prev=temp; } } dfs(n); for(i=1;i<=n;i++) { printf("\n starting/ending time of vertex %d is %d/%d",i,d[i],f[i]); ...

PROGRAM TO IMPLEMENT BREADTH-FIRST-SEARCH

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> #define max 100 void bfs(int n,int s); void enqueue(int q[],int m); int dequeue(int q[]); int color[max]; int d[max]; int pi[max]; typedef struct vert* ver; struct vert { int ident; ver next; }cor; ver adj_list[max]; void main() { int n,i,j,k,s,u; ver temp,prev; clrscr(); printf("Enter no. of nodes"); scanf("%d",&n); for(i=0;i<=n;i++) adj_list[i]='\0' ; for(i=1;i<=n;i++) { prev=NULL; printf("enter no. of vertex connected to %d",i); scanf("%d",&j); for(k=0;k<j;k++) { temp=(ver)malloc(sizeof(cor)); temp->next=NULL; printf("Enter vertex no. connected to vertex %d",i); scanf("%d",&temp->ident); if(prev==NULL) adj_list[i]=temp; else prev->next=temp; prev=temp; } } printf("\n enter the strating point of graph"); scanf("%d",&s); b...

PROGRAM FOR TASK SCHEDULING PROBLEM

#include<iostream.h> #include<conio.h> struct task { int deadl; int cost; }task[20],temp; void main() { int i,j,n,p[20],count=0,c=0,ans[20],ex[10],penalty=0,temp1=1,x=0; clrscr(); cout<<"enter the number of tasks"; cin>>n; cout<<"\n enter deadline and cost of each task\n"; cout<<"dead line   cost\n"; for(i=0;i<n;i++) { cin>>task[i].deadl>>task[i].cost; p[i]=-1; } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(task[i].cost<task[j].cost) { temp=task[i]; task[i]=task[j]; task[j]=temp; } } } for(i=0;i<n;i++) { if(p[task[i].deadl]==-1) { p[task[i].deadl]=0; ans[count++]=i; } else { c=task[i].deadl; temp1=1; while(c>0) { if(p[c]==-1) { temp1=0; p[c]=0; ans[count++]=i; break; } c--; } if(temp1==1) { ex[x++]=i; penalty=penalty+task[i].cost; } } } for(i=0;i<count;i++) { for(j=i+1;j<count;j++) { if(task[ans[i]].deadl>=task[ans[j]]....

PROGRAM FOR STRASSEN'S MATRIX MULTIPLICATION

#include<iostream.h> #include<conio.h> void main() { int a[2][2],b[2][2],c[2][2],i,j; int p1,p2,p3,p4,p5,p6,p7; clrscr(); cout<<"enter the first matrix"; for(i=1;i<=2;i++) for(j=1;j<=2;j++) cin>>a[i][j]; cout<<"enter the second matrix"; for(i=1;i<=2;i++) for(j=1;j<=2;j++) cin>>b[i][j]; p1=a[1][1]*(b[1][2]-b[2][2]); p2=(a[1][1]+a[1][2])*b[2][2]; p3=(a[2][1]+a[2][2])*b[1][1]; p4=a[2][2]*(b[2][1]-b[1][1]); p5=(a[1][1]+a[2][2])*(b[1][1]+b[2][2]) ; p6=(a[1][2]-a[2][2])*(b[2][1]+b[2][2]) ; p7=(a[1][1]-a[2][1])*(b[1][1]+b[1][2]) ; c[1][1]=p5+p4-p2+p6; c[1][2]=p1+p2; c[2][1]=p3+p4; c[2][2]=p5+p1-p3-p7; for(i=1;i<=2;i++) { for(j=1;j<=2;j++) { cout<<c[i][j]<<" "; } cout<<"\n" ; } getch(); }

MATRIX CHAIN MULTIPLICATION

#include<iostream.h> #include<conio.h> long int m[50][50],p[50],s[50][50] ; void display(long int s[50][50],int i,int j); void matrixchain(int); void matrixchain(int n) { long int i,j,k,l,q; for(i=1;i<=n;i++) { for(i=1;i<=n-1;i++) { j=i+l-1; m[i][j]=20000; for(k=i;k<j;k++) { q=(m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]); if(q<m[i][j]) { m[i][j]=q; s[i][j]=k; } } } } } void display(long int s[50][50],int i,int j) { if(i==j) cout<<"A"<<""; else { cout<<"("; display(s,i,s[i][j]) ; display(s,s[i][j]+1,j); cout<<")"; } } void main() { clrscr(); long int n,i,j; clrscr(); cout<<"enter the no. of matrices"; cin>>n; cout<<"enter the order array"; for(i=0;i<=n;i++) { cin>>p[i]; } matrixchain(n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cout<<m[i][j]<<""; } cout<<"\n" ; ...

LONGEST COMMON SUBSEQUENCE

#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> #define max 12 void lcs_length(char[],char[],int,int); void print_lcs(char[max][max],char[],int,int); void main() { char x[10],y[10],ans ; int i,j; clrscr(); cout<<"\n Enter the first sequence(less than or equal to 10 characters"; gets(x); i=strlen(x); cout<<"\n enter the second sequence"; gets(y); j=strlen(y); lcs_length(x,y,i,j); getch(); } void lcs_length(char x[10],char y[10],int m,int n) { int i,j; int c[max][max]; char b[max][max]; for(i=0;i<max;i++) for(j=0;j<max;j++) b[i][j]='0'; for(i=1;i<=m;i++) c[i][0]=0; for(j=0;j<=n;j++) c[0][j]=0; for(i=0;i<m;i++) { for(j=0;j<=n;j++) { if(x[i]==y[j]) { c[i+1][j+1]=c[i][j]+1; b[i+1][j+1]='\\'; } else if(c[i][j+1]>=c[i+1][j]) { c[i+1][j+1]=c[i][j+1]; b[i+1][j+1]='|'; } else { c[i+1][j+1]=c[i+1][j]; b[i+1][j+1]='...

MERGESORT

#include<iostream.h> #include<conio.h> void mergesort(int [],int,int); void merge(int[],int,int,int); void main() { int a[10],p,q,r,i,n; clrscr(); cout<<"enter the number of elements"; cin>>n; p=0; r=n-1; cout<<"enter the array"; for(i=0;i<n;i++) { cin>>a[i]; } mergesort(a,p,r); cout<<"the sorted array is"; for(i=0;i<n;i++) { cout<<"\n"<<a[i]; } getch(); } void mergesort(int a[],int p,int r) { if(p<r) { int q=(p+r)/2; mergesort(a,p,q); mergesort(a,p+1,r) ; merge(a,p,q,r); } } void merge(int a[],int p,int q,int r) { int c[10]; int i=p; int j=q+1; int k=p; while((i<=q)&&(j<=r)) { if(a[i]<a[j]) { c[k] =a[i]; i=i+1; k=k+1; } else { c[k]=a[j]; j=j+1; k=k+1; } } while(i<=q) { c[k]=a[i]; i=i+1; k=k+1; } while(j<=r) { c[k]=a[j]; j=j+1; k=k+1; } int l=p; while(l<=r) { a[l]=c[l]; l=l+1; } }