-1

I have been trying to use an 2D array as Reference into a function. Thanks for the help.

#include <iostream>
using namespace std;
void dfs(int *G[],int i,int *visited,int size) {
    visited[i]=1;
    int j;
    for(j=0;j<size;j++) {
        if(!visited[j]&& G[i][j] == 1)
            dfs(G,j,visited);
    }
}
mello
  • 75
  • 1
  • 8

2 Answers2

1

The issue is in the signature of your dfs function

void dfs(int *G[],int i,int *visited)

It takes a pointer to pointer to int. You are however passing an array to it

dfs(Array_From_file, 0, visited);

where Array_From_file is declared as

int Array_From_file[ROWS][COLUMNS];

Such conversion is not possible. Quick fix: change the signature of the function to:

void dfs(int G[][COLUMNS],int i,int *visited)

Better, use a std::vector<std::vector<int>> that you pass by reference. Here is an example:

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int const COLUMNS = 100;
int const ROWS = 100 ;
typedef std::vector<std::vector<int>> int_mat;

void dfs(const int_mat& G, int i, vector<int>& visited) {
    int size = ROWS * COLUMNS ;
    visited[i] = 1;
    for (int j = 0; j < size; j++) {
        if (!visited[j] && G[i][j] == 1)
            dfs(G, j, visited);
    }
}
mello
  • 75
  • 1
  • 8
vsoftco
  • 52,188
  • 7
  • 109
  • 221
  • so i should change the void pdfs (int G[][],int i , int *visitded) – mello Apr 05 '15 at 18:40
  • you should either change the signature to `void(int G[][COLUMNS], int, int*)` (you have to specify the second dimension), or use `int** Array_from_file`. – vsoftco Apr 05 '15 at 18:42
  • so i should take the int size variable into the function and put a nested for loop with the const max right – mello Apr 05 '15 at 18:47
  • @mello, yes, loop as you'd do over any bi-dimensional array, with a nested loop. – vsoftco Apr 05 '15 at 18:50
  • @mello see the updated edit for a modification of your code so it uses `std::vector`. – vsoftco Apr 05 '15 at 19:01
-1

I believe your (heavily edited) fix is this:

int one_to_two(int* a, int x, int y)
{
    return a[x*ROWS+y];
}
void dfs(int** G,int i,int *visited) {
    int size = ROWS * COLUMNS ;
    visited[i]=1;
    int j;
    for(j=0;j<size;j++) {
        if(!visited[j]&& one_to_two(G,i,j) == 1)
            dfs(G,j,visited);
    }
}

That being said, I don't think that this is a well-structured method of solving this problem. I don't know why you're using recursion to do this, an iterative loop would be much simpler. This method has the possibility to blow-up the stack. You are NOT using tail recursion here, so this method will cause memory problems if ROWS or COLUMNS gets very large.

I apologize for my screw-ups, the typing didn't work quite the way I thought it did. This will require casting your 2-d array to an int pointer before sending it into the function. I'm not sure I like this solution, its not very elegant, but it should at least compile.

On another note, not quite sure what you're trying to do with depth-first search, but I don't think this algorithm does quite what you you think it does (for one, malloc doesn't zero the memory it allocates, which is of particular concern for this algorithm)

  • this declares `G` as an array of references, which is an error (caught at compile time) – vsoftco Apr 05 '15 at 18:48
  • that just happened ... the program crashed .. DO you have any subjections ? Please – mello Apr 05 '15 at 18:49
  • Just changed it. I thought the reference would work, it does not. The double pointer should do the trick though. – Jonathan Bedard Apr 05 '15 at 18:51
  • @JonathanBedard it will still not work, `int G[][]` is the same as `int** G`, the problem is that you pass an array to it. It works only if you pass a double pointer, or change the signature to `int G[][COLUMNS]` – vsoftco Apr 05 '15 at 18:52
  • Ok, so everything I said before was fantastically messed up. Posting one that will definitely compile, although is a bit of a hack. – Jonathan Bedard Apr 05 '15 at 19:03
  • @JonathanBedard the last line `dfs(G,j,visited)` is still not going to pass... You just cannot pass the array `int[ROWS][COLS]` to `int**`. – vsoftco Apr 05 '15 at 19:28
  • @JonathanBedard yeah my DFS function is not good ? do you have any ideas how to implement it to with 2D array ? – mello Apr 05 '15 at 19:49
  • Why don't you just use a nested loop? I can post another answer, but it will be a rebuild of your entire application – Jonathan Bedard Apr 05 '15 at 20:40