Pages

Wednesday, December 17

Operating System Lab Programs using C++

This is a list of programs which used in the Operating Systems lab. The course is usually taught in the 4th/5th semester of engineering. All these programs are 100% working and are tested using the Dev C++ IDE. I urge you to first try these algorithms on your own. Try to implement them, as they are the very basic algorithms which you can easily implement. If you are not able to implement these algorithms, then use these programs as a reference. I encourage you to make these programs more effecient by making some here and there tweaks. This is another way of learning.

Thanks for reading it.

Round Robin Scheduling using C++:

This is a 100% working program with the output shown in the command window.

PROGRAM:

#include<iostream.h>
#include<conio.h>
int main(void){
        cout<<"Enter the number of processes:\n";
        int n;
        cin>>n;
        int arivl[n],burst[n],end[n],remain[n],x,y,temp,time,tq;
        for(x=0;x<n;x++){
            cout<<"Enter arrival & burst time for process "<<x<<"\n";
            cin>>arivl[x]>>burst[x];}
        cout<<"Enter the time quantum:\n";
        cin>>tq;
        for(x=0;x<n-1;x++)
            for(y=0;y<n-x-1;y++)
                if(arivl[y]>arivl[y+1]){
                    temp=arivl[y];
                    arivl[y]=arivl[y+1];
                    arivl[y+1]=temp;
                    temp=burst[y];
                    burst[y]=burst[y+1];
                    burst[y+1]=temp;
                }
        for(x=0;x<n;x++)
            remain[x]=burst[x];
        for(time=arivl[0],x=0;x==0;){
            for(y=0;y<n;y++)
                if(remain[y]!=0&&arivl[y]<=time)
                    if(remain[y]<=tq){
                        time+=remain[y];
                        remain[y]=0;
                        end[y]=time;
                    }
                    else{
                        time+=tq;
                        remain[y]-=tq;
                    }
            for(y=0,x=1;y<n;y++)
                if(remain[y]!=0)
                    x=0;
        }
        float aw=0,at=0;
        cout<<"Process\tArrival\tBurst\tWaiting\tTurnAround\n";
        for(x=0;x<n;x++){
            aw+=end[x]-arivl[x]-burst[x];
            at+=end[x]-arivl[x];
            cout<<(x+1)<<"\t"<<arivl[x]<<"\t"<<burst[x]<<"\t";
            cout<<(end[x]-arivl[x]-burst[x])<<"\t"<<(end[x]-arivl[x])<<"\n";
        }
        cout<<"\nAverage Waiting Time = "<<(aw/n);
        cout<<"\nAverage Turn Around Time = "<<(at/n);
        getch();
        return 0;
}

OUTPUT:

Round Robin Scheduling Output using C++



First Come First Serve OR FCFS Scheduling using C++:

This is also a 100% working program with the output shown in the command window.

PROGRAM:

#include<iostream.h>
#include<conio.h>
int main(){
        cout<<"Enter the number of processes:\n";
        int n;
        cin>>n;
        int arivl[n],burst[n],start[n],x,y,temp;
        for(x=0;x<n;x++){
            cout<<"Enter arrival & burst time for process "<<x<<"\n";
            cin>>arivl[x]>>burst[x];
        }
        for(x=0;x<n-1;x++)
            for(y=0;y<n-x-1;y++)
                if(arivl[y]>arivl[y+1]){
                    temp=arivl[y];
                    arivl[y]=arivl[y+1];
                    arivl[y+1]=temp;
                    temp=burst[y];
                    burst[y]=burst[y+1];
                    burst[y+1]=temp;
                }
        start[0]=arivl[0];
        for(x=1;x<n;x++){
            start[x]=start[x-1]+burst[x-1];
            if(start[x]<arivl[x])
                start[x]=arivl[x];
        }
        float aw=0,at=0;
        cout<<"Process\tArrival\tBurst\tStart\tWaiting\tTurnAround\n";
        for(x=0;x<n;x++){
            aw+=start[x]-arivl[x];
            at+=start[x]-arivl[x]+burst[x];
            cout<<(x+1)<<"\t"<<arivl[x]<<"\t"<<burst[x]<<"\t"<<start[x]<<"\t";
            cout<<(start[x]-arivl[x])<<"\t"<<(start[x]-arivl[x]+burst[x])<<"\n";
        }
        cout<<"\nAverage Waiting Time = "<<(aw/n);
        cout<<"\nAverage Turn Around Time = "<<(at/n);
        getch();
        return 0;
}

OUTPUT:


FCFS Scheduling using Dev C++


Shortest Remaining Time or SRT Scheduling using C++:

This is a 100% working program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
int main()
{
        cout<<"Enter the number of processes:\n";
        int n;
        cin>>n;
        int arivl[n],burst[n],end[n],remain[n],x,y,loc,temp,time,small;
        for(x=0;x<n;x++)
        {
            cout<<"Enter arrival & burst time for process "<<x<<"\n";
            cin>>arivl[x]>>burst[x];
        }
        for(x=0;x<n-1;x++)
            for(y=0;y<n-x-1;y++)
                if(arivl[y]>arivl[y+1])
                {
                    temp=arivl[y];
                    arivl[y]=arivl[y+1];
                    arivl[y+1]=temp;
                    temp=burst[y];
                    burst[y]=burst[y+1];
                    burst[y+1]=temp;
                }
        for(x=0;x<n;x++)
            remain[x]=burst[x];
        for(time=arivl[0],x=0;x==0;time++)
        {
            for(y=0,small=999,loc=-1;y<n;y++)
                if(arivl[y]<=time&&remain[y]!=0&&remain[y]<small)
                {
                    small=remain[y];
                    loc=y;
                }
            if(loc!=-1)
            {
                remain[loc]--;
                if(remain[loc]==0)
                    end[loc]=time+1;
            }
            for(y=0,x=1;y<n;y++)
                if(remain[y]!=0)
                    x=0;
        }
        float aw=0,at=0;
        cout<<"Process\tArrival\tBurst\tWaiting\tTurnAround\n";
        for(x=0;x<n;x++)
        {
            aw+=end[x]-arivl[x]-burst[x];
            at+=end[x]-arivl[x];
            cout<<(x+1)<<"\t"<<arivl[x]<<"\t"<<burst[x]<<"\t";
            cout<<(end[x]-arivl[x]-burst[x])<<"\t"<<(end[x]-arivl[x])<<"\n";
        }
        cout<<"\nAverage Waiting Time = "<<(aw/n);
        cout<<"\nAverage Turn Around Time = "<<(at/n);
        getch();
        return 0;
}

OUTPUT:


SRT Scheduling output shown using Dev C++


Shorest Job First or SJF Scheduling using C++: 

100% working program with the output below.

PROGRAM:

#include<iostream.h>
#include<conio.h>
int main()
{
        cout<<"Enter the number of processes:\n";
        int n;
        cin>>n;
        int arivl[n],burst[n],start[n],x,y,loc,temp,time,small;
        for(x=0;x<n;x++)
        {
            cout<<"Enter arrival & burst time for process "<<x<<"\n";
            cin>>arivl[x]>>burst[x];
            start[x]=-1;
        }
        for(x=0;x<n-1;x++)
            for(y=0;y<n-x-1;y++)
                if(arivl[y]>arivl[y+1])
                {
                    temp=arivl[y];
                    arivl[y]=arivl[y+1];
                    arivl[y+1]=temp;
                    temp=burst[y];
                    burst[y]=burst[y+1];
                    burst[y+1]=temp;
                }
        for(time=arivl[0],x=0;x==0;)
        {
            for(y=0,small=999,loc=-1;y<n;y++)
                if(arivl[y]<=time&&start[y]==-1&&burst[y]<small)
                {
                    small=burst[y];
                    loc=y;
                }
            if(loc==-1)
                time++;
            else
            {
                start[loc]=time;
                time+=burst[loc];
            }
            for(y=0,x=1;y<n;y++)
                if(start[y]==-1)
                    x=0;
        }
        float aw=0,at=0;
        cout<<"Process\tArrival\tBurst\tStart\tWaiting\tTurnAround\n";
        for(x=0;x<n;x++)
        {
            aw+=start[x]-arivl[x];
            at+=start[x]-arivl[x]+burst[x];
            cout<<(x+1)<<"\t"<<arivl[x]<<"\t"<<burst[x]<<"\t"<<start[x]<<"\t";
            cout<<(start[x]-arivl[x])<<"\t"<<(start[x]-arivl[x]+burst[x])<<"\n";
        }
        cout<<"\nAverage Waiting Time = "<<(aw/n);
        cout<<"\nAverage Turn Around Time = "<<(at/n);
        getch();
        return 0;
}

OUTPUT:

SJF Scheduling output shown using Dev C++


Banker's Algorithm using C++:

One of the most complicated of all programs. Rarely you'll find this program online working. But this algorithm is 100% working and is implemented using Dev C++.

PROGRAM:

#include<iostream>
using namespace std;
#include<conio.h>
#define MAX 20
class bankers
{private:
int al[MAX][MAX],m[MAX][MAX],n[MAX][MAX],avail[MAX];
int nop,nor,k,result[MAX],pnum,work[MAX],finish[MAX];
public:
bankers();
void input();
void method();
int search(int);
void display(); };
bankers::bankers(){
k=0;
for(int i=0;i<MAX;i++){
for(int j=0;j<MAX;j++){
al[i][j]=0;
m[i][j]=0;
n[i][j]=0;
}
avail[i]=0;
result[i]=0;
finish[i]=0; }}
void bankers::input(){
int i,j;
cout<<"Enter the number of processes:";
cin>>nop;
cout<<"Enter the number of resources:";
cin>>nor;
cout<<"Enter the allocated resources for each process: "<<endl;
for(i=0;i<nop;i++){
cout<<"\nProcess "<<i;
for(j=0;j<nor;j++){
cout<<"\nResource "<<j<<":";
cin>>al[i][j];  }}
cout<<"Enter the maximum resources that are needed for each process: "<<endl;
for(i=0;i<nop;i++){
cout<<"\nProcess "<<i;
for(j=0;j<nor;j++){
cout<<"\nResouce "<<j<<":";
cin>>m[i][j];
n[i][j]=m[i][j]-al[i][j];  }}
cout<<"Enter the currently available resources in the system: ";
for(j=0;j<nor;j++){
cout<<"Resource "<<j<<":";
cin>>avail[j];
work[j]=-1;  }
for(i=0;i<nop;i++)
finish[i]=0;
}
void bankers::method(){
int i=0,j,flag;
while(1){
if(finish[i]==0){
pnum =search(i);
if(pnum!=-1){
result[k++]=i;
finish[i]=1;
for(j=0;j<nor;j++){
avail[j]=avail[j]+al[i][j];  }}}
i++;
if(i==nop){
flag=0;
for(j=0;j<nor;j++)
if(avail[j]!=work[j])
flag=1;
for(j=0;j<nor;j++)
work[j]=avail[j];
if(flag==0)
break;
else
i=0;  }}}
int bankers::search(int i){
int j;
for(j=0;j<nor;j++)
if(n[i][j]>avail[j])
return -1;
return 0;  }
void bankers::display(){
int i,j;
cout<<endl<<"OUTPUT:";
cout<<endl<<"========";
cout<<endl<<"PROCESS\t     ALLOTED\t     MAXIMUM\t     NEED";
for(i=0;i<nop;i++){
cout<<"\nP"<<i+1<<"\t     ";
for(j=0;j<nor;j++){
cout<<al[i][j]<<"  "; }
cout<<"\t     ";
for (j=0;j<nor;j++){
cout<<m[i][j]<<"  "; }
cout<<"\t     ";
for(j=0;j<nor;j++ ){
cout<<n[i][j]<<"  ";  }}
cout<<"\nThe sequence of the safe processes are: \n";
for(i=0;i<k;i++){
int temp = result[i]+1 ;
cout<<"P"<<temp<<" "; }
cout<<"\nThe sequence of unsafe processes are: \n";
int flg=0;
for (i=0;i<nop;i++){
if(finish[i]==0){
flg=1; }
cout<<"P"<<i<<" "; }
cout<<endl<<"RESULT:";
cout<<endl<<"=======";
if(flg==1)
cout<<endl<<"The system is not in safe state and deadlock may occur!!";
else
cout<<endl<<"The system is in safe state and deadlock will not occur!!"; }
int main(){
cout<<" DEADLOCK BANKER’S ALGORITHM "<<endl;
bankers B;
B.input ( );
B.method ( );
B.display ( );
getch ( );
}

OUTPUT:



Banker's Algorithm output shown using Dev C++ 


Least Frequently Used or LFU Page Replacement Algorithm using C++:

100% working program but wasn't able to take a screenshot of the output.

PROGRAM:

