0

For some reason I get the error "No matching function call to 'OptimalBinarySearchTree'" on line 15. I am not sure if it has something to do with the way I am passing the array pointers or if I have left something off. I've never tried passing a 2D array before, so it may be messing it up.

#include <iostream>

using namespace std;

void OptimalBinarySearchTree(int n, int *P[n], int (*values)[n][n], int (*roots)[n][n]);    

int main()
{
    const int n = 18;
    char A[n] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R'};
    int P[n] = {995,22,23,562,33,8,60,118,30,723,807,626,15,89,21,128,626,621};
    int values[n][n];
    int roots[n][n];

    OptimalBinarySearchTree(n, P, values, roots);

    return 0;
}

void OptimalBinarySearchTree(int n, int *P[n], int (*values)[n][n], int (*roots)[n][n])
{
    for (int i = 1; i <= (n+1); i++)
    {
        (*values)[i][i] = 0;
    }
    for (int i = 1; i <= n; i++)
    {
        (*values)[i][i] = *P[i];
        (*roots)[i][i] = i;
    }

    for (int d = 1; d <= (n-1); d++)
    {
        for (int i = 1; i <= (n-d); i++)
        {
            int j = i + d;
            int sumP = 0;
            int minValue = 999999999;
            int minRoot = 0;

            for (int k = i; k <= j; k++)
            {
                sumP += *P[k];
                int value = (*values)[i][k-1] + (*values)[k+1][j];
                if (value < minValue)
                {
                    minValue = value;
                    minRoot = k;
                }
            }

            (*values)[i][j] = sumP + minValue;
            (*roots)[i][j] = minRoot;
        }
    }
};

Any help would be appreciated. Thanks,

joed4no
  • 1,124
  • 10
  • 16
  • Also, in `OptimalBinarySearchTree` you're trying to get access to the element outside of bounds. – awesoon Jul 06 '13 at 10:15
  • Soon, make your comment an answer so I can give you credit. The answer I was looking for was in the question you linked. Thanks. – joed4no Jul 08 '13 at 05:55

1 Answers1

0

You are adding a layer of indirection:

void OptimalBinarySearchTree(int n, int *P[n], int (*values)[n][n], int (*roots)[n][n]);    

int main()
{
    const int n = 18;
    char A[n] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R'};
    int P[n] = {995,22,23,562,33,8,60,118,30,723,807,626,15,89,21,128,626,621};
    int values[n][n];
    int roots[n][n];

    OptimalBinarySearchTree(n, P, values, roots);

    return 0;
}

The int *P[n] and int (*values)[n][n], etc means that your function takes an array of int pointers (P) and a two dimensional array of int pointers (called values). But you are passing an array of int values and a two-dimensional array of int values.

Remove the * and it should get better. However, I'm pretty sure it's either completely illegal or a compiler extension to pass int n and then use it for the dimensions of your values array. Since it's C++, you may want to consider using vector<int> and vector <vector <int> > instead.

Mats Petersson
  • 119,687
  • 13
  • 121
  • 204