Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts
Monday, May 17, 2010

Implementation of Shortest Job First Scheduling Algorithm SJF

Source code SJF Programming Algorithm
#include<stdio.h>
main()
{
int n,j,temp,temp1,temp2,
pr[10],b[10],t[10],w[10],p[10],i;
float att=0;awt=0;
for(i=0;i<10;i++)
{
b[i]=0;w[i]=0;
}
printf(“Enter the number of process:”);
scanf(“%d”,&n);
printf(“\n Enter the burst time”);
for(i=0;i<n;i++)
{
scanf(“%d”,&b[i]);
p[i]=i;
}
for(i=0;i<n;i++)
{
for(j=1;j<n;j++)
{
if(b[i]>b[j]
{
temp=b[i];
temp1=p[i];
b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
}
w[0]=0;
for(i=0;i<n;i++)
w[i+1]=w[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
awt=aw+w[i]:
att=att+t[i];
}
awt=awt/n;
att=att/n;
printf(“\n\t Process \tWaitimgtime \t Turnaroundtime\n);
for(i=0;i<n;i++)
printf(“\t p[%d] \t %d\t\t %d\n”,p[i].w[i],t[i]);
printf(“\n The average waiting time is %t \n”,awt);
printf(“The average turnaround time is %f\n”,att);
return 1;
}
OUTPUT CS1252 Operating Systems lap
[redhat35@localhost Rhel5]$ cc sjf.c
[redhat35@localhost Rhel5]$ ./a.out
Enter the number of process:5
Enter the burst time:5
4
3
2
1
Process Waiting time Turnaround time
P[4] 0 1
P[0] 1 6
P[1] 6 10
P[2] 10 13
P[3] 13 15
The average waiting time is 6.000000
The average turnaround time is 9.000000

read more "Implementation of Shortest Job First Scheduling Algorithm SJF"

Cohen Sutherland Line Clipping Algorithm in C Program | CS1355-Graphics & Multimedia Lab

Computer Graphics LC->line clipping algorithm is method of eliminate lines of outside area of the object .so outside of object viewing is removed .These line clipping was performed by the line function from graphics.h header file.
Source code programming
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void clip(float,float,float);
int i,j=0,n;
int rx1,rx2,ry1,ry2;
float x1[8],y1[8];
void main()
{
int gd=DETECT,gm;
int i,n;
float x[8],y[8],m;
clrscr();
initgraph(&gd,&gm,"");
printf("coordinates for rectangle : ");
scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);
printf("no. of sides for polygon : ");
scanf("%d",&n);
printf("coordinates : ");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
cleardevice();
outtextxy(10,10,"Before clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<n-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<n-1;i++)
{
m=(y[i+1]-y[i])/(x[i+1]-x[i]);
clip(x[i],y[i],m);
}
clip(x[0],y[0],m);
outtextxy(10,10,"After clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<j-1;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
getch();
}
void clip(float e,float f,float m)
{
while(e<rx1 e>rx2 f<ry1 f>ry2)
{
if(e<rx1)
{
f+=m*(rx1-e);
e=rx1;
}
else if(e>rx2)
{
f+=m*(rx2-e);
e=rx1;
}
if(f<ry1)
{
e+=(ry1-f)/m;
f=ry1;
}
else if(f>ry2)
{
e+=(ry2-f)/m;
f=ry2;
}
x1[j]=e;
y1[j]=f;
j++;
}
}
//# Author: J.Ajai
//#Mail-id- ajay.compiler@gmail.com
//# PH:+91-9790402155
OUTPUT

Enter the x1 & y1 coordinates:
X1 : 70
Y1 : 80
Enter the x2 & y2 coordinates:
X2 : 250
Y2 : 280

Cohen Sutherland Line Clipping Algorithm  computer Graphics Lab

read more "Cohen Sutherland Line Clipping Algorithm in C Program | CS1355-Graphics & Multimedia Lab"

Distance Vector Routing protocol Algorithm CS1305- NETWORK LAB

Step by step DVR Algorithm
The number of nodes is obtained and stored in a variable
The distance between all nodes are accepted in adjacency matrix
Each nod is estimated and displayed using estimate table
According to the input node the shortest distance between the neighbors route is calculated
For given node the shortest estimated between each node is displayed
Finally the stop Distance Vector routing Protocol algorithm
Source code in C language programming DVR CS1305- NETWORK LAB
#include<stdio.h>
#include<ctype.h>
int graph[12][12];
int e[12][12];
int ad[12];
int no,id,adc,small,chosen;
char nodes[12]={“a b c d e f g h i j k l”};
int main()
{
int i,j,k,ch1,ch2=0;
adc=0;
printf("enter the no nodes");
scanf("%d",&no);
printf("enter the value for adjacency matrix");
for(i=0;i<no;i++)
{
for(j=0;j<no;j++)
{
printf(" enter values for %d,%d position:",(i+1),(j+1));
scanf("%d",&graph[i][j]);
}
}
printf("enter initial estimates");
for(i=0;i<no;i++)
{
printf("estimate for node %c \n",nodes[i]);
for(j=0;j<no;j++)
{
printf("to node%c",nodes[j]);
scanf("%d",&e[i][j]);
}
}
do
{
printf("\n menu:\n 1.routing info for node");
printf("2.estimated table\n ");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("\n which node should routing table be built(1-a)(2-b)...");
scanf("%d",&id);
id--;
adc=0;
printf("neighbours for particular node");
for(i=0;i<no;i++)
{
if(graph[id][i]==1)
{
ad[adc]=i;
adc++;
printf("%c",nodes[i]);
}
}
for(i=0;i<no;i++)
{
if(id!=i)
{
small=100;
chosen=-1;
for(j=0;j<adc;j++)
{
int total=e[ad[j]][i]+e[id][ad[j]];
if(total<small)
{
small=total;
chosen=j;
} }
e[id][i]=small;
printf("\n shortest estimate to %c is %d",nodes[i],small);
printf("\n next hop is %c",nodes[ad[chosen]]);
} else
e[id][i]=0;
}
break;
case 2:
printf("\n");
for(i=0;i<no;i++)
{ for(j=0;j<no;j++)
printf("%d",e[i][j]);
printf("\n");
} break;
}
printf("\n do u want to continue (1-yes,2-no)");
scanf("%d",&ch2);
}
while(ch2==1);
return 0; }
Output for Distance Vector Routing Protocol CS1305- NETWORK LAB
[it28@localhost studentwebsite]$ cc distancevector.c
[it28@localhost studentwebsite]$ ./a.out
Enter the no of nodes 4
Enter the values for adjacency matrix:
Enter the value for 1,1 position:0
Enter the value for 1,2 position:1
Enter the value for 1,3 position:1
Enter the value for 1,4 position:0
Enter the value for 2,1 position:1
Enter the value for 2,2 position:0
Enter the value for 2,3 position:0
Enter the value for 2,4 position:1
Enter the value for 3,1 position:1
Enter the value for 3,2 position:0
Enter the value for 3,3 position:0
Enter the value for 3,4 position:1
Enter the value for 4,1 position:0
Enter the value for 4,2 position:1
Enter the value for 4,3 position:1
Enter the value for 4,4 position:0
Enter the initial estimates for node a
To node a 0
To node b 32
To node c 25
To node d 66
Estimate for node b
To node a 10
To node b 0
To node c 23
To node d 56
Enter the initial estimates for node c
To node a 12
To node b 23
To node c 0
To node d 41
Enter the initial estimates for node d
To node a 13
To node b 53
To node c 62
To node d 0
Menu
1.Routing Information for a node
2.Estimate table
Enter the choice(1,2):1
Which node should routing table be built(1-a)(2-b)…1
The neighbors for the particular node b c
Shortest estimate to b is 32
Next hop is b
Shortest estimate to c is 25
Shortest estimate to d is 66
Nest hop is c
Do you want to continue (1-yes,2-no)
Menu
1. Routing Information for a node
2. Estimate table
Enter the choice(1,2):2
0 32 25 66
10 0 23 56
12 23 0 41
13 53 62 0

read more "Distance Vector Routing protocol Algorithm CS1305- NETWORK LAB"

Online Banking System Algorithm in c++ programming Internet Bank Software Generate code in c++

To generate the code for the online ticket reservation system using rational rose.
PROCEDURE Generate Source code:
Step1:Design the use case >diagram under the use case view ,sequence ,class, collaboration,activity,state chart, diagram under the logical view, component diagram under the component view. Deployment diagram under the deployment view for the online ticket reservation system.
Step2:
Select the menu tool in that chooses the option, in that choose the tab notation and set the default language as ANCI C++ and click ok.
Step3:
Goto to the component view ,in that select anyone of the relevant component in the component diagram and right click the component and choose open specification and click the realize tab and assign the classes listed.
Step4:
Right the same component choose open specification and select general tab andset the language as and press apply.
Step5:
Select the same component in the browser window right click it, choose and click generate code. The directory window specifies the directory path where the header file and input files are to be stored and press ok.
Step6:
All the required files will be generated in the specified directory. If any change is done in the class diagram does the above steps again.
CLASS DIAGRAM : Online Banking System
 Online Banking System, Algorithm in c++ programming  Generate Sourcecode in c++ Account.cpp Source code in c++ programming algorithm
#include ”account.h”
//##ModelId=48C0C8E7031C
account::update acc()
{
}
//## ModelId=48C0C8F00203
account::check acc()
{
}
account.h source in c++ programming algorithm
#ifndef ACCOUNT_H_HEADER_INCLUDED_B73F6364
#define ACCOUNT_H_HEADER_INCLUDED_B73F6364
//##ModelId=48C0C8BB00FA
class account
{
public:
//##ModelId=48C0C8E7031C
Update acc();
//##ModelId=48C0C8F00203
check acc();
private:
//##ModelId=48C0C8C9036B
int accno;
//##ModelId=48C0C8CF037A
int bal;
//##ModelId=48C0C8D401F4
string cust name;
};
#end if /*ACCOUNT_H_HEADER_INCLUDED_B73F6364*/
bank.cpp in c++ programming algorithm
#include “bank.h”
//## ModelId=48C0C8900203
bank::providing loan()
{
}
//##ModelId=48C0C8980261
bank::update details()
{
}
//##ModelId=48C0C8A00157
bank::collect money()
{
}
//##ModelId=48C0C8A90251
bank::transaction()
{
}
bank.h Online Banking System
#ifndef BANK_H_HEADER_INCLUDED_B73F22D0
#define BANK_H_HEADER_INCLUDED_B73F22D0
//##ModelID=48C0C828001F
class bank
{
public:
//##ModelId=48C0C8900203
providing loan();
//##ModelId=48C0C8980261
update details();
//##ModelId=48C0C8A00157
collect money();
//##ModelId=48C0C8A90251
transaction();
private:
//##ModelId=48C0C834034B
string cust details;
//##ModelId=48C0C84C0232
string loan details();
//##ModelId=48C0C85D0167
string rules and regulation;
//##ModelId=48C0C86F000F
string trans.type;
//##ModelId=48C0C87B00DA
string trans.date;
//##ModelId=48C0C8840109
int trans.time;
};
#endif /*BANK_H_HEADER_INCLUDED_ B73F22D0*/
customer.cpp Internet Bank Software
#include “customer.h”
//##ModelId=48C0C80201C5
customer::create new acc()
{
}
//##ModelId=48C0C80A0242
Customer::deposit()
{
}
//##ModelId=48C0C80F02EE
customer::withdraw()
{
}
Thus the code can be generated for bank process by using rational rose as successfully. This code has been generating by use of use case diagram, Activity diagram, Component diagram, Collaboration and class diagram. Internet Bank Software

read more "Online Banking System Algorithm in c++ programming Internet Bank Software Generate code in c++"

HEAP SORT Data Structure Source Code Programming Algorithm Heap Sort

Heap sort there header file it contains following function void swap(int *x,int *y),void percdown(int A[],int i,int n),void display(int A[],int n),void heapsort(int A[],int n) by using these fucniton and data variable it will perform Heap sort algorithm. Data structure Lab Source code Programming algorithm - CS1152 c/c++
“Heap.h” file Heap Sort Programming Algorithm #include<stdio.h>HEAP SORT Data Structure Source Code Programming Algorithm Heap Sort
#include<conio.h>
#define leftchild(i) (2*i+1)
void swap(int *x,int *y)
{ int temp;
temp=*x;
*x=*y;
*y=temp;
}
void percdown(int A[],int i,int n)
{
int child,tmp;
for(tmp=A[i];leftchild(i)<n;i=child)
{
child=leftchild(i);
if(child!=n-1&&A[child+1]>A[child])
child++;
if(tmp<A[child])
A[i]=A[child];
else
break;
}
A[i]=tmp;
}
void display(int A[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\t%d\t",A[i]);
}
printf("\n");
}
void heapsort(int A[],int n)
{
int i;
for(i=n/2;i>=0;i--)
percdown(A,i,n);
display(A,n);
for(i=n-1;i>0;i--)
{
swap(&A[0],&A[i]);
percdown(A,0,i);
display(A,n);
getch();
}
}“Heap.c” file Heap Sort Algorithm Data Structure
#include"heap.h"
void main()
{
int A[20],n;
int i;
clrscr();
printf("\n\n How many numbers do u want to sort:\t");
scanf("%d",&n);
printf("\n Enter the values");
for(i=0;i<n;i++)
{
printf("\nA[%d]\t",i);
scanf("%d",&A[i]);
}
heapsort(A,n);
getch();
}
Heap Sort OUTPUT Data structure Lab Source code Programming algorithm - CS1152 c/c++
How many numbers do u want to sort: 5
Enter the values
A[0] 100
A[1] 25
A[2] 10
A[3] 9
A[4] 5
100 25 10 9 5
25 9 10 5 100
10 9 5 25 100
9 5 10 25 100
5 9 10 25 100

read more "HEAP SORT Data Structure Source Code Programming Algorithm Heap Sort"
Friday, May 14, 2010

Circle drawing using Bresenhams Algorithm In c program | CS1355-Graphics & Multimedia Lab

Without using circle function in graphics draw the circle. By using Bresenhams Algorithm draw the circle.These Circle algorithm is a variant of Bresenham’s line algorithm.The algorithm begin with circle formula .

x(2)+y(2)=r(2),x(2) repersent x square..,
See the source code
Source code C Language programming
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void cplot(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int x,y,p,xc,yc,r;
initgraph(&gd,&gm,"");
cleardevice();
printf("x,y,r : ");
scanf("%d%d%d",&xc,&yc,&r);
x=0;y=r;
p=1-r;
cplot(xc,yc,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cplot(xc,yc,x,y);
}
getch();
}
void cplot(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc-y,15);
putpixel(xc+y,yc+x,15);
putpixel(xc-y,yc+x,15);
putpixel(xc+y,yc-x,15);
putpixel(xc-y,yc-x,15);
}
//# Author: J.Ajai
//#Mail-id- ajay.compiler@gmail.com
//# PH:+91-9790402155
OUTPUT
Bresenhams Circle Drawing Algorithm
Enter the center point of the circle:
X = 100
Y = 200
R = 50
Bresenhams Circle Drawing Algorithm in c program | Computer Graphics  Lap:

read more "Circle drawing using Bresenhams Algorithm In c program | CS1355-Graphics & Multimedia Lab"

Ellipse Drawing using Midpoint Algorithm in c program | CS1355-Graphics & Multimedia Lab

Without using ellipse function in graphics draw the ellipse. By using Bresenhams algorithm .The main object in the algorithm is to perform analyse and manipulate linear equation so integer arithmetic is used in the calculations.Integer Arithmetic has the advantages of speed and precision; working with floating point values requires more time and it is difficult one and memory and such values would need to be rounded to integers anyway. In this article we assign the more difficult problem of approximating the plot of an ellipse on a grid of discrete pixels, using only
integer arithmetic.
Ellipse Drawing Algorithm in c program  computer graphics lab

Source code programming
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"");
scanf("%d%d",&xc,&yc);
scanf("%d%d",&a,&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc+x,yc-y,10);
}
//# Author: J.Ajai
//#Mail-id- ajay.compiler@gmail.com
//# PH:+91-9790402155
OUTPUT
Ellipse Drawing Algorithm
Enter the co-ordinates
Xc = 200
Yc = 200
A = 100
B = 70
Ellipse Drawing Algorithm in c program  computer graphics lab
read more "Ellipse Drawing using Midpoint Algorithm in c program | CS1355-Graphics & Multimedia Lab"

