|
|
||||
|
|
This snippet submitted by prog-bman on 2005-01-26. It has been viewed 15211 times.
Rating of 2.6964 with 112 votes Distance#include <iostream>
#include <cmath>
#include <string>
/*
This program takes 2 x,y coords and calculates the distance between them.
I wrote this cause I was getting tired of doing the math on paper for my
college algebra class :).
There is no input validation but I'll leave that up to anyone who wants to use it.
*/
void getValues(double &x, double &x1, double &y, double &y1);
void getDistance(const double x, const double x1, const double y, const double y1);
int main(void)
{
double x;
double x1;
double y;
double y1;
bool exit = false;
std::string exitletter;
while(!exit)
{
getValues(x,x1,y,y1);
getDistance(x,x1,y,y1);
std::cout<<"Another? (y or n)"<<std::endl;
std::cin>>exitletter;
if(exitletter == "n")
{
exit = true;
}
}
return 0;
}
void getValues(double &x, double &x1, double &y, double &y1)
{
std::cout<<"Enter x"<<std::endl;
std::cin>>x;
std::cout<<"Enter y"<<std::endl;
std::cin>>y;
std::cout<<"Enter x2"<<std::endl;
std::cin>>x1;
std::cout<<"Enter y2"<<std::endl;
std::cin>>y1;
}
void getDistance(const double x, const double x1, const double y, const double y1)
{
double distance;
double preSqrtValue;
preSqrtValue = ( pow( (x1 - x), 2) + pow( (y1 - y), 2) );
distance = sqrt( ( pow( (x1 - x), 2) + pow( (y1 - y), 2) ) );
std::cout<<"Before taking the square root "<<preSqrtValue<<std::endl;
std::cout<<"Distance equals "<<distance<<std::endl;
}
More snippets Add a snippet! ----- |
|
||
|
|
||||