Distance between 2 points - multiple times

0 cheeky carter · January 4, 2015
as part of my computer coursework, I ask the user to input the number of corners a fence will have (between 3 and 10) and ask them to input the co-ordinates. i then have to find the distance of each side. How do i do this? I know how to find the distance between 2 points but how do i get it to keep calculating the distance?
#include<iostream>

using namespace std;


int main(){

int corners = 0;    
double x[10], y[10]; 
double dist; 
double Distance;

//enter no. of corners
while (corners < 3 || corners>10){
cout << "Please enter the number of corners, (between 3 and 10) of the fence:" << endl;
cin >> corners;
}

//Ask user to input co-ordinates of the corners 
for (int i = 0; i < corners; i++){

cout << "Please input positive x and y values for corner " << i + 1 << endl;
cin >> x >> y;
}

//Calculate distance
int j = corners - 1;

for (int i = 0; i < corners; i++){
dist = (x - x)*(x - x) + (y - y)*(y - y);
j = i;
        Distance = sqrt(dist);
}

//Print result
cout << "Distance between these points = " << Distance << " metres." << endl;

return 0;
}


Post a Reply

Replies

Oldest  Newest  Rating
0 Stan Botha · January 5, 2015
#include<iostream>
#include<cmath>
using namespace std;

int main(){

int corners = 0;
double x[10], y[10];
double dist;

//enter no. of corners
while (corners < 3 || corners>10)
{
    cout << "Please enter the number of corners, (between 3 and 10) of the fence:" << endl;
cin >> corners;
}

//Ask user to input co-ordinates of the corners
for (int i = 0; i < corners; i++){

cout << "Please input positive x and y values for corner " << i + 1 << endl;
cin >> x >> y;
}

//Calculate distance
int j = corners - 1;

for (int i = 0; i < corners; i++){
if(i<j)
{
               dist = sqrt((pow((x[i+1]-x),2) + pow((y[i+1]-y),2)));
               cout << "Distance of length " << i+1 << " = " << dist << "meters" << endl;
}
else
{
    dist = sqrt((pow((x-x[0]),2) + pow((y-y[0]),2)));
               cout << "Distance of length " << i+1 << " = " << dist << "meters" << endl;
}
}

return 0;
}
0 Stan Botha · January 5, 2015
for some reason, inserting the code above some information got lost.
/images/forum/upload/2015-01-05/0524ce89fc68efa2d221ccd62774d489.PNG
0 cheeky carter · January 5, 2015
thank you very much!!
  • 1

C++

107,340 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator