Sei sulla pagina 1di 21

PRACTICAL NO.

:-1

Q1. Write a C program that

i. Accepts a number of points n.


ii. Accepts a set S of n points in XY-plane.
iii. Accepts two points A and B on a Line L.
iv. Sort the given set of points of S w.r.t. line L.

PROGRAM:-

#include<stdio.h

#include<math.h>

void main()

int n,i;

float X1,Y1,X2,Y2,m,c,x[10],y[10];

clrscr();

printf("\n Enter the number of

points,n=");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\n Enter the point (x[%d],y[%d])=",i,i);

scanf("%f%f",&x[i],&y[i]);

printf("\n Enter the co_ordinates of the point A:(X1,Y1)");

scanf("%f%f",&X1,&Y1);

printf("\n Enter the co_ordinates of the point b:(X2,Y2) ");

scanf("%f%f",&X2,&Y2);

if((X2-X1)!=0)

m=(Y2-Y1)/(X2-X1);

c=Y1-m*X1;

printf("\n Slope of the line is m =%f",m);

printf("\n Y-intercept of th line is c%f",c)


printf("\n Equation of the line is,y = (%f)x+(%f)",m,c);

for(i=1;i<=n;i++)

if(y[i]==m*x[i]+c)

printf("\n The point (%f,%f) lies on the line",x[i],y[i]);

else

printf("\n The point(%f,%f) does not lie on the line",x[i],y[i]);

else

printf("\n Line is parallel to Y-axis");

printf("\n Equation of the line is, x =%f",X1);

for(i=1;i<=n;i++)

if(x[i]==X1)

printf("\n The point (%f,%f) lies on the line",x[i],y[i]);

else

printf("\n The point (%f,%f) does not lie on the line",x[i],y[i]);

getch();

OUTPUT:-

Enter the number of points,n=2

Enter the point (x[1],y[1])=2

Enter the point (x[2],y[2])=3

Enter the co_ordinates of the point A:(X1,Y1)5

Enter the co_ordinates of the point b:(X2,Y2) 7

8
Slope of the line is m =1.000000

Y-intercept of the line is c = 1.000000

Equation of the line is,y = (1.000000)x+(1.000000)

The point(2.000000,1.000000) does not lie on the line

The point (3.000000,4.000000) lies on the linePress any key to continue . . .

PRACTICAL NO. :-2


Q2. Write C-program that :

i. Accepts the number of points ‘n’ to sort.


ii. Accepts a set of n points in XY plane.
iii. Accepts the vertices of the rectangle with its sides parallel to co-ordinates axes.
iv. Sorts the given set of points with respect to the rectangle.

PROGRAM:-

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

int n,i;

float x[20],y[20],Xmin,Ymin,Xmax,Ymax;

printf("\n Enter the number of points,n=");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\n Enter the point (x[%d],y[%d])",i,i);

scanf("%f%f",&x[i],&y[i]);

}
do

printf("\n Enter the values of Xmin,Xmax");

scanf("%f%f",&Xmin,&Xmax);

}while(Xmin>=Xmax);

do

printf("\n Enter the valuesYmin,Ymax");

scanf("%f%f",&Ymin,&Ymax);

}
while(Ymin>=Ymax);

for(i=1;i<=n;i++)

if((Xmin<x[i] && x[i]<Xmax) && (Ymin<y[i] && y[i]<Ymax))

printf("\n The point (%f,%f) lies inside the rectangle",x[i],y[i]);

else if(Xmin>x[i] && x[i]>Xmax && Ymin>y[i] && y[i]>Xmax)

printf("\n The point (%f,%f) lies outside the rectangle",x[i],y[i]);

else

printf("\n The point (%f,%f) lies on the rectangle",x[i],y[i]);

return 0;

//getch();

OUTPUT:-

Enter the number of points,n=2

Enter the point (x[1],y[1])1

Enter the point (x[2],y[2])3

Enter the values of Xmin,Xmax5

Enter the values of Ymin,Ymax7

The point (1.000000,2.000000) lies on the rectangle

The point (3.000000,4.000000) lies on the rectanglePress any key to continue . . .


PRACTICAL NO. :-3
Q3. Find Farthest Distance
Write C-program that:

i. Accepts the number of points ‘n’.


ii. Accepts a set S of n points.
iii. Find a pair (or pairs) of the points in S which are farthest apart.

PROGRAM:-

#include<stdio.h>

#include<math.h>

main()

int i,j,n;

float x[20],y[20],d[20][20],max;

printf("\n Enter the numbers of point,n=");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\n Enter the point(x[%d],y[%d])=",i,i);

