Square to Trapezium

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<iostream.h>

struct vertex
{
int x;
int y;
}v1,v2,v3,v4;
float count;
void plot(vertex a,vertex b,vertex c, vertex d,int p)
{ if(p==0)
{
setcolor(5);
}
else{
setcolor(6);
}
/* draw a diagonal line */
line(a.x+150, a.y+150, b.x+150, b.y+150);
line(b.x+150, b.y+150, c.x+150, c.y+150);
line(c.x+150, c.y+150, d.x+150, d.y+150);
line(d.x+150, d.y+150, a.x+150 ,a.y+150);
/* clean up */
cout<<“/n”;
}
void scale(int p,float &count)
{
int flag=0;
float cdec;
vertex v,w,a1,a2;
int inc,dec;
v=v2;
w=v3;
//cout<<“flag”<<flag;
if(p==1)
{
count=count+0.05;
cout<<“count is”<<count;
v2.x=v3.x=v.x;
//cout<<“w.y is”<<w.y<<” count is”<<count<<“v1.y is”<<v1.y;
v3.y=w.y*count+(1-count)*v1.y;
//cout<<“y coordinate of third vertex is”<<v3.y;
inc=v3.y-w.y;
v2.y=v.y-inc;
//cout<<“y coordinate of 2nd vertex is”<<v2.y;
//cout<<“\n inc is”<<inc;

//cout<<“a1 “<<a1.x<<” “<<a1.y<<” a2″<<a2.x<<” “<<a2.y;
//cout<<“v “<<v2.x<<” “<<v2.y<<“w”<<v3.x<<” “<<v3.y;
}
if(p==0)
{v=v2;
w=v3;
cdec=1/count;
//cout<<” “<<cdec;
v2.x=v3.x=v.x;
v3.y=w.y*cdec+(1-cdec)*v1.y;
//cout<<” a1 calculated “<<a1.y;
dec=w.y-v3.y;
//cout<<“dec is”<<dec;
v2.y=v.y+dec;

count=count-0.05;
//cout<<“” <<a1.x <<“\t”<<a1.y<<” “<<a2.x<<” “<<a2.x<<” “<<a2.y;
//cout<<“\n values of v and w”;
//cout<<v.x<<” “<<v.y<<” “<<w.x<<” “<<w.y;
}
plot(v1,v2,v3,v4,p);
}
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, “”);

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf(“Graphics error: %s\n”, grapherrormsg(errorcode));
printf(“Press any key to halt:”);
getch();
exit(1);
}
cout<<“enter 1 vertex x coordinate”;
cin>>v1.x;
cout<<“enter y”;
cin>>v1.y;
v2.x=v1.x+100;
v2.y=v1.y;
v3.x=v1.x+100;
v3.y=v1.y+100;
v4.x=v1.x;
v4.y=v1.y+100;

/* draw a diagonal line */
line(v1.x, v1.y, v2.x, v2.y);
line(v2.x, v2.y, v3.x, v3.y);
line(v3.x, v3.y, v4.x, v4.y);
line(v4.x, v4.y, v1.x,v1.y);
/* clean up */

char choice;
int flag2=0;
count=1.0;
do
{
if(flag2==0)
{
cout<<“enter w for increase”;
cout<<“\n enter s for decrease”;
cout<<“\n enter e for exit”;
}
flag2++;
choice=getch();
switch(choice)
{
case ‘w’: scale(1,count);
break;
case ‘s’: scale(0,count);
break;
}
}
while(choice!=’e’);

getch();
closegraph();
return 0;
}

B-Spline Curve

#include <graphics.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <dos.h>

int main(void)
{

int gdriver = DETECT, gmode, errorcode;

float t,i,y,x,j,k;
int cx[20],cy[20],n;

/* initialize graphics and local
variables */
initgraph(&gdriver, &gmode, “c:\\tc\\bgi”);
line(320,0,320,480);
line(0,240,640,240);
cout<<“enter the no of pts”;
cin>>n;
for(i=1;i<n-1;i++)
{
cout<<“enter the control pts “;
cin>>cx[i]>>cy[i];
}
cout<<“enter the start and end point x,y c ordinates”;
cin>>cx[0]>>cy[0]>>cx[n-1]>>cy[n-1];
for(i=3;i<=n-1;i++)
{
for(t=0;t<=1;t+=.0005)
{
x=((pow((1-t),3))*cx[i-3]+(3*t*t*t-6*t*t+4)*cx[i-2]+(-3*t*t*t+3*t*t+3*t+1)*cx[i-1]+t*t*t*cx[i])/6;
y=((pow((1-t),3))*cy[i-3]+(3*t*t*t-6*t*t+4)*cy[i-2]+(-3*t*t*t+3*t*t+3*t+1)*cy[i-1]+t*t*t*cy[i])/6;
putpixel(320+x,240-y,RED);
delay(1);
}
}
putpixel(320+cx[0],240-cy[0],WHITE);
putpixel(320+cx[n-1],240-cy[n-1],WHITE);
getch();
}

