Guest User

Untitled

a guest
Nov 18th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <cmath>
  5.  
  6. #define FILENAME "lab2.txt"
  7.  
  8. using namespace std;
  9. int main(void)
  10. {
  11. double s_Distance = 1000000;
  12. int closest = 0;
  13. //Declare and initialize variables.
  14. double unknown[5], known[5];
  15. double distance(double hand_1[5],double hand_2[5]);
  16. int count=1;
  17. cout << "Please enter 5 measurments to compare fingers" << endl;
  18.  
  19. for (int i = 0; i < 5; i++)
  20. cin >> unknown[i];
  21.  
  22. ifstream lab;
  23. lab.open(FILENAME);
  24.  
  25. if (lab.fail())
  26. cout << "Error opening input file." << endl;
  27. else
  28. {
  29. while (!lab.eof())
  30. {
  31. for(int i = 0; i < 5; i++)
  32. lab >> known[i];
  33.  
  34. cout << "Known set " << count << ": ";
  35.  
  36. for (int i = 0; i < 5; i++)
  37. cout << known[i] << " ";
  38.  
  39. cout << endl;
  40.  
  41. if (s_Distance > distance(unknown,known))
  42. {
  43. s_Distance = distance(unknown,known);
  44. closest = count;
  45. }
  46.  
  47. // Compute and print distance.
  48. cout << "Distance : " << distance(unknown,known) << endl << endl;
  49. count++;
  50. }
  51. }
  52.  
  53. cout << "Shortest Distance : " << s_Distance << endl << endl;
  54. cout << "Known " << closest << " has best match" << endl << endl;
  55. // Exit program.
  56. return 0;
  57. }
  58. //-----------------------------------------------------------------
  59. // This function computes the distance between two hand measurements.
  60. double distance(double hand_1[5],double hand_2[5])
  61. {
  62. // Declare variables.
  63. int k;
  64. double sum=0;
  65. // Compute sum of absolute value differences.
  66. for (k=0; k<=4; k++)
  67. sum = sum + fabs(hand_1[k]-hand_2[k]);
  68. // Return distance value.
  69. return sum;
  70. }
Add Comment
Please, Sign In to add comment