Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <cmath>
- #define FILENAME "lab2.txt"
- using namespace std;
- int main(void)
- {
- double s_Distance = 1000000;
- int closest = 0;
- //Declare and initialize variables.
- double unknown[5], known[5];
- double distance(double hand_1[5],double hand_2[5]);
- int count=1;
- cout << "Please enter 5 measurments to compare fingers" << endl;
- for (int i = 0; i < 5; i++)
- cin >> unknown[i];
- ifstream lab;
- lab.open(FILENAME);
- if (lab.fail())
- cout << "Error opening input file." << endl;
- else
- {
- while (!lab.eof())
- {
- for(int i = 0; i < 5; i++)
- lab >> known[i];
- cout << "Known set " << count << ": ";
- for (int i = 0; i < 5; i++)
- cout << known[i] << " ";
- cout << endl;
- if (s_Distance > distance(unknown,known))
- {
- s_Distance = distance(unknown,known);
- closest = count;
- }
- // Compute and print distance.
- cout << "Distance : " << distance(unknown,known) << endl << endl;
- count++;
- }
- }
- cout << "Shortest Distance : " << s_Distance << endl << endl;
- cout << "Known " << closest << " has best match" << endl << endl;
- // Exit program.
- return 0;
- }
- //-----------------------------------------------------------------
- // This function computes the distance between two hand measurements.
- double distance(double hand_1[5],double hand_2[5])
- {
- // Declare variables.
- int k;
- double sum=0;
- // Compute sum of absolute value differences.
- for (k=0; k<=4; k++)
- sum = sum + fabs(hand_1[k]-hand_2[k]);
- // Return distance value.
- return sum;
- }
Add Comment
Please, Sign In to add comment