Plotting Circle

#include<dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<iostream.h>

void plotcircle(int x,int y,int x1,int y1, int n)
{
delay(50);
putpixel(x1+x+320,240+y1+y,4);
putpixel(x1-x+320,240+y1-y,4);
putpixel(x1+x+320,240+y1-y,4);
putpixel(x1-x+320,240+y1+y,4);
putpixel(x1-y+320,240+y1+x,4);
putpixel(x1-y+320,240+y1-x,4);
putpixel(x1+y+320,240+y1-x,4);
putpixel(x1+y+320,240+y1+x,4);
}

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, “”);

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf(“Graphics error: %s\n”, grapherrormsg(errorcode));
printf(“Press any key to halt:”);
getch();
exit(1);
}
int x1,y1,x,y,r,d;
cout<<“enter coordinates of centre”;
cin>>x1>>y1;
cout<<“\n enter radius”;
cin>>r;
d=1-r;
x=0;
y=r;
while(x<=y)
{
if(d<0)
{
d=d+2*x+3;
x++;
}
else
{
d=d+2*x-2*y+5;
x++;
y–;
}
plotcircle(x,y,x1,y1,5);
}
/* clean up */
getch();
closegraph();
return 0;
}

DDA line drawing Algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<iostream.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, “”);

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf(“Graphics error: %s\n”, grapherrormsg(errorcode));
printf(“Press any key to halt:”);
getch();
exit(1);
}
int x1,x2,y1,y2;
cout<<“enter start points”;
cin>>x1>>y1;
cout<<“enter end points”;
cin>>x2>>y2;
int dx,dy;
dx=abs(x2-x1);
dy=abs(y2-y1);
float inc,x,y;
if(dx>dy)
{
x=x1;
y=y1;
inc=1.0*dx/dy;
while(x2>x)
{
putpixel(x,y,4);
x=x+1;
y=y+inc;
}
}
/*
if(dx>dy)
{
x=x1;
y=y1;
inc=dx/dy;
while(x2>x)
{
putpixel(x,y,2);
x=x+1;
y=y+inc;
}
}*/
if(dx<=dy)
{
x=x1;
y=y1;
inc=1.0*dx/dy;
while(x2>x)
{
putpixel(x,y,4);
x=x+inc;
y=y+1;
}
}
/* clean up */
getch();
closegraph();
return 0;
}

 

 

 

FIFO-Disk Scheduling

#include<iostream.h>
#include<conio.h>
void sort(int d[],int n)
{
int temp,small,pos;
for(int i=0;i<n-1;i++)
{
small=d[i];
pos=i;
for(int j=i+1;j<n;j++)
{
if(small>d[j])
{
small=d[j];
pos=j;
}
}
temp=d[pos];
d[pos]=d[i];
d[i]=temp;
}
}
void main()
{
clrscr();
int head,d[20],n;
cout<<” enter number of disk schedulings you want to do”;
cin>>n;
cout<<“enter head”;
cin>>head;
for(int i=0;i<n;i++)
{
cout<<“\n enter”;
cin>>d[i];
}
sort(d,n);
cout<<“order is”;
for(int j=0;j<n;j++)
{
cout<<“\n”<<d[j];
}
getch();
}

 

 

First Program

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr;
cout<<“this is my first program”;
getch();
}

Ellipse with Rotating Circle

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 0, endangle = 360;
int xradius = 100, yradius = 50;

/* initialize graphics, local variables */
initgraph(&gdriver, &gmode, “”);

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)
/* an error occurred */
{
printf(“Graphics error: %s\n”, grapherrormsg(errorcode));
printf(“Press any key to halt:”);
getch();
exit(1);
/* terminate with an error code */
}

 

/* draw ellipse */
ellipse(100,100, stangle, endangle,70,90 );
//putpixel(100,0,3);
//cleardevice();
//putpixel(170,0,3);
circle(180,0,10);
circle(100,190,10);
circle(100,0,10);
float x1,y1,x,y,a,b,fx,fy;
x1=100;
y1=100;
a=80;
b=100;
float d;
x=10;
y=b;

