I have the following exercise to solve using structures: "Create a structure to store the coordinates of a point on a plane (x, y). Read from the keyboard the coordinates of three points in the plane and calculate the perimeter of the triangle they form."
This is my code, I was successful at calculating all the distances between the three points a,b,c: dab,dac, dbc but I failed calculating the perimeter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
struct coordinates {
float x;
float y;
};
float dist_ab(struct coordinates a, struct coordinates b){
float dist_ab=sqrtf(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
return dist_ab;
}
float dist_ac(struct coordinates a, struct coordinates c){
float dist_ac=sqrtf(pow(a.x - c.x, 2.0) + pow(a.y - c.y, 2.0));
return dist_ac;
}
float dist_bc(struct coordinates b, struct coordinates c){
float dist_bc=sqrtf(pow(b.x - c.x, 2.0) + pow(b.y - c.y, 2.0));
return dist_bc;
}
void perimeter_abc(float dist_ab, float dist_ac, float dist_bc){
float perimeter_abc=dist_ab+dist_ac+dist_bc;
}
int main()
{
struct coordinates a;
struct coordinates b;
struct coordinates c;
float dab, dac, dbc;
float sum_abc;
printf("Insert the coordinates of point a: \n");
scanf("%f %f", &a.x, &a.y);
printf("Insert the coordinates of point b: \n");
scanf("%f %f", &b.x, &b.y);
printf("Insert the coordinates of point c: \n");
scanf("%f %f", &c.x, &c.y);
dab=dist_ab(a,b);
dac=dist_ac(a,c);
dbc=dist_bc(c,b);
sum_abc=perimeter_abc(dist_ab,dist_ac,dist_bc);
printf("Distance between points a and b: %f\n", dab);
printf("Distance between points a and c: %f\n", dac);
printf("Distance between points b and c: %f\n", dbc);
printf("Perimeter of the triangle abc: %f\n", sum_abc);
return 0;
}
|
1) I created the structure coordinates to store the coordinates of a point in a plane (x,y)
2) I created three functions to calculate the distances between three points a,b,c: dist_ab,dist_ac,dist_bc
3) I created a fourth function to calculate the perimeter: perimeter_abc which is the sum of all distances: dist_ab+dist_ac+dist_bc
4) In int main()I read all the coordinates and print the results. I used the library math.h to use the functions sqrt and pow.
I get a warning and an error.
In line 25 I get the warning:
|25|warning: unused variable 'perimeter_abc' [-Wunused-variable]|
In line 46 I get the error:
|46|error: cannot convert 'float (*)(coordinates, coordinates)' to 'float' for argument '1' to 'void perimeter_abc(float, float, float)'|
Could you help me to fix it?