This program is to find the area of a triangle using three vertices. It compiles but I get these warnings in the main function:
37:21: warning: variable 'aTriangle' set but not used [-Wunused-but-set-variable]45:7: warning: 'area' is used uninitialized in this function [-Wuninitialized]
I get the first one at the struct triangle aTriangle declaration and the second at cout area. The output is 0. I don't understand why.
Code:
//-----Include required headers here-----
#include<iostream>
#include<cmath>
using namespace std;
//-----End of headers-----
//-----Don't change/delete structs-----
struct vertex{
float x;
float y;
};
struct triangle{
vertex vertices[3];
};
//-----Structs end here
//-----Add new functions here(if any)-----
//-----New functions end here-----
float cal_area(triangle aTriangle) {
//Write your solution below this line
float s,area,a,b,c;
a=sqrt(pow((aTriangle.vertices[1].x-aTriangle.vertices[2].x),2)-pow((aTriangle.vertices[1].y-aTriangle.vertices[2].y),2));
b=sqrt(pow((aTriangle.vertices[2].x-aTriangle.vertices[3].x),2)-pow((aTriangle.vertices[2].y-aTriangle.vertices[3].y),2));
c=sqrt(pow((aTriangle.vertices[1].x-aTriangle.vertices[3].x),2)-pow((aTriangle.vertices[1].y-aTriangle.vertices[3].y),2));
s=0.5*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
return (area);
//Dont write below this line
}
int main()
{
struct triangle aTriangle;
aTriangle.vertices[1].x=1;
aTriangle.vertices[2].x=2;
aTriangle.vertices[3].x=5;
aTriangle.vertices[1].y=4;
aTriangle.vertices[2].y=5;
aTriangle.vertices[3].y=20;
float cal_area(triangle aTriangle);
cout<<area<<endl;
return 0;
}