scanf("%f%f",&x[i],&y[i]);

for(i=1;i<n;i++)

for(j=i+1;j<=n;i++)

d[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]*y[j])-(y[i]*y[j]));

printf("\n Distance between the points (%f,%f) and (%f,%f) is, d[%d][%d]=%f",x[i],y[i],x[j],y[j],i,j,d[i][j]);

max=d[1][2];

for(i=1;i<n;i++)

for(j=i+1;j<=n;i++)
{

if(max<d[i][j])

max=d[i][j];

printf("\n The maximum distance is,max=%f",max);

for(i=1;i<n;i++)

for(j=i+1;j<+n;j++)

if(max==d[i][j])

printf("\n The points (%f,%f) and (%f,%f) are farthest apart with distance=%f",x[i],y[i],x[j],y[j],max);

return 0;

//getch();

OUTPUT:-

Enter the numbers of point,n=2

Enter the point(x[1],y[1])=1

Enter the point(x[2],y[2])=3

Distance between the points (1.000000,2.000000) and (3.000000,4.000000) is, d[1][2]=2.000000

Distance between the points (3.000000,4.000000) and (3.000000,4.000000) is, d[2][2]=0.000000

Distance between the points (0.000000,-0.000000) and (3.000000,4.000000) is, d[3][2]=3.000000


PRACTICAL NO. :-4
Q4. Least mutual distance

Write a C-program that :

i. Accepts the number of points ‘n’.


ii. Accepts a set S of n points.
iii. Find a pair (or pairs) of the points in S which are at least mutual distance.

PROGRAM:-

#include<stdio.h>

#include<math.h>

int main()

int i,j,n;

float x[20],y[20],d[20][20],min;

printf("\n Enter the number of points, n= ");

scanf("%d", &n);

for(i=1;i<=n;i++)

printf("\n Enter the point (x[%d],y[%d])=",i,i);

scanf("%f%f",&x[i],&y[i]);

for(i=1;i<n;i++)

for(j=i+1;j<=n;j++)

d[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));

printf("\n Distance between the points (%f,%f) and (%f,%f) is, d[%d][%d] = %f",
x[i],y[i],x[j],y[j],i,j,d[i][j]);

min=d[1][2];

for(i=1;i<n;i++)

for(j=i+1;j<=n;j++)
{

if(min>d[i][j])

min=d[i][j];

printf("\n The minimum distance is , min= %f",min);

for(i=1;i<n;i++)

for(j=i+1;j<=n;j++)

if(min==d[i][j])

printf("\n The points (%f,%f) and (%f,%f) are at least mutual distance = %f",
x[i],y[i],x[j],y[j],i,j,d[i][j]);

}
OUTPUT:-

Enter the number of points, n= 2

Enter the point (x[1],y[1])=1

Enter the point (x[2],y[2])=3

Distance between the points (1.000000,2.000000) and (3.000000,4.000000) is, d[1][2] = 2.828427

The minimum distance is , min= 2.828427

The points (1.000000,2.000000) and (3.000000,4.000000) are at least mutual distance = 0.000000Press any key to
continue . . .
PRACTICAL NO. :- 5
Q5. Sorting with respect to a Rectangular Box

Write a C-program that:

i. Accepts the number of points ‘n’ to sort.


ii. Accepts s set of points in 3 – dimensional space.
iii. Accepts the vertices of the 3 dimensional rectangular box with its faces parallel to co-ordinate
planes.
iv. Determine the points of the input set which are inside the given rectangular

box. PROGRAM: -

#include <stdio.h>

#include<math.h>

main()

int n,i;

float x[20],y[20],z[20],xmin,ymin,zmin,xmax,ymax,zmax;

printf("\n Enter the no of points ,n=");

scanf("%d",&n);

for(i=1;i<n;i++)

printf("\n Enter the points (x[%d],y[%d],z[%d])",i,i,i);

scanf("%f%f%f",&x[i],&y[i],&z[i]);

do

printf("\n Enter the values of xmin,xmax");

scanf("%f%f",&xmin,&xmax);

while(xmin>=xmax);

do