#include<iostream.h>
#include<conio.h>
int main()
{
    int n,a,b,c,x,y;
    cout<<"Enter the capacity of cache:\n";
    cin>>n;
    int cac[n],lis[n],fre[n];
    cout<<"Enter initial cache pages:\n";
    for(b=0;b<n;b++)
    {
        cin>>cac[b];
        lis[b]=cac[b];
    }
    cout<<"Enter the number of new entries:\n";
    cin>>a;
    int ent[a];
    cout<<"Enter new entries of pages:\n";
    for(b=0;b<a;b++)
        cin>>ent[b];
    for(b=0;b<n;b++)
        fre[b]=1;
    cout<<"\nInitial cache and LFU list:\n";
    for(b=0;b<n;b++)
        cout<<cac[b]<<"  ";
    cout<<"\t";
    for(b=0;b<n;b++)
        cout<<lis[b]<<"  ";
    cout<<"\n";
    for(b=0;b<a;b++)
    {
        for(x=0,c=0,y=0;x<n;x++)
            if(lis[x]==ent[b])
            {
                c=1;
                y=x;
            }
        if(c!=0)
            fre[y]++;
        else
        {
            for(x=0;x<n;x++)
                if(cac[x]==lis[0])
                    cac[x]=ent[b];
            for(x=0;x<n-1;x++)
            {
                lis[x]=lis[x+1];
                fre[x]=fre[x+1];
            }
            lis[x]=ent[b];
            fre[x]=1;
        }
        for(x=0;x<n;x++)
            for(y=0;y<n-x-1;y++)
                if(fre[y]>fre[y+1])
                {
                    c=fre[y];
                    fre[y]=fre[y+1];
                    fre[y+1]=c;
                    c=lis[y];
                    lis[y]=lis[y+1];
                    lis[y+1]=c;
                }
        cout<<"\nCache and LFU list at entry "<<ent[b]<<":\n";
        for(x=0;x<n;x++)
            cout<<cac[x]<<"  ";
        cout<<"\t";
        for(x=0;x<n;x++)
            cout<<lis[x]<<"  ";
        cout<<"\n";
    }
    getch();
    return 0;
}

Tuesday, December 16

Computer Graphic Programs using Dev C++

Understanding an algorithm and implementing an algorithm are two different things. Some of the students find it difficult to do that. For those students, I have created this comprehensive list of programs which can be used in Computer Graphics Lab. I urge you to first try them out on your own. And if you are not able to implement them, only then use these programs as your backup.
Thanks for reading it and follow me on Google+ for all future updates.

FloodFill Line Drawing Algorithm using Dev C++:

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

void FloodFill(int,int,int,int);
void MidPoint(int);
void main(){
int xCenter=320;
int yCenter=240;
int gDriver = DETECT, gMode, errorcode;
initgraph(&gDriver, &gMode, "c:\\tc\\bgi");
cleardevice();
MidPoint(49);
FloodFill(xCenter+1,yCenter+1,0,8);
getch();
return;}

void MidPoint(int Radius){
int iCntr, x, y,p0;
x=0;
y=Radius;
int xCenter=320;
int yCenter=240;
p0=(5/4)-Radius;
putpixel(xCenter+x,yCenter+y,15);
putpixel(xCenter-x,yCenter+y,15);
putpixel(xCenter+x,yCenter-y,10);
putpixel(xCenter-x,yCenter-y,10);
putpixel(xCenter+y,yCenter+x,12);
putpixel(xCenter-y,yCenter+x,12);
putpixel(xCenter+y,yCenter-x,9);
putpixel(xCenter-y,yCenter-x,9);
while(x<=y){
if(p0 < 0){
p0=p0 + 2*(x+1) + 1;
x=x+1;}
else{
p0=p0 + 2*(x+1) + 1 - 2*(y-1);
x=x+1;
y=y-1;}
putpixel(xCenter+x,yCenter+y,15);
putpixel(xCenter-x,yCenter+y,15);
putpixel(xCenter+x,yCenter-y,10);
putpixel(xCenter-x,yCenter-y,10);
putpixel(xCenter+y,yCenter+x,12);
putpixel(xCenter-y,yCenter+x,12);
putpixel(xCenter+y,yCenter-x,9);
putpixel(xCenter-y,yCenter-x,9);}}

void FloodFill(int pointx,int pointy,int OldColor,int NewColor){
int Intensity=getpixel(pointx,pointy);
if(Intensity==OldColor){
putpixel(pointx,pointy,NewColor);
FloodFill(pointx+1,pointy,OldColor,NewColor);
FloodFill(pointx-1,pointy,OldColor,NewColor);
FloodFill(pointx,pointy+1,OldColor,NewColor);
FloodFill(pointx,pointy-1,OldColor,NewColor); }}


Scanfill Algorithm using C++:

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

struct Polygon{
int x;
int y;};

struct edRecord{
int YUpper;
float XIntersect;
float dxperscan;
struct edRecord *Next; };

typedef struct edRecord Edge;
typedef struct Polygon Nodes;
Edge  *Active;
Edge **Edges;
int Cntr=6;
Nodes NodesObj[6]={{100,100},{150,150},{150,200},{100,250},{50,200},{50,150}};

void Scanfill();
void InsertEdge(Edge*,Edge*);
void BuildEdgeList(int,Nodes*);
int YNext(int);
void MakeEdgeRec(Nodes,Nodes,int,Edge*);
void BulidActiveList(int);
void UpdateActiveList(int);
void DeleteAfter(Edge*);
void ResortActiveList(void);
void ResortActiveList(int);

void main(){
int gDriver = DETECT, gMode;
int n,xArray[12];
int iCntr,jCntr;

initgraph(&gDriver,&gMode,"c:\\tc\\bgi");

for(iCntr=0,jCntr=0;iCntr<12;iCntr+=2,jCntr++)
xArray[iCntr] = NodesObj[jCntr].x;
for(iCntr=1,jCntr=0;iCntr<12;iCntr+=2,jCntr++)
xArray[iCntr] = NodesObj[jCntr].y;

drawpoly(6,xArray);

line(NodesObj[5].x,NodesObj[5].y,NodesObj[0].x,NodesObj[0].y);   //TO CLOSE POLYGON
getch();

Scanfill();
getch();
closegraph();
restorecrtmode();
return; }

void Scanfill(){
int iCntr,Scan;
//initialize edgelists with dummy nodes
Edges=new Edge*[getmaxy()];
for(iCntr=0;iCntr<getmaxy();iCntr++){
Edges[iCntr] = new Edge;
Edges[iCntr]->Next = NULL; }
//initialize Active edgeList with dummy nodes
Active = new Edge;
Active->Next = NULL;
BuildEdgeList(Cntr,NodesObj);
for(Scan=0;Scan<getmaxy();Scan++){
BulidActiveList(Scan);
if (Active->Next != NULL){
ResortActiveList(Scan);
UpdateActiveList(Scan);
ResortActiveList(); }}}

void InsertEdge(Edge* edgeList,Edge* InitialEdge){
Edge *PEdge,*QEdges;

QEdges=edgeList;
PEdge=QEdges->Next;
while(PEdge!=NULL)
if(InitialEdge->XIntersect < PEdge->XIntersect)
PEdge=NULL;
else{
QEdges=PEdge;
PEdge=PEdge->Next;  }
InitialEdge->Next = QEdges->Next;
QEdges->Next = InitialEdge; }

