create a structure called print to store x and y co-ordinate values write functions to get co-ordinate values .calculate the distance between two points in to display the result .write a main program to get the input from the user repeatedly and calculate the distance between every successive points and then find the total distance also
#include <iostream>
using namespace std;
struct point{
float x;
float y;
};
float calculateDistance(point point1,point point2){
return sqrt(pow((point2.x-point1.x),2)+pow((point2.y-point1.y),2));
}
int main() {
struct point points[100];
int numberPoints=0;
float totalDistance=0;
while(numberPoints<2){
cout<<"Enter number of points: ";
cin>>numberPoints;
}
for(int i=0;i<numberPoints;i++){
cout<<"Enter x co-ordinate for point "<<(i+1)<<": ";
cin>>points[i].x;
cout<<"Enter y co-ordinate for point "<<(i+1)<<": ";
cin>>points[i].y;
}
cout<<"\n";
for(int i=0;i<numberPoints-1;i++){
float distanceSuccessivePoints=calculateDistance(points[i],points[i+1]);
totalDistance+=distanceSuccessivePoints;
cout<<"The distance between successive points ("<<points[i].x<<","<<points[i].y<<"), ("<<points[i+1].x<<","<<points[i+1].y<<"): "<<distanceSuccessivePoints<<"\n";
}
cout<<"\nThe total distance: "<<totalDistance<<"\n";
cout<<"\n\n";
system("pause");
return 0;
}
Comments
Leave a comment