// Write a main()function that declares several integer, double, and City and Planet objects,
// and uses the calcDistance()function to compute the distance for several pairs. Save the
// file as Distance.cpp.
#include <iostream>
#include <cmath>
double calcDistance(double dX0, double dY0, double dX1, double dY1)
{
return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0));
}
int main()
{
using namespace std;
char city1[] = "New York";
int x1 = 2;
int y1 = 7;
char city2[] = "Chicago";
double x2 = 4.0;
double y2 = 5.0;
// Calculate two distances
cout << "Distance = " << calcDistance(0.0, 0.0, 0.0, 1.0) << endl; // check
cout << "Distance from "<< city1 <<" to " <<city2 << " = "<< calcDistance(x1, y1, x2, y2) << endl;
char planet1[] = "Earth";
int X1 = 4;
int Y1 = 11;
char planet2[] = "Sun";
double X2 = 6.0;
double Y2 = 111.0;
cout << "Distance from "<< planet1 <<" to " <<planet2 << " = "<< calcDistance(X1, Y1, X2, Y2) << endl;
system("PAUSE");
return 0;
}
Comments
Leave a comment