0

# How do you write a program in C that calculates the area of a triangle given the three x and y coordinates of each corner of the triangle Alongside it must make use of structs.?

Updated: 10/19/2022

Wiki User

12y ago

To calculate the area of a triangle you need

1. to get the coordinates of the points of the triangles A = (xa, ya), etc (into a structure, eg user input and parsing, direct assignment in the program, etc)
2. calculate area = abs( (xb*ya - xa*yb) + (xc*yb - xb*yc) + (xa*yc - xc*ya) ) / 2
3. put the answer somewhere (eg onto stdout, in the struct for future use, etc).

Your structure would include (at a minimum) the coordinates of the points, eg:

struct triangle

{

int xa, ya;

int xb, yb;

int xc, yc;

};

then you would declare a structure to hold the points and use it to calculate the area, eg:

struct triangle t1;

t1.xa = 0; t1.ya = 0;

t1.xb = 5; t1.yb = 5;

t1,xc = 7; t1.yc = 7;

area = calculate_area(&t1);

To calculate the area, you could put it into a function:

calculate_area(tri)

struct triangle *tru;

{

area;

area = ()abs( (tri->xb * tri->ya - tri->xa * ... )) /* formula above */

return area;

}

I'll let you think about:

1. what the return type ought to be;
2. what type to use to store the triangle's points;
3. any potential error trapping that may need to be done;
4. what compiler options (and header files) you may need.

and put the program together.

Exactly what data you want to store in the struct (minimum the points, but you may want to store the area of the triangle as well - in future you could expand to include calculating the side lengths and angles)

Wiki User

12y ago

Earn +20 pts
Q: How do you write a program in C that calculates the area of a triangle given the three x and y coordinates of each corner of the triangle Alongside it must make use of structs.?
Submit
Still have questions?
Related questions

### How do you write a qbasic program to find area and circumerence of a circle?

Write a program that calculates the area of a triangle in QBASIC

### What are the answers for Programming logic and design chapter 6?

program calculates

### How do you write a c program which calculates the value of money at the end of each year of investment?

Reference:cprogramming-bd.com/c_page3.aspx#calculates the value of money

### Using while loop write a program which calculates the product of digits from 1 to 5 and also show these nos vertically?

Using while loop, write a program which calculates the product of digits from 1 to 5 and also show these no's vertically.

### Is a program with no input an incorrect program?

No, as long as it calculates something, displays something, or otherwise has an output that benefits the user, it is a program. It could be a program that makes a random number, or a program that tells the user a joke from a database.

rapid

### What is a computer program that coordinates all the activities of a computer hardware?

An operating system coordinates the activities of computer hardware

Operating system

yea

### How do you write a program that calculates the sum of the matrix elements given numbers?

ring me and ill explain - 086 22222222222222227 ring me

### Using a computer how do you create a program that calculates the circumference and area of a circle?

a circle has an area of 36pie square centimeters what is the circumference ?

### What does the computer program Md5sum do?

Md5sum is a program that verifies and calculates 128-bit MD5 hashes. The MD5 checksum is seen as the compact digital fingerprint that is unique to each file.