Help on my programming homework.

Let me just say this I need someone to explain to me what am doing wrong so I can better understand it.

I was wondering if someone can show me how to find the distance between two points? I have most of the code written but I'm horrible at math and I having problems plugging in formula in C++. So any help or pointer would be great!

Here is the question that they have asked me.

Write a program that calculates the distance between Link (pictured above) and various special items on the map. Your program will prompt the user for the position of Link (represented by an x, y pair), and then output the distance between the items below. This program requires you to implement the "distance between two points" formula you learned from Algebra class. When outputting the distance round to one decimal place.


Item Location
Sword (5.0, 3.0)
Heart (-3.125, 0)
Triforce (1.5, 8.1)




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;

double distance(double x1, double x2, double y1, double y2);

int main()
{
	double linkX, linkY;
	cout <<("== Zelda (version 0.000001) ==\n");
	cout <<("What is the position of our hero, Link?\n");
	cout <<("Enter X and Y coordinated separated by a space.\n");

	cin >> linkX >> linkY;

    cout <<("Sword is at (5.0, 3.0). Distance from Link:\n");
    cout <<("Heart is at (-3.125, 0). Distance from Link:\n");
    cout <<("Triforce is at (1.5, 8.1). Distance from Link:\n");

	return 0;
Distance Formula:

sqrt ( ( X2 - X1 )^2 + ( Y2 - Y1 )^2 )

Since you've included math.h, this can easily be written as:

double distance = sqrt ( pow ( x2 - x1, 2 ) + pow ( y2 - y1, 2 ) );
Thank you for that information, so when I enter this into the C++ I have to place it at the top before the brackets so It can run properly is that correct?
No, that code would go in your distance function. (I'm assuming your distance function returns the distance between two points.)
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
#include <iostream>
#include <cmath>
using namespace std;

double distance(double x1, double x2, double y1, double y2)
{
  return sqrt ((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
}

int main()
{
    double linkX, linkY;
    cout <<"== Zelda (version 0.000001) ==\n";
    cout <<"What is the position of our hero, Link?\n";
    cout <<"Enter X and Y coordinated separated by a space.\n";

    cin >> linkX >> linkY;

    cout <<"Sword is at (5.0, 3.0). Distance from Link: " 
         << distance(linkX, 5.0, linkY, 3.0) << "\n";
    cout <<"Heart is at (-3.125, 0). Distance from Link: " 
         << distance(linkX, -3.125, linkY, 0) << "\n";
    cout <<"Triforce is at (1.5, 8.1). Distance from Link: " 
         << distance(linkX, 1.5, linkY, 8.1) << "\n";

	return 0;
}
Last edited on
Yea sorry I'm not following on what you mean. So I have to plug it in where it says

cout <<("Sword is at (5.0, 3.0). Distance from Link:\n"); Because I have no idea how to plug in the numbers I'm new to C++ and this is my second week in this class.
@Stewbond

I really thank you for that but I really didn't learn anything by you doing it for me. Don't get me wrong I'm happy but I'm trying to understand what I was doing wrong and understand what was my next step to take to solve this.

Thank you tho... I'm still going to try and understand this.
Stewbond put it in the right place. Now you can call the function "distance" with distance ( firstXValue, secondXValue, firstYValue, secondYValue ). When you call this function, it will return the distance between the two points you gave it, as a double. So, cout << distance ( ... ) will print the distance between two objects.
@Stewbond & @Yay295

Thank you for the help I was able to finish it and turn it in before midnight.

You guys rock. :)
Topic archived. No new replies allowed.