Day 21 Homework: Intro to C++
c++ program that reads hand lengths from a file and compares to the users input
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
double distance(double *hand_1, double *hand_2);
int main(void)
{
ifstream data;
//Declare and initialize variables.
double unknown[5] , known[5], temp, min = 0;
int size = 1, *match;
match = new int[size];
data.open("hand_lengths.txt");
cout << "Enter Unknown Hand Lengths: ";
for (int k = 0; k <= 4; k++)
{
cin >> unknown[k];
}
for (int counter = 1; !data.eof(); counter++)
{
for (int k = 0; k <= 4; k++)
{
data >> known[k];
}
temp = distance(unknown, known);
if (temp < min || counter == 1)
{
min = temp;
match = new int[1];
match[0] = counter;
size = 1;
}
else if (temp == min)
{
size++;
int *tempArr = match;
match = new int[size];
for (int i = 0; i < size - 1; i++)
match[i] = tempArr[i];
delete[]tempArr;
match[size - 1] = counter;
}
}
data.close();
// Compute and print distance.
cout << "Minimum Distance: " << min << " is closest to known: " ;
for(int i = 0; i < size ; i++)
cout << match[i] << " ";
cout << endl;
// Exit program.
delete[] match;
system("Pause");
return 0;
}
//-----------------------------------------------------------------
// This function computes the distance between two hand measurements.
double distance(double *hand_1, double *hand_2)
{
// 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;
}
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
double distance(double *hand_1, double *hand_2);
int main(void)
{
ifstream data;
//Declare and initialize variables.
double unknown[5] , known[5], temp, min = 0;
int size = 1, *match;
match = new int[size];
data.open("hand_lengths.txt");
cout << "Enter Unknown Hand Lengths: ";
for (int k = 0; k <= 4; k++)
{
cin >> unknown[k];
}
for (int counter = 1; !data.eof(); counter++)
{
for (int k = 0; k <= 4; k++)
{
data >> known[k];
}
temp = distance(unknown, known);
if (temp < min || counter == 1)
{
min = temp;
match = new int[1];
match[0] = counter;
size = 1;
}
else if (temp == min)
{
size++;
int *tempArr = match;
match = new int[size];
for (int i = 0; i < size - 1; i++)
match[i] = tempArr[i];
delete[]tempArr;
match[size - 1] = counter;
}
}
data.close();
// Compute and print distance.
cout << "Minimum Distance: " << min << " is closest to known: " ;
for(int i = 0; i < size ; i++)
cout << match[i] << " ";
cout << endl;
// Exit program.
delete[] match;
system("Pause");
return 0;
}
//-----------------------------------------------------------------
// This function computes the distance between two hand measurements.
double distance(double *hand_1, double *hand_2)
{
// 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;
}
Comments
Post a Comment