void BuildEdgeList(int Cntr,Nodes* Vertices){
Edge* NewEdge;
Nodes Vertex1,Vertex2;
int YPrev,iCntr;
Vertex1.x=Vertices[Cntr-1].x;
Vertex1.y=Vertices[Cntr-1].y;
YPrev = Vertices[Cntr-2].y;
for(iCntr=0;iCntr<Cntr;iCntr++){
Vertex2.x = Vertices[iCntr].x;
Vertex2.y = Vertices[iCntr].y;
if(Vertex1.y != Vertex2.y){
NewEdge = new Edge;
if(Vertex1.y < Vertex2.y)
MakeEdgeRec(Vertex1,Vertex2,YNext(iCntr),NewEdge);
else
MakeEdgeRec(Vertex2,Vertex1,YPrev,NewEdge); }
YPrev = Vertex1.y;
Vertex1.x = Vertex2.x;
Vertex1.y = Vertex2.y;  }}

void BulidActiveList(int Scan){
Edge *PEdge,*QEdges;
PEdge=Edges[Scan]->Next;
while(PEdge!=NULL){
QEdges=PEdge->Next;
InsertEdge(Active,PEdge);
PEdge=QEdges;  }}

void ResortActiveList(int Scan){
Edge *iEdge,*jEdge;
int iCntr;
iEdge = Active->Next;
while(iEdge!=NULL){
jEdge = iEdge->Next;
for(iCntr=iEdge->XIntersect;iCntr<=jEdge->XIntersect-1;iCntr++)
putpixel(iCntr,Scan,RED);
iEdge=jEdge->Next; }}

void UpdateActiveList(int Scan){
Edge *PEdge,*QEdges;
QEdges=Active;
PEdge=Active->Next;
while(PEdge!=NULL){
if(Scan>=PEdge->YUpper){
PEdge=PEdge->Next;
DeleteAfter(QEdges);  }
else{
PEdge->XIntersect+=PEdge->dxperscan;
QEdges=PEdge;
PEdge=PEdge->Next; }}}

void ResortActiveList(){
Edge *PEdge,*QEdges;
PEdge=Active->Next;
Active->Next=NULL;
while(PEdge!=NULL){
QEdges=PEdge->Next;
InsertEdge(Active,PEdge);
PEdge=QEdges; }}

int YNext(int YCurr){
int jCntr;
if(YCurr+1>Cntr)
jCntr=1;
else
jCntr=YCurr+1;
while(NodesObj[YCurr].y == NodesObj[jCntr].y){
if(jCntr+1>Cntr)
jCntr=1;
else
jCntr++;  }
return(NodesObj[jCntr].y); }

void DeleteAfter(Edge* QEdges){
Edge* PEdge;

PEdge = QEdges->Next;
QEdges->Next = PEdge->Next;
delete PEdge; }

void MakeEdgeRec(Nodes Lower,Nodes Upper,int YComp,Edge* CurrEdge){
CurrEdge->dxperscan = (Upper.x - Lower.x) / (Upper.y - Lower.y);
CurrEdge->XIntersect = Lower.x;
if(Upper.y < YComp)
CurrEdge->YUpper = Upper.y - 1;
else
CurrEdge->YUpper = Upper.y;
InsertEdge(Edges[Lower.y],CurrEdge); }


Bresenham's Line Drawing Algorithm using C++:

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
char IncFlag;
void Bresenham(int x1,int x2,int y1,int y2);
void DrawLine(int X,int Y,int End,int PInc,int NInc,int P,int XInc,int YInc);
void main(){
int gDriver=DETECT, gMode;
int x1,x2,y1,y2;
void Bresenham(int,int,int,int);
initgraph(&gDriver,&gMode,"c:\\tc\\bgi");

cout<<endl<<"x1   : ";
cin>>x1;
cout<<endl<<"y1   : ";
cin>>y1;
cout<<endl<<"x2   : ";
cin>>x2;
cout<<endl<<"y2   : ";
cin>>y2;
line(320,0,320,480);
line(0,240,640,240);
Bresenham(x1,x2,y1,y2);
getch(); }