Ellipse Drawing Algorithm in C++ Program Using Midpoint CS1355-Graphics and Multimedia Lab

Ellipse drawing algorithm is drawn by there plot ellipse function in these program.
Plot ellipse function all the variable is passed by the float variable. And the final a1 data variable is find and declare by the long data variable .I also use the other method to draw the ellipse drawing using mid point algorithm see that program and source code ellipse drawing algorithm.There header files are iostream.h conio.h graphics.h math.h stdlib.h stdio.h
Source code in c++ Language Programming For ellipse drawing algorithm
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
void plotellipse(float,float,float,float);
class ellip
{
private:
float a,b,xc,yc,x,y,h,v,k;
public:
void getdata();
void plot();
};
void ellip::getdata()
{
cout<<"Enter the value for a and b:";
cin>>a>>b;
}
void ellip::plot()
{
xc=getmaxx()/2;
yc=getmaxy()/2;
line(0,yc,xc*2,yc);
line(xc,0,xc,yc*2);
for(k=0;k<360;k++)
{
h=a*cos((k*3.14)/180);
v=b*sin((k*3.14)/180);
x=h;
y=v;
plotellipse(xc,yc,x,y);
}
}
void main()
{
ellip p;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:/tc/bgi");
cleardevice();
gotoxy(2,2);
p.getdata();
settextstyle(2,0,8);
outtextxy(325,5,"ELLIPSE");
p.plot();
getch();
closegraph();
}
void plotellipse(float xc,float yc,float x,float y)
{
int i,j;
long a1,a2;
getaspectratio(&i,&j);
a1=(long)x*(long)j/(long)i;
a2=(a1);
putpixel(xc+a2,yc+y,15);
putpixel(xc-a2,yc+y,15);
putpixel(xc+a2,yc-y,15);
putpixel(xc-a2,yc-y,15);
}
Output of Ellipse Drawing Algorithm CS1355-Graphics and Multimedia Lab
Ellipse Drawing Algorithm in C++ Program Using Midpoint CS1355-Graphics and Multimedia Lab

