Help with a school project for calculating distance

I have to write a program that calculates the distance between two points using the distance formula. I am trying to use pow but I don't think that I am doing it correctly, so I could use some help. I tried pow using * and , but it still wasn't working for me.

Thanks

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
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>

using namespace std;

double distance(int x1, int x2, int y1, int y2);
double radius(int x1, int x2, int y1, int y2);
double circumference(double radius);
double area(double radius);

int main()
{
    int x1,x2,y1,y2;
    double rad, dis, cir, a;
  
    cout << "Enter the x-coordinate for the center of the circle: ";
    cin >> x1;
    cout << "Enter the y-coordinate for the center of the circle: ";
    cin >> y1;
    cout << "Enter the x-coordinate for the point on the circle: ";
    cin >> x2;
    cout << "Enter the y-coordinate for the point on the circle: ";
    cin >> y2;
  
    dis = distance(x1,x2,y1,y2);
    rad = radius(x1,x2,y1,y2);
    cir = circumference(rad);
    a = area(rad);
  
    cout << "\n\nThe distance between the two points is: " << dis << endl;
    cout << "The radius of the circle is: "<< rad << endl;
    cout << "The circumference of the circle is: " << cir << endl;
    cout << "The area of the circle is: "<< a << endl;
  
    return 0;
}

double distance(int x1, int x2, int y1, int y2)
{
    double dist = sqrt(pow(x2-x1),2)+ pow(y2-y1),2);
    return dist;
}

double radius(int x1, int x2, int y1, int y2)
{
    return distance(x1,x2,y1,y2);
}

double circumference(double rad)
{
    double pi = 3.1416;
    return (pi*rad*2);
}

double area(double rad)
{
    double pi = 3.1416;
    return (pi*rad*rad);
}
Hello Alex4321,

"math.h" is a C header and "cmath" if the C++ header file. Chances are the "cmath" will include the "math.h" header file. At least it did at one time.

"cmath" is all you need.

Your line of code sqrt(pow(x2-x1),2)+ pow(y2-y1),2). Right idea and it will work, but you are missing some ().

"sqrt" takes 1 parameter and the closing ) is around the first "pow". "pow" takes 2 parameters, but the () are only giving it 1 parameter and the + second "pow" is not included in the "sqrt" function.

Andy
Hello Alex4321,

1
2
3
4
5
6
7
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

constexpr double PI{3.1415926535};


And in your last 2 functions:
1
2
3
4
double circumference(double rad)
{
    return (PI * rad * 2);
}

This way you only have to define PI once and use it where needed.

Andy
Topic archived. No new replies allowed.