void Bresenham(int x1,int x2,int y1,int y2){
int S,O,End;
int P;
int dx = abs(x1 - x2);
int dy = abs(y1 - y2);
float Slope;
int PInc,NInc,XInc,YInc;
if (dx == 0)  //Slope Infinite
{}
else{
Slope = (float)(y1 - y2) / (x1 - x2);
if (Slope>-1 && Slope<1){
IncFlag = 'X';
PInc = 2 * (dy - dx);
NInc = 2 * dy;
P = 2 * dy - dx;
if (x1>x2){
S = x2;
O = y2;
End = x1;}
else{
S = x1;
O = y1;
End = x2;}
// DrawLine(x,y,End,PInc,NInc,P,XInc,YInc); }
else{
IncFlag = 'Y';
PInc = 2 * (dx - dy);
NInc = 2 * dx;
P = 2 * dx - dy;
if (y1 > y2){
O = x2;
S = y2;
End = y1;}
else{
O = x1;
S = y1;
End = y2; }}
if (IncFlag == 'X')
putpixel(320+S,240-O,12);
else
putpixel(320+O,240-S,12);
while (S <= End){
S++;
if (P<0)
P = P + NInc;
else{
P = P + PInc;
if (Slope>0.0)
O++;
else
O--;}
if (IncFlag == 'X')
putpixel(320+S,240-O,12);
else
putpixel(320+O,240-S,12);  }}}


DDA Line Drawing Algorithm using C++:

# include <graphics.h>
# include <math.h>
# include <conio.h>
# include <iostream.h>
void DDALine(int x1,int y1,int x2,int y2,int iColor);
void main()
{
int gDriver=DETECT,gMode;

int x1,x2,y1,y2,iColor;

initgraph(&gDriver,&gMode,"c:\\tc\\bgi");
cleardevice();
cout<<endl<<"Enter x1  : ";
cin>>x1;
cout<<"Enter y1  : ";
cin>>y1;
cout<<endl<<"Enter x2  : ";
cin>>x2;
cout<<"Enter y2  : ";
cin>>y2;
cout<<endl<<"Enter COLOR  : ";
cin>>iColor;
cleardevice();
DDALine(320,1,320,480,12);
DDALine(1,240,640,240,12);
circle(320,240,2);
DDALine(320+x1,240-y1,320+x2,240-y2,iColor%16);
getch();
}
void DDALine(int x1,int y1,int x2,int y2,int iColor){
float dX,dY,iSteps;
float xInc,yInc,iCount,x,y;
dX = x1 - x2;
dY = y1 - y2;
if (fabs(dX) > fabs(dY)){
iSteps = fabs(dX);
}
else{
iSteps = fabs(dY);
}
xInc = dX/iSteps;
yInc = dY/iSteps;
x = x1;
y = y1;
circle(x,y,1);
for (iCount=1; iCount<=iSteps; iCount++){
putpixel(floor(x),floor(y),iColor);
x -= xInc;
y -= yInc;
}
circle(x,y,1);
return;
}


Sutherland Hodgemann Polygon Clipping Algorithm using C++:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#define ROUND(a) ((int)(a+0.5))
#define n 4

#define LEFT_EDGE 0x1
#define RIGHT_EDGE 0x2
#define BOTTOM_EDGE 0x4
#define TOP_EDGE 0x8

#define INSIDE(a)  (!a)
#define REJECT(a,b) (a&b)
#define ACCEPT(a,b) (!(a|b))

typedef struct wcpt2{
int x,y;
}wcpt2;

typedef struct dcpt{
int x,y;
}dcpt;

void main(){
int gd=DETECT,gm;
int left,top,right,bottom;
int x1,x2,y1,y2;
int maxx, maxy;
   /* our polygon array */
int poly[10];
void clipline(dcpt,dcpt,wcpt2,wcpt2);
clrscr();

initgraph(&gd,&gm,"c:\\tc30\\bgi");
maxx = getmaxx()/4;
maxy = getmaxy()/4;

poly[0] = 20;        /* 1st vertex */
poly[1] = maxy / 2;

poly[2] = maxx - 10; /* 2nd */
poly[3] = 10;

poly[4] = maxx - 50; /* 3rd */
poly[5] = maxy - 20;

poly[6] = maxx / 2;  /* 4th */
poly[7] = maxy / 2;

poly[8] = poly[0];
poly[9] = poly[1];

   /* draw the polygon */
drawpoly(5, poly);

rectangle(20,25,80,125);
wcpt2 pt1,pt2;
dcpt winmin,winmax;

winmin.x=20;
winmin.y=25;
winmax.x=80;
winmax.y=125;

pt1.x=20;
pt1.y=maxy/2;
pt2.x=maxx-10;
pt2.y=10;

// clipline(winmin,winmax,pt1,pt2);

int i=0;
for(int index=0;index<n;index++){
if(index==n-1){
pt1.x=poly[i];
pt1.y=poly[i+1];
i=0;
pt2.x=poly[i];
pt2.y=poly[i+1];
clipline(winmin,winmax,pt1,pt2);
}
else{
pt1.x=poly[i];
pt1.y=poly[i+1];
pt2.x=poly[i+2];
pt2.y=poly[i+3];
clipline(winmin,winmax,pt1,pt2);
}
i+=2;
}
pt1.x=poly[i];
pt1.y=poly[i+1];
clipline(winmin,winmax,pt1,pt2);
getch();
}
unsigned char encode(wcpt2 pt,dcpt winmin,dcpt winmax){
unsigned char code=0x00;
if(pt.x < winmin.x)
code=code | LEFT_EDGE;
if(pt.x > winmax.x)
code=code | RIGHT_EDGE;
if(pt.y < winmin.y)
code=code | TOP_EDGE;
if(pt.y > winmax.y)
code=code | BOTTOM_EDGE;
return code;
}
void swappts(wcpt2 *p1,wcpt2 *p2){
wcpt2 tmp;
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}


void swapcode(unsigned char *c1,unsigned char *c2)
{
unsigned char tmp;
tmp = *c1;
*c1 = *c2;
*c2 = tmp;
}


void clipline(dcpt winmin,dcpt winmax,wcpt2 p1,wcpt2 p2){
unsigned char encode(wcpt2,dcpt,dcpt);
unsigned char code1,code2;
int done = 0 , draw = 0;
float m;
void swapcode(unsigned char *c1,unsigned char *c2);
void swappts(wcpt2 *p1,wcpt2 *p2);
while(!done){
code1 = encode(p1,winmin,winmax);
code2 = encode(p2,winmin,winmax);
if(ACCEPT(code1,code2)){
draw = 1;
done = 1;
}
else if(REJECT(code1,code2))
done = 1;
else if(INSIDE(code1)) {
swappts(&p1,&p2);
swapcode(&code1,&code2);
}
if(code1 & LEFT_EDGE){
p1.y += (winmin.x - p1.x) *  (p2.y - p1.y) / (p2.x - p1.x);
p1.x = winmin.x;
}
else if(code1 & RIGHT_EDGE){
p1.y += (winmax.x - p1.x) *  (p2.y - p1.y) / (p2.x - p1.x);
p1.x = winmax.x;
}
else if(code1 & TOP_EDGE){
if(p2.x != p1.x)
p1.x += (winmin.y - p1.y) *  (p2.x - p1.x) / (p2.y - p1.y);
p1.y = winmin.y;
}
else if(code1 & BOTTOM_EDGE){
if(p2.x != p1.x)
p1.x += (winmax.y - p1.y) *  (p2.x - p1.x) / (p2.y - p1.y);
p1.y = winmax.y;  }}
    if(draw){
setcolor(5);
line(p1.x,p1.y,p2.x,p2.y); }}


Cohen Sutherland Line Clipping Algorithm using C++:

#include <iostream.h>
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
FILE *fp;

class clipping{
private:
int x;
int y;
int code[4];
public:
clipping();
void getxy(char p[5]);
void encode(int left,int right,int bottom,int top);
int getcode(int k);
float getx(void);
float gety(void);
friend int accept(clipping p1,clipping p2);
friend int reject(clipping p1,clipping p2);
friend void swap_points(clipping &p1,clipping &p2);
int point_inside(void);
void clipping::operator =(clipping p2);
void changex(int winy,float m);
void changey(int winx,float m);
};

clipping::clipping(){
int i;
x = 0;
y = 0;
for(i=0;i<4;i++)
code[i] = 0;
}

float clipping::getx(void){
return(x);
}

float clipping::gety(void){
return(y);
}

void clipping::getxy(char p[5]){
cout << "\nEnter the coordinates of point " << p;
cout << "\nX :-  ";
fscanf(fp,"%d",&x);
cout << "\nY :-  ";
fscanf(fp,"%d",&y);
}

void clipping::encode(int left,int right,int bottom,int top){
if(x < left)
code[0] = 1;
else
code[0] = 0;
if (x > right)
code[1] = 1;
else
code[1] = 0;
if (y > bottom)
code[2] = 1;
else
code[2] = 0;
if (y < top)
code[3] = 1;
else
code[3] = 0;
}

int clipping::getcode(int k){
if (code[k] == 1)
return(1);
return(0);
}

int accept(clipping p1,clipping p2){
int k;
for(k=0;k<4;k++)
if (p1.getcode(k) || p2.getcode(k))
return(0);
return(1);
}

int reject(clipping p1,clipping p2){
int k;
for(k=0;k<4;k++)
if (p1.getcode(k) && p2.getcode(k))
return(1);
return(0);
}

int clipping::point_inside(void){
if (code[0] || code[1] || code[2] || code[3])
return(0);
return(1);
}

void clipping::operator =(clipping p2){
int i;
x = p2.x;
y = p2.y;
for (i=0;i<4;i++)
code[i] = p2.code[i];
}

void swap_points(clipping &p1,clipping &p2){
clipping tmp;
tmp = p1;
p1 = p2;
p2 = tmp;
}

void clipping::changex(int winy,float m){
x = x + (winy - y) / m;
y = winy;
}

void clipping::changey(int winx,float m){
y = y + (winx - x) * m;
x = winx;
}

void main(){
int driver = DETECT,mode;
initgraph(&driver,&mode,"c:\\tc\\bgi");
clipping p1,p2,tmp;
int left,top,right,bottom,done=0,draw=0;
float m;
fp = fopen("cohensut.in","r");
clearviewport();
p1.getxy("p1");
p2.getxy("p2");
left = getmaxx() / 2 - 50;
right = getmaxx() / 2 + 50;
bottom = getmaxy() / 2 + 50;
top = getmaxy() / 2 - 50;
clearviewport();
rectangle(left,top,right,bottom);
line(p1.getx(),p1.gety(),p2.getx(),p2.gety());
while(done == 0) {
p1.encode(left,right,bottom,top);
p2.encode(left,right,bottom,top);
if (accept(p1,p2)){
done = 1;
draw = 1;
}
else if (reject(p1,p2)){
done = 1;
}
else{
if (p1.point_inside())
swap_points(p1,p2);
m = (p2.gety() - p1.gety()) / (p2.getx() - p1.getx());
if(p1.getcode(0))
p1.changey(left,m);
else if(p1.getcode(1))
p1.changey(right,m);
else if (p1.getcode(2))
p1.changex(bottom,m);
else if (p1.getcode(3))
p1.changex(top,m);
}
cout << "\nPress any key to continue...\n";
getch();
clearviewport();
rectangle(left,top,right,bottom);
line(p1.getx(),p1.gety(),p2.getx(),p2.gety());
}
if (draw){
rectangle(left,top,right,bottom);
line(p1.getx(),p1.gety(),p2.getx(),p2.gety());
}
    getch();
}

Boundary Fill (8 Connected Points) Program using C++:

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

class Circle{
int XCoord;
int YCoord;
int Radius;
void PlotPoint(int,int,int);
public:
Circle() {
XCoord=0;
YCoord=0;
Radius=0;
}

Circle(int x, int y, int z) {
XCoord=x;
YCoord=y;
Radius=z;
}

void CircleMidpoint();
void InitGraphs();
void BoundryFill(int,int,int,int);
void BoundryFillRound();
};

void Circle :: InitGraphs(){
int gMode, gDriver=DETECT;
int errorcode;
initgraph(&gDriver,&gMode,"c:\\tc\\bgi");
errorcode = graphresult();

if (errorcode != grOk) {
   cout<<"Graphics error: "<< grapherrormsg(errorcode);
   cout<<"Press any key to halt:";
   getch();
   exit(1);  }
setbkcolor(3);  }

void Circle :: PlotPoint(int x,int y,int z){
putpixel(XCoord+x,YCoord+y,z);
putpixel(XCoord-x,YCoord+y,z);
putpixel(XCoord+x,YCoord-y,z);
putpixel(XCoord-x,YCoord-y,z);
putpixel(XCoord+y,YCoord+x,z);
putpixel(XCoord-y,YCoord+x,z);
putpixel(XCoord+y,YCoord-x,z);
putpixel(XCoord-y,YCoord-x,z);  }

void Circle :: CircleMidpoint(){
int x,y;
int p;
x=0;
y=Radius;
PlotPoint(x,y,15);

p=1-Radius;
while(x<y) {
if(p<0)
    x+=1;
else{
x+=1;
y-=1; }

if(p<0)
p=p+2*x+1;
else{
p=p + 2 *(x-y) + 1;
}
PlotPoint(x,y,15);  }}

void Circle :: BoundryFillRound(){
int x,y;
int p;
for(int iCntr=1;iCntr<Radius;iCntr++) {
x=0;
y=iCntr;
PlotPoint(x,y,5);

p=1-iCntr;
while(x<y) {
if(p<0)
    x+=1;
else{
x+=1;
y-=1;
}
if(p<0)
p=p+2*x+1;
else{
p=p + 2 *(x-y) + 1;
}
PlotPoint(x,y,5);
}
// sleep(1); }}

void Circle :: BoundryFill(int x, int y,int fill,int bound){
int cur;
cur=getpixel(x,y);
if(cur!=bound && cur!= fill){
putpixel(x,y,fill);
Circle :: BoundryFill(x+1, y,fill,bound);
Circle :: BoundryFill(x-1, y,fill,bound);
Circle :: BoundryFill(x, y-1,fill,bound);
Circle :: BoundryFill(x, y+1,fill,bound);
Circle :: BoundryFill(x+1, y+1,fill,bound);
Circle :: BoundryFill(x+1, y+1,fill,bound);
Circle :: BoundryFill(x-1, y-1,fill,bound);
Circle :: BoundryFill(x-1, y+1,fill,bound); }}
void main(){
Circle cObj(250,250,42);
cObj.InitGraphs();
cObj.CircleMidpoint();
cObj.BoundryFillRound();
//cObj.BoundryFill(250,250,11,15);
getch();
closegraph();
}


Nicol Lee Algorithm for Line Clipping using C++:

# include <stdio.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>

int xmin,ymin,xmax,ymax;
void main(){
int x1,y1,x2,y2;
int gdriver = DETECT, gmode, errorcode;
int findRegionP1(int,int);
void clipline1(int,int,int,int);
void clipline2(int,int,int,int);
void clipline3(int,int,int,int);
int ch;
float m;
clrscr();
printf("\nEnter the xmin:->");
scanf("%d",&xmin);
printf("\nEnter the ymin:->");
scanf("%d",&ymin);
printf("\nEnter the xmax:->");
scanf("%d",&xmax);
printf("\nEnter the ymax:->");
scanf("%d",&ymax);

initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
setcolor(15);
rectangle(xmin,ymin,xmax,ymax);
printf("Enter the x1:->");
scanf("%d",&x1);
printf("Enter the y1:->");
scanf("%d",&y1);
printf("Enter the x2:->");
scanf("%d",&x2);
printf("Enter the y2:->");
scanf("%d",&y2);
setcolor(12);
line(x1,y1,xmin,ymin);
line(x1,y1,xmax,ymin);
line(x1,y1,xmax,ymax);
line(x1,y1,xmin,ymax);
getch();
setcolor(3);
line(x1,y1,x2,y2);
getch();
ch=first_end_point_region(x1,y1);
switch(ch){
case 1:
clipline1(x1,y1,x2,y2);
break;
case 2:
clipline2(x1,y1,x2,y2);
break;
case 3:
clipline3(x1,y1,x2,y2);
break;
default:
printf("\nInvalid select of the option: ");
}
getch();
}

int first_end_point_region(int x,int y){
/* u have two equations:-
xmin <= x <= xmax;
ymin <= y <= ymax; */

if(x>=xmin && x<=xmax && y>=ymin && y<=ymax)
return 1;
else if(x<xmin && y>=ymin && y<=ymax)
return 2;
else if(x<=xmin && y<=ymin)
return 3;
else
return 0;
}

/* point p1 is inside the clip window */
void clipline1(int x1,int y1,int x2,int y2){
int draw=1;
float m,m1,m2,m3,m4;
int nx1,ny1,nx2,ny2;

/* calculate slopes for all the lines passing thru vertices
and including the input line :- */

m=((float)(y2-y1))/(x2-x1);
m1=((float)(ymin-y1))/(xmin-x1);
m2=((float)(ymin-y1))/(xmax-x1);
m3=((float)(ymax-y1))/(xmax-x1);
m4=((float)(ymax-y1))/(xmin-x1);
nx1=x1;
ny1=y1;

// point p2 is on top
if(((abs(m)>=m1 && x2<x1) || (abs(m)>abs(m2) && x2>x1)) && y1>y2){
  // point p2 is also inside clip window
  if(y2>ymin){
  nx2=x2;
  ny2=y2; }
  // point p2 is outside clip window
  else{
  ny2=ymin;
  nx2=x1+(ymin-y1)/m; }}
// point p2 is on right side of clip window
else if(m>m2 && m<m3 && x2>=xmax){
// point p2 is inside clip window
if(x2<xmax){
nx2=x2;
ny2=y2;
}
// point p2 is outside clip window
else{
nx2=xmax;
ny2=y1+(xmax-x1)*m;  }}
// point p2 is on bottom side of clip window
else if((abs(m)>=m3 && x2>x1) || (abs(m)>abs(m4) && x2<x1)){
// point p2 is inside clip window
  if(y2<ymax){
nx2=x2;
ny2=y2;  }
// point p2 is outside clip window
  else{
ny2=ymax;
nx2=x1+(ymax-y1)/m;  }}
// point p2 is on left side of clip window
else if(m>m4 && m<m1){
// point p2 is inside the clip window
if(x2>xmin){
nx2=x2;
ny2=y2; }
// point p2 is outside the clip window
else{
nx2=xmin;
ny2=y1+(xmin-x1)*m;  }}
else
draw=0;
getch();
cleardevice();
setcolor(18);
rectangle(xmin,ymin,xmax,ymax);
if(draw){
setcolor(12);
line(x1,y1,xmin,ymin);
line(x1,y1,xmax,ymin);
line(x1,y1,xmax,ymax);
line(x1,y1,xmin,ymax);
setcolor(5);
line(nx1,ny1,nx2,ny2);  }}

/* Point p1 is in the edge region */
void clipline2(int x1,int y1,int x2,int y2){
int draw=1;
float m,m1,m2,m3,m4;
int nx1,ny1,nx2,ny2;

m=((float)(y2-y1))/(x2-x1);
m1=((float)(ymin-y1))/(xmin-x1);
m2=((float)(ymin-y1))/(xmax-x1);
m3=((float)(ymax-y1))/(xmax-x1);
m4=((float)(ymax-y1))/(xmin-x1);

// Point p2 is in Left-Top region
if(m>m1 && m<m2){
// Point p2 is inside the clip window
if(y2>ymin){
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=x2;
ny2=y2;  }
// Point p2 is outside the clip window
else{
nx1=xmin;
ny1=y1+m*(xmin-x1);
ny2=ymin;
nx2=x1+(ymin-y1)/m;  }}

// Point p2 is in Left-Right region
else if(m>m2 && m<m3){
// Point p2 is inside the clip window
if(x2<xmax){
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=x2;
ny2=y2;  }
// Point p2 is outside the clip window
else{
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=xmax;
ny2=y1+(xmax-x1)*m;  }}
// Point p2 is in Left-Bottom region
else if(m>m3 && m<m4){
// Point p2 is inside the clip window
if(y2<ymax){
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=x2;
ny2=y2;
}
// Point p2 is outside the clip window
else{
nx1=xmin;
ny1=y1+m*(xmin-x1);
ny2=ymax;
nx2=x1+(ymax-y1)/m;  }}
else
draw=0;
getch();
cleardevice();
setcolor(18);
rectangle(xmin,ymin,xmax,ymax);
if(draw){
setcolor(12);
line(x1,y1,xmin,ymin);
line(x1,y1,xmax,ymin);
line(x1,y1,xmax,ymax);
line(x1,y1,xmin,ymax);
setcolor(5);
line(nx1,ny1,nx2,ny2); }}

/* Point p1 is in the Corner Region */
void clipline3(int x1,int y1,int x2,int y2){
int draw=1;
float m,m1,m2,m3,m4,tm1,tm2;
int nx1,ny1,nx2,ny2;
int flag,t;
tm1=((float)(ymin-y1))/(xmin-x1);
tm2=((float)(ymax-ymin))/(xmax-xmin); //diagonal slope

m=((float)(y2-y1))/(x2-x1);
m1=((float)(ymin-y1))/(xmax-x1);
m2=((float)(ymax-y1))/(xmax-x1);
m3=((float)(ymin-y1))/(xmin-x1);
m4=((float)(ymax-y1))/(xmin-x1);

// Point p1 is towards the left side of the clip window (case2)
if(tm1<tm2){
flag=2;
t=m2;
m2=m3;
m3=t;
}
// Point p1 is towards the top side of the clip window (case1)
else
flag=1;

// Point p2 is in the Top-Right region
if(m>m1 && m<m2){
// Point p2 is outside the clip window
if(x2>xmax && y2>ymin){
ny1=ymin;
nx1=x1+(ymin-y1)/m;
nx2=xmax;
ny2=y1+m*(xmax-x1); }

// Point p2 is inside the clip window
else if(y2>ymin && x2<xmax){
ny1=ymin;
nx1=x1+(ymin-y1)/m;
ny2=y2;
nx2=x2; }}
// Point p2 is Left-Right or Top-Bottom region
else if(m>m2 && m<m3){
// Point p2 is in Top-Bottom region (case1)
if(flag==1){
// Point p2 is outside the clip window
       if(y2>=ymax)
       {
       ny1=ymin;
       nx1=x1+(ymin-y1)/m;
       nx2=x1+(ymax-y1)/m;
       ny2=ymax;
       }
       // Point p2 is inside the clip window
       else if(y2>=ymin){
       ny1=ymin;
       nx1=x1+(ymin-y1)/m;
       nx2=x2;
       ny2=y2;  }}

// Point p2 is in Left-Right region (case2)
else{
// Point p2 is outside the clip window
       if(x2>=xmax){
       nx1=xmin;
       ny1=y1+m*(xmin-x1);
       nx2=xmax;
       ny2=y1+m*(xmax-x1);  }
       // Point p2 is inside the clip window
       else if(x2>=xmin){
       nx1=xmin;
       ny1=y1+m*(xmin-x1);
       nx2=x2;
       ny2=y2; }}}

// Point p2 is in Left-Bottom region
else if(m>m3 && m<m4){
// Point p2 is outside the clip window
if(y2>=ymax){
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=x1+(ymax-y1)/m;
ny2=ymax;
}
// Point p2 is inside the clip window
else if(y2>=ymin){
nx1=xmin;
ny1=y1+m*(xmin-x1);
ny2=y2;
nx2=x2; }}
else
draw=0;
getch();
cleardevice();
setcolor(18);
rectangle(xmin,ymin,xmax,ymax);
if(draw){
setcolor(12);
line(x1,y1,xmin,ymin);
line(x1,y1,xmax,ymin);
line(x1,y1,xmax,ymax);
line(x1,y1,xmin,ymax);
setcolor(5);
line(nx1,ny1,nx2,ny2);  }}

Program to draw a Capacitor using C++:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main(){
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:tcbgi");
outtextxy(110,60,"C");
line(80,100,100,100);
line(100,80,100,120);
line(120,80,120,120);
line(120,100,140,100);
getch();
closegraph();
}


Program to draw a Circle using C++:

#include<graphics.h>
#include<conio.h>
int main(){
int gd=DETECT, gm;
initgraph(&gd, &gm, "c:\\turboc3\\bgi" );
circle(200,100,100);
getch();
closegraph();
}

Program to draw space(outside Earth) using C++:

#include<graphics.h>
main(){
  int gd=DETECT,gm;
  int i,x,y;
  initgraph(&gd,&gm,"");
  line(0,0,640,0);
  line(0,0,0,480);
  line(639,0,639,480);
  line(639,479,0,479);
  for(i=0;i<=1000;i++){
    x=rand()%639;
    y=rand()%479;
    putpixel(x,y,15);
  }
  getch();
  closegraph();
}

Program to Plot Bar Graph using C++:

#include<graphics.h>
main(){
  int gd=DETECT,gm,maxx,maxy,x,y,button;
  initgraph(&gd,&gm,"");
  line(80,150,200,150);
  line(80,150,80,50);
  settextstyle(1,HORIZ_DIR,1);
  outtextxy(100,153,"X axis->");
  settextstyle(1,VERT_DIR,1);
  outtextxy(60,50,"Y axis->");
  bar(100,100,120,150);
  bar(130,120,150,150);
  getch();
  closegraph();}

Program to draw a scenery using C++:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void tree(int t,int q){
int r=15;
r=15;
setfillstyle(6,BROWN);
rectangle(150+t,390+q,170+t,340+q);
floodfill(151+t,342+q,2);
setfillstyle(11,GREEN);
fillellipse(142+t,347+q,r,r);
fillellipse(160+t,330+q,r,r);
fillellipse(175+t,340+q,r,r);
fillellipse(180+t,350+q,r,r);}

int main(){
int gd=DETECT,gm;
int i,j,k,t,q,r;
float x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(2);
rectangle(0,0,getmaxx(),getmaxy());
for(i=0;i<getmaxx();i+=120){
line(i,250,i+60,170);
line(i+60,170,i+120,250);}

t=10;
line(0,400,getmaxx(),350);
B:line(50+t,280,50+t,320);
line(50+t,320,60+t,320);
line(60+t,320,70+t,320);
line(70+t,320,80+t,320);
line(130+t,320,80+t,320);
line(70+t,320,70+t,300);
line(60+t,300,60+t,320);
line(70+t,300,60+t,300);
line(80+t,320,80+t,280);
line(130+t,320,130+t,280);
line(80+t,280,130+t,280);
line(50+t,280,80+t,280);
line(50+t,280,65+t,260);
line(65+t,260,80+t,280);
line(65+t,260,110+t,260);
line(130+t,280,110+t,260);
setfillstyle(SOLID_FILL,DARKGRAY);
floodfill(51+t,282,2);
setfillstyle(SOLID_FILL,RED);
floodfill(61+t,302,2);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(81+t,282,2);
setfillstyle(SOLID_FILL,MAGENTA);
floodfill(66+t,270,2);
setfillstyle(4,BROWN);
floodfill(100+t,270,2);
if(t==10){
t=320;
goto B;}

tree(0,-10);
tree(100,-20);
tree(30,-100);
tree(350,-60);
tree(160,-50);
//circle();

setfillstyle(SOLID_FILL,12);
fillellipse(150,80,35,35);
setfillstyle(2,LIGHTBLUE);
floodfill(10,10,2);
setfillstyle(SOLID_FILL,CYAN);
floodfill(1,getmaxy()-1,2);
setfillstyle(SOLID_FILL,LIGHTGREEN);
floodfill(49,270,2);

//x=150;
//y=80;
for(i=36;i<80;i++)
for(j=0;j<=360;j+=10){
x=150+i*cos(((float)j*3.14)/180);
y=80+i*sin(((float)j*3.14)/180);
putpixel(x,y,LIGHTRED);}
getch();}

Program to draw a moving Car using C++:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
int main(){
int gd=DETECT,gm;
int i=0,m=0;
initgraph(&gd,&gm,"..\\bgi");
while(!kbhit()){
cleardevice();
i++;
if(i>getmaxx())
i-=670;
line(80+i,300,90+i,270);
line(90+i,270,120+i,270);
line(120+i,270,160+i,240);
line(160+i,240,230+i,240);
line(230+i,240,275+i,270);
line(275+i,270,310+i,270);
line(310+i,270,335+i,290);
line(335+i,290,335+i,300);
line(255+i,300,335+i,300);
line(180+i,300,210+i,300);
line(80+i,300,135+i,300);
arc(232+i,300,0,180,23);
arc(157+i,300,0,180,23);
circle(232+i,300,18);
circle(157+i,300,18);
pieslice(232+i,300,0+m,90+m,18);
pieslice(232+i,300,180+m,270+m,18);
pieslice(157+i,300,0+m,90+m,18);
pieslice(157+i,300,180+m,270+m,18);
if(m<-360)
m=90;
m-=3;
delay(5);}
getch();
closegraph();}