read more "Ellipse Drawing Algorithm in C++ Program Using Midpoint CS1355-Graphics and Multimedia Lab"

Bresenhams Circle Drawing Algorithm in c++ language programming

Using outtextxy and putpixel function in graphics function it will draw the Breshenhams circle drawing algorithm. Putpixel and outtextxy they two have the three arguments using these argument it will draw the circle
Source code for Bresenhams circle drawing algorithm CS1355-Graphics and Multimedia Lab
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
class mycircle
{
private:
int x,y,r,d,x1,y1;
public:
mycircle();
void showcircle();
}
mycircle::mycircle()
{
x=0,y=0;
cout<<"Enter the Co-ordinates of Circle:";
cin>>x>>y;
cout<<"Enter the Radius of the Circle:";
cin>>r;
}
void mycircle::showcircle()
{
char *s;
x1=0,y1=r;
d=3-2*r;
while(x1<=y1)
{
putpixel((x+x1+320),(y+y1+240),5);
putpixel((x-x1+320),(y+y1+240),5);
putpixel((x+x1+320),(y-y1+240),5);
putpixel((x-x1+320),(y-y1+240),5);
putpixel((x+y1+320),(y+x1+240),5);
putpixel((x-y1+320),(y+x1+240),5);
putpixel((x+y1+320),(y-x1+240),5);
putpixel((x-y1+320),(y-x1+240),5);
if(d<0)
{
d+=4*x1+6;
}
else
{
d+=4*(x1-y1)+10;
y1--;
}
x1++;
}
setcolor(5);
outtextxy(318+x,235+y," ");
setcolor(15);
sprintf(s,"Center(%d,%d)",x,y);
outtextxy(20,10,"The Center is at:");
outtextxy(20,20,s);
sprintf(s,"Radius=%d",r);
outtextxy(20,30,s);
getch();
}
void main()
{
int gd=DETECT,gm,i,j,xx=190,xxx=480;
clrscr();
mycircle a;
char *mess[]={"b","r","e","s","e","n","h","a","m","s","c","i","r","c","l","e","a","l","g",
"o","r","i","t","h","m"};
initgraph(&gd,&gm,"C:/tc/bgi");
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(i=0,j=27;i<16,j<=14;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<510;i+=10)
{
if(i==320)
{
continue;
}
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
{
continue;
}
outtextxy(317,i,"-");
}
outtextxy(310,230,"o");
outtextxy(530,240,"x");
outtextxy(320,450,"-y");
outtextxy(100,240,"-x");
outtextxy(320,30,"y");
a.showcircle();
closegraph();
}
Output for Bresenhams Circle Drawing Algorithm CS1355-Graphics and Multimedia Lab
Bresenhams Circle Drawing Algorithm

