#include stdio.h
#include stdlib.h
#include math.h
#define MAX_VERT 50
enum {x, y};
typedef struct triangle
{
double v1[2];
double v2[2];
double v3[2];
} triangle;
double area(triangle a);
double perimeter(double *vertices, int size);
double side(double *p1, double *p2);
int main(void)
{
int n, idx;
int triangles;
int index;
int xycount;
double xy;
double triangle_area;
double polygon_area;
double perim;
double polygon_vertices[MAX_VERT] = {0.0};
triangle a;
FILE *data;
xycount = 0;
polygon_area = 0;
if((data = fopen("pvert.txt", "r")) == NULL)
{
fprintf(stderr, "can't open data file");
exit(EXIT_FAILURE);
}
/* Read x-y coordinates of the vertices
of the polygon from a file. */
while(fscanf(data, "%lf", &xy) == 1)
polygon_vertices[xycount++] = xy;
fclose(data);
idx = 0;
/* triangles in polygon = vertices - 2 */
triangles = (xycount / 2) - 2;
putchar('');
for(index = 2, idx = 0; idx < triangles; index += 2, ++idx)
{
/* Load vertices of a triangle into struct.
1st vertex of the polygon will be the 1st
vertex of each triangle. index holds the
starting index of each consecutive set of
triangle vertices after the 1st. */
a.v1[x] = polygon_vertices[0];
a.v1[y] = polygon_vertices[1];
a.v2[x] = polygon_vertices[index+0];
a.v2[y] = polygon_vertices[index+1];
a.v3[x] = polygon_vertices[index+2];
a.v3[y] = polygon_vertices[index+3];
/* calculate the area of the triangle */
triangle_area = area(a);
printf("area of triangle = %.2f
", triangle_area);
/* add triangle area to polygon area */
polygon_area += triangle_area;
}
printf("area of polygon = %.2f", polygon_area);
/* calculate the perimeter of the polygon */
perim = perimeter(polygon_vertices, xycount);
printf("perimeter of polygon = %.2f", perim);
return 0;
}
/* calculate triangle area with Heron's formula */
double area(triangle a)
{
double s1, s2, s3, S, area;
s1 = side(a.v1, a.v2);
s2 = side(a.v2, a.v3);
s3 = side(a.v3, a.v1);
S = (s1 + s2 + s3) / 2;
area = sqrt(S*(S - s1)*(S - s2)*(S - s3));
return area;
}
/* calculate polygon perimeter */
double perimeter(double *vertices, int size)
{
int idx, jdx;
double p1[2], p2[2], pfirst[2], plast[2];
double perimeter;
perimeter = 0.0;
/* 1st vertex of the polygon */
pfirst[x] = vertices[0];
pfirst[y] = vertices[1];
/* last vertex of polygon */
plast[x] = vertices[size-2];
plast[y] = vertices[size-1];
/* calculate perimeter minus last side */
for(idx = 0; idx <= size-3; idx += 2)
{
for(jdx = 0; jdx < 4; ++jdx)
{
p1[x] = vertices[idx];
p1[y] = vertices[idx+1];
p2[x] = vertices[idx+2];
p2[y] = vertices[idx+3];
}
perimeter += side(p1, p2);
}
/* add last side */
perimeter += side(plast, pfirst);
return perimeter;
}
/* calculate length of side */
double side(double *p1, double *p2)
{
double s1, s2, s3;
s1 = (p1[x] - p2[x]);
s2 = (p1[y] - p2[y]);
s3 = (s1 * s1) + (s2 * s2);
return sqrt(s3);
}
Subscribe to:
Post Comments (Atom)
History Of C..
In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.
My 1st Program...
#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t\"INFO COMPUTERS\"\n");
printf ("\t\t\t********************************");
getch ();
}
Next Step...
#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");
printf ("\t\t\t | 108,Sambhaji Nagar | \n\n");
printf ("\t\t\t | Lokhande Nagar. NGP-22| \n\n");
printf ("\t\t\t | Ph : 9373144662 | \n\n");
printf ("\t\t\t --------------------------- ");
}
For More Tutors Visite : www.igct.tk
Program for adding two number using different data types
#include
#include
void main ()
{
int ia,ib,ic;
unsigned int ua,ub,uc;
long la,lb,lc;
long unsigned lua,lub,luc;
float fa,fb,fc;
double lfa,lfb,lfc;
long double La,Lb,Lc;
clrscr ();
printf ("\n\t\t Program for adding two number using different data types");
printf ("\n\n\n ***Enter two number of Integer type***");
printf ("\n\n Enter first number->");
scanf ("%d",&ia);
printf ("\n\n Enter second number->");
scanf ("%d",&ib);
ic=ia+ib;
printf ("\n\n\t Addition = %d",ic);
printf ("\n\n\n ***Enter two number of Unsigned type***");
printf ("\n\n Enter first number->");
scanf ("%u",&ua);
printf ("\n\n Enter second number->");
scanf ("%u",&ub);
uc=ua+ub;
printf ("\n\n\t Addition = %u",uc);
printf ("\n\n\n ***Enter two number of long type***");
printf ("\n\n Enter first number->");
scanf ("%ld",&la);
printf ("\n\n Enter second number->");
scanf ("%ld",&lb);
lc=la+lb;
printf ("\n\n\t Addition = %ld",lc);
printf ("\n\n\n ***Enter two number of long Unsined type***");
printf ("\n\n Enter first number->");
scanf ("%lu",&lua);
printf ("\n\n Enter second number->");
scanf ("%lu",&lub);
luc=lua+lub;
printf ("\n\n\t Addition = %lu",luc);
printf ("\n\n\n ***Enter two number of Float type***");
printf ("\n\n Enter first number->");
scanf ("%f",&fa);
printf ("\n\n Enter second number->");
scanf ("%f",&fb);
fc=fa+fb;
printf ("\n\n\t Addition = %f",fc);
printf ("\n\n\n ***Enter two number of double type***");
printf ("\n\n Enter first number->");
scanf ("%lf",&lfa);
printf ("\n\n Enter second number->");
scanf ("%lf",&lfb);
lfc=lfa+lfb;
printf ("\n\n\t Addition = %lf",lfc);
printf ("\n\n\n ***Enter two number of long double type***");
printf ("\n\n Enter first number->");
scanf ("%Lf",&La);
printf ("\n\n Enter second number->");
scanf ("%Lf",&Lb);
Lc=La+Lb;
printf ("\n\n\t Addition = %Lf",Lc);
getch ();
}

0 comments:
Post a Comment