Need help with structures, calculate perimeter of 3 given points

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?
Last edited on
sum_abc = perimeter_abc(dist_ab, dist_ac, dist_bc);
The return type of perimeter_abc is void so you can't assign it to a variable

As a general rule you should use double instead of float, because they have higher precision.
pow returns a double so when you pass it to sqrf as a float you might get incorrect results.

1
2
3
4
5
6
7
8
9
10
11
12
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;
}


The function could be condensed into one function and called with the different coordinates.:
 
double coord_distance(struct coordinates b, struct coordinates c)


Hope this makes any sense.
Thank you Thomas. I modified the code and removed the function to sum distances. I calculate distances directly in the last printf instead, so much easier!. My code with your recommendations:

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

struct coordinates {
    double x;
    double y;
};

double dist_ab(struct coordinates a, struct coordinates b){
    double dist_ab=sqrtf(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
    return dist_ab;
}
double dist_ac(struct coordinates a, struct coordinates c){
    double dist_ac=sqrtf(pow(a.x - c.x, 2.0) + pow(a.y - c.y, 2.0));
    return dist_ac;
}
double dist_bc(struct coordinates b, struct coordinates c){
    double dist_bc=sqrtf(pow(b.x - c.x, 2.0) + pow(b.y - c.y, 2.0));
    return dist_bc;
}

int main()
{
    struct coordinates a;
    struct coordinates b;
    struct coordinates c;
    double dab, dac, dbc;
    printf("Insert the coordinates of point a: \n");
    scanf("%lf %lf", &a.x, &a.y);
    printf("Insert the coordinates of point b: \n");
    scanf("%lf %lf", &b.x, &b.y);
    printf("Insert the coordinates of point c: \n");
    scanf("%lf %lf", &c.x, &c.y);

    dab=dist_ab(a,b);
    dac=dist_ac(a,c);
    dbc=dist_bc(c,b);
    printf("Distance between points a and b: %lf\n", dab);
    printf("Distance between points a and c: %lf\n", dac);
    printf("Distance between points b and c: %lf\n", dbc);
    printf("Perimeter of the triangle abc: %lf\n", dab+dac+dbc);

    return 0;
}


Maybe there is a more "elegant" and better "academical" manner to solve the sum of all the distances but now my code works. Give me your feedback. What do you think?
your 3 functions are doing exactly the same thing!
the first calculates the distance between a and b
the second calculates the distance between a and c
the third calculates the distance between b and c
a, b, c are just labels. Each function simply calculates the (Euclidean) distance b/w 2 coordinates instances. Try it and see
I understand. I am fairly new to this. You say to merge all the three functions in one. If I do that, what I return?, what I put in double(struct, struct)? I get the idea, but I am not able to follow you.

double dist_ab(struct coordinates a, struct coordinates b){
double dist_ab=sqrtf(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
return dist_ab;
}

The bolded text is what worry me. I have dist_ab, dist_ac and dist_bc. I don't know what to return and what to put in double....
you don't need to change much, just get rid of the 2 superfluous functions and keep any one of the three. then you could also given it a more generic name like distanceCoords for eg. (avoid outright distance() because that name is also used elsewhere in CPP)
ok, what I return instead of return dist_ab?
You just return a double.
1
2
3
4
5
double distCoords(struct coordinates b, struct coordinates c)
{
    double  ret = sqrtf(pow(b.x - c.x, 2.0) + pow(b.y - c.y, 2.0));
    return ret;
}
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
#include <iostream>
#include <cmath>

using namespace std;

struct coordinates {
    double x = 0;
    double y = 0;
};
double dist_Coord(const coordinates& a, const coordinates& b){
    return std::pow((std::pow(a.x - b.x, 2.0) + std::pow(a.y - b.y, 2.0)),0.5);
}

int main()
{
    coordinates a{}, b{}, c{};

    std::cout << "Insert the coordinates of point a: \n";
    std::cin >> a.x >> a.y; //input validation recommended
    //use std::cout, std::cin instead of printf(), scanf() which are more C than CPP
    std::cout << "Insert the coordinates of point b: \n";
    std::cin >> b.x >> b.y;
    std::cout << "Insert the coordinates of point c: \n";
    std::cin >> c.x >> c.y;

    double dab = dist_Coord(a,b);
    //could also use auto specifier when you become familiar with it
    double dac = dist_Coord(a,c);
    double dbc = dist_Coord(b,c);

    std::cout << "Distance between points a and b: " << dab << "\n";
    std::cout << "Distance between points a and c: " << dac << "\n";
    std::cout << "Distance between points b and c: " << dbc  << "\n";
    std::cout << " Perimeter of the triangle abc: " << dab + dac + dbc << "\n";

    return 0;

}
Thank you gunner. Let's give a try.
I have the solution with the code I have been working (sorry to say the course is about C/C++ but we are compiling our codes with C++ but with C syntaxis, I understand if you complain against me because of using C code)

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

struct coordinates {
    double x;
    double y;
};

double dist_coord(struct coordinates a, struct coordinates b){
   double dist_coord=sqrtf(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
   return dist_coord;
}

int main()
{
    struct coordinates a,b,c;

    printf("Insert the coordinates of point a: \n");
    scanf("%lf %lf", &a.x, &a.y);
    printf("Insert the coordinates of point b: \n");
    scanf("%lf %lf", &b.x, &b.y);
    printf("Insert the coordinates of point c: \n");
    scanf("%lf %lf", &c.x, &c.y);

    double dab=dist_coord(a,b);
    double dac=dist_coord(a,c);
    double dbc=dist_coord(c,b);

    printf("Distance between points a and b: %lf\n", dab);
    printf("Distance between points a and c: %lf\n", dac);
    printf("Distance between points b and c: %lf\n", dbc);
    printf("Perimeter of the triangle abc: %lf\n", dab+dac+dbc);

    return 0;
}
Topic archived. No new replies allowed.