read more "Bresenhams Circle Drawing Algorithm in c++ language programming"

WINDOW – VIEW PORT MAPPING algorithm Source code C Language Programming

Window view port mapping is draw by the graphics function using rectangle and line function. There rectangle and line has four argument there are declare as the Float data variable.
Source code Window-View Port Mapping
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
float sx,sy;
int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:/tc/bgi");
printf("Enter the Co-ordinates x1,y1,x2,y2,x3,y3\n");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
w1=5;
w2=5;
w3=635;
w4=465;
rectangle(w1,w2,w3,w4);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
getch();
v1=425;
v2=75;
v3=550;
v4=250;
sx=(float)(v3-v1)/(w3-w1);
sy=(float)(v4-v2)/(w4-w2);
rectangle(v1,v2,v3,v4);
x1=v1+floor(((float)(x1-w1)*sx)+0.5);
x2=v1+floor(((float)(x2-w1)*sx)+0.5);
x3=v1+floor(((float)(x3-w1)*sx)+0.5);
y1=v2+floor(((float)(y1-w2)*sy)+0.5);
y2=v2+floor(((float)(y2-w2)*sy)+0.5);
y3=v2+floor(((float)(y3-w2)*sy)+0.5);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
return 0;
}
Output of the window View Port
CS1355-Graphics and Multimedia Lab
WINDOW – VIEW PORT MAPPING algorithm Source code C Language Programming

