Day 22 Nov 14

For homework we had to modify the hand recognition code to read the knowns from a data file, create an array of unknowns from user input, point to the known value with the smallest difference, and print it all out.

Here's the code
#include <iostream>
#include <cmath>
#include <fstream>
//#define FILENAME "known.txt"

using namespace std;
int main(void)
{
double known[5];

ifstream myfile ("known.txt");
  if (myfile.is_open())
  {
    int k;
    for(k=0;k<=4;k++)
{
myfile>>known[k];
}
    myfile.close();
  }




//Declare and initialize variables.
//double unknown[5]={5.4,7.2,7.9,7.4,5.1};
double unknown[5];
//double known[5]={6.2,7.0,8.0,7.4,5.8};
cout<<"enter 5 unknown values"<<endl;
int k;
for(k=0;k<=4;k++)
{

cin>>unknown[k];

}

double distance(double hand_1[5],double hand_2[5]);
double comp(double hand_1[5],double hand_2[5]);
// Compute and print distance.
cout << "Distance: " << distance(unknown,known) << endl;
cout<<"The smallest distance is: "<<comp(unknown,known) <<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;
}

double comp(double hand_1[5],double hand_2[5])
{
double min;
double comp;
int k;
double val;
for(k=0;k<=4;k++)
{
comp=hand_1[k]-hand_2[k];
min=hand_1[0]-hand_2[0];
if(comp<min)
min=fabs(comp);
val=k;

}
for(k=0;k<=4;k++)
{
if(k==val)
cout<<"known "<<k<<" has the best match"<<endl;
}
return min;
}

Here's a screen shot of the code working

Comments

Popular posts from this blog

Day 5

Day 21 Nov 9