These problems relate to the program developed in this section for comparing hand measurements.
1. Modify the program so that it reads the unknown measurement from the keyboard.
2. Modify the program so that it reads the known measurement from a data file and contains a loop that
compares the unknown to all measurements in the file.
3. Modify the program in problem 2 so that it also prints the minimum distance measurement.
4. Modify the program in problem 3 so that it prints the entry number with the minimum distance, as in
“Known 4 has best match.”
5. Modify the program in problem 4 so that it prints all entries with the minimum distance.
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
#define FILENAME "known.txt"
int main(void)
{
ifstream known;
known.open(FILENAME);
if (known.fail()){
cout << "Error opening input file." << endl;
}
//Declare and initialize variables.
int k=0;
double minimum1=100;
double minimum2=100;
double unknown[5], known1[5];
double distance(double hand_1[5],double hand_2[5]);
cout << "Enter the 5 unknown values:" << endl;
for(int b=0;b<5;b++){
cin >> unknown[b];
}
while(!known.eof()){
known >> known1[k];
k++;
}
for(int c=0;c<5;c++){
if(unknown[c]<minimum1){
minimum1 = unknown[c];
}
}
for(int d=0;d<5;d++){
if(known1[d]<minimum2){
minimum2 = known1[d];
}
}
if(minimum1<minimum2){
minimum2 = minimum1;
}
// Compute and print distance.
cout << "Distance: " << distance(unknown,known1) << endl;
cout << "minimum:" << minimum2 << endl;
known.close();
// 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 a;
double sum=0;
// Compute sum of absolute value differences.
for (a=0; a<=4; a++)
sum = sum + fabs(hand_1[a]-hand_2[a]);
// Return distance value.
return sum;
}
No comments:
Post a Comment