{
printf("\n Enter the values of ymin,ymax");

scanf("%f%f",&ymin,&ymax);

while(ymin>=ymax);

do

printf("\n Enter the values of zmin,zmax");

scanf("%f%f",&zmin,&zmax);

while(zmin>=zmax);

for(i=1;i<=n;i++)

if((xmin<x[i] && x[i]<xmax) && (ymin<y[i] && y[i]<ymax) && (zmin<z[i] && z[i]<zmax))

printf("\n The point (%f,%f,%f) lies inside the rectangular box",x[i],y[i],z[i]);

else if((xmin>x[i] && x[i]>xmax) && (ymin>y[i] && y[i]>ymax) && (zmin>z[i] && z[i]>zmax))

printf("\n The point (%f,%f,%f) lies outside the rectangular box",x[i],y[i],z[i]);

else

printf("\n The point (%f,%f,%f) lies on the rectangular box",x[i],y[i],z[i]);

OUTPUT:-

Enter the no of points ,n=2

Enter the points (x[1],y[1],z[1])1

Enter the values of xmin, xmax 4

Enter the values of ymin, ymax 6

7
Enter the values of zmin, zmax 1

Enter the values of zmin, zmax 8

The point (1.000000,2.000000,3.000000) lies on the rectangular box

The point
(190559127726044340000000000000000.000000,181924824779851750000000000000000.000000,840405.125000)
lies on the rectangular box Press any key to continue . . .
PRATCICAL NO. :-6
Q6. Sorting with respect to a Convex Polygon

Write a C-program that:

i. Accepts number of vertices of a convex polygon


ii. Accepts the co-ordinates of the vertices of the polygon
iii. Accepts a point Z in XY-plane
iv. Determines if Z is inside the Polygon

PROGRAM:-

#include<stdio.h>

#include<math.h>

#include<conio.h>

int main(void)

int i,n,flag,c1,c2,c3;

unsigned int cnt1,cnt2,cnt3;

float x[20],y[20],a,b,p,q;

do

printf("\n Enter the number of vertices, n=");

scanf("%d",&n);

while(n<3);

do

for(i=1;i<=n;i++)

printf("\n Enter x[%d],y[%d]",i,i);

scanf("%f%f",&x[i],&y[i]);

c1=0;

c2=0;

c3=0;
x[n+1]=x[1];

y[n+1]=y[1];

x[n+2]=x[2];

y[n+2]=y[2];

for(i=1;i<=n;i++)

a=((x[i+1]-x[i])*(y[i+2]-y[i+1])-(x[i+2]-x[i+1])*(y[i+1]-y[i]));

if(a>0)

c1++;

if(a<0)

c2++;

if(a==0)

c3++;

printf("\n c1=%d,c2=%d,c3=%d",c1,c2,c3); if((c1==n||

c2==n||(c1+c3)==n||(c2+c3)==n)&&(c3!=n))

printf("\n It is a convex polygon");

flag=0;

else

printf("\n it is not a convex polygon");

flag=1;

printf("\n Renter the vertices");

while(flag==1);

printf("\n Enter the points to sort (p,q)");

scanf("%f%f",&p,&q);

}
cnt1=0;

cnt2=0;

cnt3=0;

for(i=1;i<=n;i++)

b=((x[i]-p)*(y[i+1]-q)*(x[i+1]-p)*(y[i]-q));

if(b>0)

cnt1++;

if(b<0)

cnt2++;

if(b==0)

cnt3++;

if(cnt1==n||cnt2==n)

printf("\n The point (%f%f) is inside the convex polygon",p,q);

else if(cnt1+cnt3==n||cnt2+cnt3==n)

printf("\n The point (%f%f) is on the convex polygon",p,q);

else

printf("\n The point (%f%f) is outside the convex polygon",p,q);

return 0;

}
OUTPUT: -
PRACTICAL NO. :- 7(A)
Q7. Generation of circle:

Generate uniformly spaced n-points on the circle, x^2+y^2=r^2

7.(A) circle (closed curve)

PROGRAM:-

#include <stdio.h>

#include<math.h>

main()