read more "WINDOW – VIEW PORT MAPPING algorithm Source code C Language Programming"

QUICK SORT Data Structure lab programming Algorithm Quick Sort

Quick sort Programming Source code it has the header file quick.h it contains void swap(int *x,int *y),void display(int A[],int n),void insertionsort(int A[],int n),int median(int A[],int left,int right),void quicksort(int A[],int left,int right) by using these function and there variable we can use in the main function of the program
“Quick.h” file
QUICK SORT Data Structure lab programming Algorithm in c / c++QUICK SORT Data Structure lab programming Algorithm Quick Sort
#include<stdio.h>
#include<conio.h>
#define cutoff (3)
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
void display(int A[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",A[i]);
}
void insertionsort(int A[],int n)
{
int j,p,temp;
for(p=1;p<n;p++)
{
temp=A[p];
for(j=p;j>0&&A[j-1]>temp;j--)
A[j]=A[j-1];
A[j]=temp;
}
}
int median(int A[],int left,int right)
{
int center=(left+right)/2;
if(A[left]>A[center])
swap(&A[left],&A[center]);
if(A[left]>A[right])
swap(&A[left],&A[right]);
if(A[center]>A[right])
swap(&A[center],&A[right]);
if(A[left]<=A[center]<=A[right])
swap(&A[center],&A[right-1]);
return A[right-1];
}
void quicksort(int A[],int left,int right)
{
int i,j,pivot;
if(left+cutoff<=right)
{
pivot=median(A,left,right);
i=left;
j=right-1;
for(;;)
{
while(A[++i]<pivot)
{}
while(A[--j]>pivot)
{}
if(i<j)
swap(&A[i],&A[j]);
else
break;
}
swap(&A[i],&A[right-1]);
quicksort(A,left,i-1);
quicksort(A,i+1,right);
}
else
insertionsort(A+left,right-left+1);
}
“Quick.c” file
QUICK SORT Data Structure lab programming Algorithm in c c++
#include"quick.h"
void main()
{
int A[20],n,i;
clrscr();
printf("\n how many numbers do u want to sort:\t");
scanf("%d",&n);
printf("\n\n Enter the values");
for(i=0;i<n;i++)
{
printf("\n A[%d]\t",i);
scanf("%d",&A[i]);
}
quicksort(A,0,n-1);
display(A,n);
getch();
}
OUTPUT
QUICK SORT Data Structure lab Output in c c++
How many numbers do u want to sort: 5
Enter the values
A[0] 25
A[1] 10
A[2] 89
A[3] 65
A[4] 15
10 15 25 65 89

read more "QUICK SORT Data Structure lab programming Algorithm Quick Sort"

Flag counter

free counters