d = a*a*(0.25-b) + b*b;

while((b*b*(x+1))<=(a*a*(y-0.5)))
{
if(d<0)
{
d+= 1.0*b*b*(3+2*x);
x++;
}
else
{
d+= b*b*(3+2*x)*1.0+2*1.0*a*a*(1-y);
x++;
y–;
}
ellipse(100,100, stangle, endangle,70,90 );
circle(x+100,y+100,10);
delay(200);
cleardevice();
}

d = b*b*(1.0*x+0.5)*(1.0*x+0.5)+1.0*a*a*(y-1)*(y-1)-1.0*a*a*b*b;

while(y>=0)
{
if(d<0)
{
d+= 2.0*b*b*(1+x)+1.0*a*a*(3-2*y);
x++;
y–;
}
else
{
d+= a*a*(3-2*y)*1.0;
y–;
}
ellipse(100,100, stangle, endangle,70,90 );
circle(x+100,y+100,10);
delay(200);
cleardevice();
}
x=10;
y=b;

d = a*a*(0.25-b) + b*b;

while((b*b*(x+1))<=(a*a*(y-0.5)))
{
if(d<0)
{
d+= 1.0*b*b*(3+2*x);
x++;
}
else
{
d+= b*b*(3+2*x)*1.0+2*1.0*a*a*(1-y);
x++;
y–;
}
ellipse(100,100, stangle, endangle,70,90 );
circle(100-x,100-y,10);
delay(200);
cleardevice();
}

d = b*b*(1.0*x+0.5)*(1.0*x+0.5)+1.0*a*a*(y-1)*(y-1)-1.0*a*a*b*b;

while(y>=0)
{
if(d<0)
{
d+= 2.0*b*b*(1+x)+1.0*a*a*(3-2*y);
x++;
y–;
}
else
{
d+= a*a*(3-2*y)*1.0;
y–;
}
ellipse(100,100, stangle, endangle,70,90 );
circle(100-x,100-y,10);
delay(200);
cleardevice();
}

cleardevice();
ellipse(100,100,stangle,endangle,80,100);
/* delay(1000);
for(int i=0;i<100;i=i+10)
{ ellipse(100,100, stangle, endangle,70,90 );
circle(100+i,0+i,10);
delay(200);
cleardevice();
}
/* clean up */
getch();
closegraph();
return 0;
}

Ellipse- Computer Graphics

#include<iostream.h>
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>

void plotpixel(int x, int y)
{
// delay(10);
putpixel((x+320),(240-y),15);
putpixel((-x+320),(240-y),15);
putpixel((x+320),(240+y),15);
putpixel((-x+320),(240+y),15);

}

void ell(float a, float b)
{
float x=0,y=b;

float d = a*a*(0.25-b) + b*b;

setcolor(RED);
line(320,0,320,480);
setcolor(BLUE);
line(0,240,640,240);
setcolor(WHITE);

plotpixel(x,y);

while((b*b*(x+1))<=(a*a*(y-0.5)))
{
if(d<0)
{
d+= 1.0*b*b*(3+2*x);
x++;
}
else
{
d+= b*b*(3+2*x)*1.0+2*1.0*a*a*(1-y);
x++;
y–;
}
plotpixel(x,y);
}

d = b*b*(1.0*x+0.5)*(1.0*x+0.5)+1.0*a*a*(y-1)*(y-1)-1.0*a*a*b*b;

while(y>=0)
{
if(d<0)
{
d+= 2.0*b*b*(1+x)+1.0*a*a*(3-2*y);
x++;
y–;
}
else
{
d+= a*a*(3-2*y)*1.0;
y–;
}
plotpixel(x,y);
}

}

void main()
{
clrscr();

float a,b;
cout<<“Enter a & b : “;
cin>>a>>b;

int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, “c:\\tc\\bgi”);

ell(a,b);
getch();
closegraph();
}

FIFO Page Replacement Algorithm

