I'm new to c++, and fairly sure the error is in the variables I'm passing into the function. Essentially, I have two functions defined in a double_arrays.cpp file - first is to determine the Euclidian distance between two vectors (passed in as arrays with 10 values, this function works great). The other (closestPair) is to find which two vectors (again, each being defined as an array with 10 values) are closest in distance. When I am calling this function in my "main.cpp" file, I am getting a "no matching function called closestPair" error.
I am fairly sure that the error is either in the values I am passing into the function, or in the way I am trying to return it (by printing the values to the console).
DISCLAIMER - THIS IF FOR AN ASSIGNMENT :), so hints towards a solution will be more than welcome!
Here are my files:
main.cpp :
#include <iostream>
#include "double_arrays.h"
int main(int argc, const char * argv[])
{
//Define test values to test vectDistance
double first[10] = {
0.595500, 0.652927, 0.606763, 0.162761, 0.980752, 0.964772, 0.319322, 0.611325, 0.012422, 0.393489
};
double second[10] = {
0.416132, 0.778858, 0.909609, 0.094812, 0.380586, 0.512309, 0.638184, 0.753504, 0.465674, 0.674607
};
//call vectDistance with test values, should equal 1.056238
std::cout << "Euclidian distance is " << vectDistance(first, second) << std::endl;
std::cout << "Should equal ~1.056238" << std::endl;
//Define test values for closestPair
double a[10] = {
0.183963, 0.933146, 0.476773, 0.086125, 0.566566, 0.728107, 0.837345, 0.885175, 0.600559, 0.142238
};
double b[10] = {
0.086523, 0.025236, 0.252289, 0.089437, 0.382081, 0.420934, 0.038498, 0.626125, 0.468158, 0.247754
};
double c[10] = {
0.969345, 0.127753, 0.736213, 0.264992, 0.518971, 0.216767, 0.390992, 0.242241, 0.516135, 0.990155
};
//create 2D array to send to closestPair
double** test = new double*[3];
test[0] = a;
test[1] = b;
test[2] = c;
//output the values of the two vectors which are closest, in Euclidian distance
std::cout << closestPair(test) << std::endl;
return 0;
}
double_arrays.cpp :
#include "double_arrays.h"
#include <iostream>
#include <vector>
#include <math.h>
double vectDistance( double first[], double second[]) {
int i = 0;
double distance = 0.0;
for (int j = 0; j < 10; ++j) {
distance += pow((first[j] - second[i]), 2);
++i;
}
return distance = pow(distance, 0.5);
}
double** closestPair(double arrays[][10]) {
double** closest = new double*[2];
closest[0] = new double[10];
closest[0] = arrays[0];
closest[1] = new double[10];
closest[1] = arrays[1];
double minDistance = vectDistance(arrays[0], arrays[1]);
for (int i = 0; i < 9; ++i){
for (int j = i + 1; j < 10; ++j) {
if (vectDistance(arrays[i], arrays[j]) < minDistance) {
minDistance = vectDistance(arrays[i], arrays[j]);
closest[0] = arrays[i];
closest[1] = arrays[j];
}
}
}
return closest;
}
And, finally, the header file, header.h:
#ifndef double_arrays_double_arrays_h
#define double_arrays_double_arrays_h
double vectDistance( double first[], double second[]);
double** closestPair(double arrays[][10]);
#endif
const intinstead of having a lot of10s to modify if it changes. Also you surenew double*[3]matchesdouble arrays[][10]?std::coutdo with adouble**? (i.e. how should it print that?)