{
int i,n;

float a,b,x[10],y[10],dt,t1,pi=3.142;

printf("\n Enter the number of points n=");

scanf("%d",&n);

printf("\n Enter the length of semi x-axis and semi y-axis,a and b=");

scanf("%f%f",&a,&b);

printf("\n Enter the intial angle t1=");

scanf("%f",&t1);

t1=t1*pi/180;

//printf("\n Enter the final angle,tn=");

//scanf("%f",&tn);

//tn=tn*pi/180;

x[1]=a*cos(t1);

y[1]=b*sin(t1);

printf("\n Initial point is (x[1],y[1])=(%f,%f)",x[1],y[1]);

dt=2*pi/n;

for(i=1;i<n;i++)

x[i+1]=x[i]*cos(dt)-y[i]*(a/b)*sin(dt);

y[i+1]=x[i]*(b/a)*sin(dt)-y[i]*cos(dt);
printf("\n (x[%d],y[%d])=(%f,%f)",i+1,i+1,x[i+1],y[i+1]);

OUTPUT:-

Enter the number of points n=2

Enter the length of semi x-axis and semi y-axis,a and b=4

Enter the intial angle t1=45

Initial point is (x[1],y[1])=(2.828139,3.535894)

(x[2],y[2])=(-2.826987,3.534454)Press any key to continue . . .

7.(A) circle (Open curve)

PROGRAM:-

#include <stdio.h>

#include<math.h>

main()

int i,n;

float r,x[10],y[10],dt,t1,tn,pi=3.142;

printf("\n Enter the number of points n=");

scanf("%d",&n);

printf("\n Enter the radius r=");

scanf("%f",&r);
printf("\n Enter the intial angle t1=");

scanf("%f",&t1);

t1=t1*pi/180;

printf("\n Enter the final angle,tn=");

scanf("%f",&tn);

tn=tn*pi/180;

x[1]=r*cos(t1);

y[1]=r*sin(t1);

printf("\n Initial point is (x[1],y[1])=(%f,%f)",x[1],y[1]);

dt=(tn-t1)/(n-1);

for(i=1;i<n;i++)

x[i+1]=x[i]*cos(dt)-y[i]*sin(dt);

y[i+1]=x[i]*sin(dt)-y[i]*cos(dt);

printf("\n (x[%d],y[%d])=(%f,%f)",i+1,i+1,x[i+1],y[i+1]);

OUTPUT: -

Enter the number of points n=2

Enter the radius r=45

Enter the intial angle t1=90

Enter the final angle,tn=45

Initial point is (x[1],y[1])=(-0.009164,45.000000)

(x[2],y[2])=(31.816566,-31.810083)Press any key to continue . . .

Q8. Generation of Ellipse:


PRACTICAL NO. :- 8(A)
Generate uniformly spaced n- points on the ellipse, (x²/a²)+(y²/b²) =1

8.(A) Ellipse (Closed curve):

PROGRAM:-

#include <stdio.h>

#include<math.h>

main()

int i,n;

float a,b,x[10],y[10],dt,t1,pi=3.142;

printf("\n Enter the number of points n=");

scanf("%d",&n);

printf("\n Enter the length of semi x-axis and semi y-axis,a and b=");

scanf("%f%f",&a,&b);

printf("\n Enter the intial angle t1=");

scanf("%f",&t1);

t1=t1*pi/180;

x[1]=a*cos(t1);

y[1]=b*sin(t1);

printf("\n Initial point is (x[1],y[1])=(%f,%f)",x[1],y[1]);

dt=2*pi/n;

for(i=1;i<n;i++)

x[i+1]=x[i]*cos(dt)-y[i]*(a/b)*sin(dt);

y[i+1]=x[i]*(b/a)*sin(dt)-y[i]*cos(dt);

printf("\n (x[%d],y[%d])=(%f,%f)",i+1,i+1,x[i+1],y[i+1]);

}
OUTPUT:-

Enter the number of points n=2

Enter the length of semi x-axis and semi y-axis,a and b=1

Enter the intial angle t1=45

Initial point is (x[1],y[1])=(0.707035,1.414358)

(x[2],y[2])=(-0.706747,1.413782)Press any key to continue . . .

8.(A) Ellipse (Open curve):

PROGRAM:-

#include <stdio.h>

#include<math.h>

main()

int i,n;

float a,b,x[10],y[10],dt,t1,tn,pi=3.142;

printf("\n Enter the number of points n=");

scanf("%d",&n);

printf("\n Enter the length of semi x-axis and semi y-axis,a and b=");

scanf("%f%f",&a,&b);

printf("\n Enter the intial angle t1=");

scanf("%f",&t1);
t1=t1*pi/180;

printf("\n Enter the final angle,tn=");

scanf("%f",&tn);

tn=tn*pi/180;

x[1]=a*cos(t1);

y[1]=b*sin(t1);

printf("\n Initial point is (x[1],y[1])=(%f,%f)",x[1],y[1]);

dt=(tn-t1)/(n-1);

for(i=1;i<n;i++)

x[i+1]=x[i]*cos(dt)-y[i]*(a/b)*sin(dt);

y[i+1]=x[i]*(b/a)*sin(dt)-y[i]*cos(dt);

printf("\n (x[%d],y[%d])=(%f,%f)",i+1,i+1,x[i+1],y[i+1]);

OUTPUT:-

Enter the number of points n=2

Enter the length of semi x-axis and semi y-axis,a and b=12

21

Enter the intial angle t1=45

Enter the final angle,tn=90

Initial point is (x[1],y[1])=(8.484417,14.850755)

(x[2],y[2])=(-0.002444,-0.000001)Press any key to continue . . .

Potrebbero piacerti anche