-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);
    }
}
2
  • @mello I think the last one addresses exactly your question. Commented Apr 5, 2015 at 18:44
  • Both do, the reason why you get the problem is because of static/dynamic array mismatch, this problem has been answered in both linked answers. Commented Apr 5, 2015 at 18:44

2 Answers 2

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);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

so i should change the void pdfs (int G[][],int i , int *visitded)
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.
so i should take the int size variable into the function and put a nested for loop with the const max right
@mello, yes, loop as you'd do over any bi-dimensional array, with a nested loop.
@mello see the updated edit for a modification of your code so it uses std::vector.
-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)

8 Comments

this declares G as an array of references, which is an error (caught at compile time)
that just happened ... the program crashed .. DO you have any subjections ? Please
Just changed it. I thought the reference would work, it does not. The double pointer should do the trick though.
@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]
Ok, so everything I said before was fantastically messed up. Posting one that will definitely compile, although is a bit of a hack.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.