#include<iostream.h>
#include<conio.h>
struct process
{
int page;
int value;
}stack[3];
process least()
{
process small;
small=stack[0];
for(int i=0;i<3;i++)
{
if(small.value>stack[i].value)
{
small=stack[i];
}
}
return small;
}
void inc(int p,int c)
{
for(int i=0;i<3;i++)
{
if(stack[i].page==p)
{
stack[i].value=c;
}
}
}
int compare(int p)
{
int flag=0;
for(int i=0;i<3;i++)
{
if(stack[i].page==p)
{
flag=1;
break;
}
}
if(flag==1)
{
return 1;
}
else
{
return 0;
}
}
void replace(process a,int p,int c)
{
process cmp;
for(int i=0;i<3;i++)
{
if(a.page==stack[i].page)
{
process b;
b.value=c;
b.page=p;
stack[i]=b;
}
}
}
void display()
{
cout<<“\n stack[0]”<<stack[0].page;
cout<<“\n stack[1]”<<stack[1].page;
cout<<“\n stack[2]”<<stack[2].page;
getch();
}
void main()
{
clrscr();
int p[30];
int n;
cout<<“enter n”;
cin>>n;
cout<<“enter “;
for(int i=0;i<n;i++)
{
cin>>p[i];
cout<<“enter”;
}
clrscr();
int pgf[20];
stack[0].page=p[0];
stack[0].value=0;
stack[1].page=p[1];
stack[1].value=1;
stack[2].page=p[2];
stack[2].value=2;
pgf[0]=p[0];
pgf[1]=p[1];
pgf[2]=p[2];
int count=3;
process ans2;
int temp=3,ans1;
while(count<n)
{ //cout<<“\n count is”<<count;
ans1=compare(p[count]);
/*if(count==10)
{cout<<“temp is”<<temp<<“\t value of page”<<p[count];}*/
if(ans1==1)
{
//cout<<“\n same number “;
inc(p[count],count);
}
if(ans1==0)
{
//cout<<“\n no match with selected”;
ans2=least();
replace(ans2,p[count],count);
pgf[temp]=p[count];
temp++;
//cout<<“\n value of temp”<<temp;
}
count++;
display();
}
cout<<” page faults are”;
for(int j=0;j<=temp;j++)
{
cout<<“\n “<<pgf[j];
}
getch();
}

 

 

Graphs-Menu Driven Program

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct adjnode
{
char dest;
adjnode *link;
};
struct node
{
char data;
node *next;
adjnode *adj;
};

node *first,*last,*start;

void enteradj(node* ptr)
{ int nadj;
cout<<” no of adjacent node present??”;
cin>>nadj;

if(nadj>0)
{
adjnode *beg,*end;
beg=new(adjnode);
ptr->adj=beg;
cout<<“enter the first adjacent member/destination”;
cin>>beg->dest;
beg->link=NULL;
for(int a=1;a<nadj;a++)
{
end=new(adjnode);
cout<<“enter destination”;
cin>>end->dest;
fflush(stdin);
end->link=NULL;
beg->link=end;
beg=end;
}
}
}
void findnode()
{ int flag=0;
char ch;
node *ptr;
ptr=start;
cout<<“enter the node to be found”;
cin>>ch;
fflush(stdin);
while(ptr!=NULL)
{
if(ch==ptr->data)
{
flag=1;
break;
}
ptr=ptr->next;
}
if(flag==1)
{
cout<<“yes the item is present”;
}
else{
cout<<“unsuccessful search”;
}
}

void insertnode()
{
node *ptr;
ptr=new(node);
cout<<“enter data”;
cin>>ptr->data;
ptr->next=NULL;
last->next=ptr;
last=ptr;
enteradj(ptr);
}
void deletenode()
{ node *ptr,*save;
char dlt;
int flag=0;
cout<<“enter data to be deleted”;
cin>>dlt;
ptr=start;
if(dlt==ptr->data)
{
start=start->next;
delete ptr;
}
else
{ptr=start->next;
save=start;
while(ptr!=NULL)
{
if(ptr->data==dlt)
{flag=1;
save->next=ptr->next;
delete ptr;
}
else
{
save=ptr;
ptr=ptr->next;
}
}
}
if(flag==0)
{
cout<<“unsuccessful”;
}
}
int main()
{
int n;
cout<<“enter n”;
cin>>n;
first=new(node);
cout<<“enter the first data”;
cin>>first->data;
first->next=NULL;
start=first;
fflush(stdin);
enteradj(first);

for(int b=1;b<n;b++)
{
last=new(node);
cout<<“enter the data of node”;
cin>>last->data;
fflush(stdin);
enteradj(last);
last->next=NULL;
first->next=last;
first=last;
}
int choice;

do
{
cout<<” enter 1 to insert a node”;
cout<<“\n 2 to delete a node containing particular data”;
cout<<“\n 3 to find a node”;
cout<<“\n 4 to exit”;
cin>>choice;
switch(choice)
{
case 1: insertnode();
break;
case 2: deletenode();
break;
case 3: findnode();
break;
}
}while(choice!=4);
getch();
return 0;
}

Calendar

July 2012
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories

Archives