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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
/*
Program prompts user to enter two points in the form (x1, y1) and (x2, y2) and finds the distance between the two points using a function.
Algorithm steps:
1. Define a function called findDistance (...) that takes four parameters x1, y1 and x2, y2 as two points.
a. finds the distance between them using the equation: sqrt//FIX ME sqrt symbol ((x2-x1)^2 + (y2-y1)^2)
b. returns the calculated distance value
2. Prompt user to enter four numbers
3. Convert the values into float and store them into variables
4. Call function getDistance by passing 4 entered numbers as arguments
5. Display results with proper description. Format output numbers to 2 decimal points.
6. Test and validate that program output is correct for a given set of input points.
*/
#include <iostream>
#include <cstdio>
#include <cassert>
#include <cmath>
using namespace std;
const double epsilon = 1e-6; // 0.000001 accuracy upto 6 decimal points
// function prototypes
// function that calculates the distance between two points
// x1, y1 and x2, y2 and returns the calculated value double findDistance (int, int, int, int);
// test function that runs automated testing void test();
double findDistance(int x1, int y1, int x2, int y2)
{
//int x1, y1, x2, y2; // can not get rid of c4700
//x1 = 0;
//y1 = 0;
//x2 = 0;
//y2 = 0;
//cout << sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2) << endl;
cout << sqrt(pow(x2 - x1, 2) + (y2 - y1, 2)) << endl;
//FIXME - Find the distance between (x1, y1) and (x2, y2)
// following the algorithm step 1
// return the calculated distance
return 0.000000;
}
void test()
{
assert(findDistance(4, 3, 5, 1) - 2.23607 <= epsilon);
assert(findDistance(3, 5, 7, 0) - 6.40312 <= epsilon);
assert(findDistance(-1, 2, 4, 18) - 16.76305 < epsilon);
// FIXME - add atleast two more test cases
cout << "all tests passed..." << endl;
}
int main()
{
double distance;
int x1, y1, x2, y2; // variables to store two points (x1, y1) and (x2, y2)
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
char ch;
distance = findDistance(x1, y1, x2, y2);
//for (int main = 1; main(findDistance) <= 10;)
system("cls");
cout << "Program calculates distance between 2 points on a 2d Coordinates." << endl;
cout << "Enter a point in the form (x, y) : ";
// parse the input stream
cin >> ch >> x1 >> ch >> y1 >> ch; // value stored in ch is ignored
printf(" (x1, y1) = (%d, %d)\n", x1, y1);
cout << "Enter a second point in the form (x, y): ";
cin >> ch >> x2 >> ch >> y2 >> ch; // value store in ch is ignored
printf(" (x2, y2) = (%d, %d)\n", x2, y2);
//FIXME - Read/parse the second point and store data into variables x2 and y2 //fixed
test();
//FIXME - Call test function // fixed
//findDistance(x1, y1, x2, y2);
//FIXME - call findDistance function passing proper arguments //
printf("The distance between the 2 points = ", distance);
cout << findDistance(x1, y1, x2, y2) << endl;
//FIXME - using printf function display the returned distance with proper description
cin.ignore(1000, '\n');
cout << "Good Bye!" << endl;
cin.get();
return 0;
}
| |