0

I'm working on a program to find pathways in undirected graphs, and I'm using a specific function to return a 2D array of ints. The problem is, though, that I can't figure out how to make one of the dimensions' size determined at run time. When I try to leave the function declaration as

int prim(int in[][3], int nsteps, int nedges);

I get this error:

error: incompatible types in assignment of ‘int’ to ‘int [(((sizetype)(((ssizetype)(nvertex + -1)) + -1)) + 1)][3]’
     ov = prim(v, nvertex-1, nedge);

Changing it to

int prim[][3](int in[][3], int nsteps, int nedges);

gives me this error instead:

p5.cpp:23:4: error: expected unqualified-id before ‘[’ token
int[][3] prim(int in[][3], int nsteps, int nedges);
   ^

I can't figure out for the life of me how to get this to work. I'd appreciate some help. Here's relevant snippets of the code:

# include <iostream>
# include <fstream>
# include <string>
using namespace std;

int crimMin(int in[][3], int nsteps, int nedges);

int crimMax(int in[][3], int nsteps, int nedges);

int prim(int in[][3], int nsteps, int nedges);

int main(int argc, char** argv){

Skipped most of main, this is the relevant portion

else if (x == cmax){
for (i = 0; i < nvertex-1; i++){
    if (v[i][2] == 1) v[i][2] = 0;
    else if (v[i][2] == 0) v[i][2] = 1;
}
ov = prim(v, nvertex-1, nedge);
for (i = 0; i < nvertex-1; i++){
    if (ov[i][2] == 1) ov[i][2] = 0;
    else if (ov[i][2] == 0) ov[i][2] = 1;
}
for (i = 0; i < nvertex-1; i++){
    cout << ov[i][0] << " " << ov[i][1] << " ";
    if (ov[i][2] == 1) cout << "C" << endl;
    else if (ov[i][2] == 0) cout << "W" << endl;
}
return 0;

Then later down at my definition...

int prim(int in[][3], int nsteps, int nedges){
    int step = 0, curr = 0;
    int i;
    int out[nsteps][3];
    int unvisited[nsteps];
    for (i = 0; i < nsteps; i++) unvisited[i] = 1;{
    //Building the output...
    }
    return out;
}
  • Why not using `std::array` or `std::vector` to make your life easier? – πάντα ῥεῖ Nov 29 '16 at 01:51
  • Your function `prim` returns an integer type which is 32bits or 4bytes on most x86 architectures. You define your `out` variable as a double array of integers. What do you think your compiler is going to return from this function? It isn't going to return an array of integers for it will only return 1 integer if your compiler will even compile. Also you are passing values from the function parameters and using them to create the array size. This isn't going to work either, under the Visual Studio 2015 C++ compiler this fails because `nsteps` does not evaluate to a constant expression or value. – Francis Cugler Nov 29 '16 at 02:13

0 Answers0