Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program that creates a struct Node, struct Edge, and struct Triangle.

ID: 3567505 • Letter: W

Question

Write a C program that creates a struct Node, struct Edge, and struct Triangle. A node will consist of the int values x and y which represent the x and y coordinates of the node. An edge contains two nodes and a double value weight where the weight is the Euclidean distance between the two nodes. A Triangle will consist of three edges. Your program must contain the following two functions

a) struct Triangle *constructTriangle(struct Node *a, struct Node *b, struct Node *c)
b) void calculateWeight(struct Edge * e)


ConstructTriangle should create a triangle with three edges formed from the three input nodes. Your program should calculate and populate the weight member of each edge.


CalculatWeight should calculate the weight of the given edge e and store it in the weight member.

You main function should create three nodes at (1,2), (1,3), and (2,2), pass them to constructTriangle in any order, and print out the two points coordinates and the weight of the three edges in the format below.

Write a C program that creates a struct Node, struct Edge, and struct Triangle. A node will consist of the int values x and y which represent the x and y coordinates of the node. An edge contains two nodes and a double value weight where the weight is the Euclidean distance between the two nodes. A Triangle will consist of three edges. Your program must contain the following two functions a) struct Triangle *constructTriangle(struct Node *a, struct Node *b, struct Node *c) b) void calculateWeight(struct Edge * e) ConstructTriangle should create a triangle with three edges formed from the three input nodes. Your program should calculate and populate the weight member of each edge. CalculatWeight should calculate the weight of the given edge e and store it in the weight member. You main function should create three nodes at (1,2), (1,3), and (2,2), pass them to constructTriangle in any order, and print out the two points coordinates and the weight of the three edges in the format below. Sample output:

Explanation / Answer

All structs are referred using pointer referece ->

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

struct Node{
int x,y;
};

struct Edge{
struct Node *start,*end;
double weight;
};

struct Triangle{
struct Edge *e1,*e2,*e3;
};

struct Triangle *constructTriangle(struct Node *,struct Node *,struct Node *);
void calculateWeight(struct Edge *);

int main(){

struct Node *node1 = malloc(sizeof(struct Node));
node1->x = 1;
node1->y = 2;

struct Node *node2 = malloc(sizeof(struct Node));
node2->x = 1;
node2->y = 3;

struct Node *node3 = malloc(sizeof(struct Node));
node3->x = 2;
node3->y = 2;

struct Triangle * theTriangle = constructTriangle(node1,node2,node3);

printf("Edge1: (%d,%d), (%d,%d), (%f)",theTriangle->e1->start->x,theTriangle->e1->start->y, theTriangle->e1->end->x,theTriangle->e1->end->y, theTriangle->e1->weight);


printf(" Edge2: (%d,%d), (%d,%d), (%f)",theTriangle->e2->start->x,theTriangle->e2->start->y, theTriangle->e2->end->x,theTriangle->e2->end->y, theTriangle->e2->weight);


printf(" Edge3: (%d,%d), (%d,%d), (%f)",theTriangle->e3->start->x,theTriangle->e3->start->y, theTriangle->e3->end->x,theTriangle->e3->end->y, theTriangle->e3->weight);

return 0;
}

struct Triangle *constructTriangle(struct Node *a,struct Node *b,struct Node *c){
struct Edge *edge1 = malloc(sizeof(struct Edge));
edge1->start = a;
edge1->end = b;
calculateWeight(edge1);

struct Edge *edge2 = malloc(sizeof(struct Edge));
edge2->start = b;
edge2->end = c;
calculateWeight(edge2);

struct Edge *edge3 = malloc(sizeof(struct Edge));
edge3->start = c;
edge3->end = a;
calculateWeight(edge3);

struct Triangle *tri = malloc(sizeof(struct Triangle));
tri->e1 = edge1;
tri->e2 = edge2;
tri->e3 = edge3;

return tri;
};

void calculateWeight(struct Edge *edge){
int x1,x2,y1,y2;

x1 = edge->start->x;
y1 = edge->start->y;

x2 = edge->end->x;
y2 = edge->end->y;

edge->weight = sqrt(pow(x2-x1,2) + pow(y2-y1,2));
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote