简体   繁体   中英

passing a double pointer boolean array to a function

I have a double pointer boolean. I would like to pass this boolean to a function

The code is used for graph theory, to create an adj matrix, check if the graph has cycles or not ...

The problem comes from the cycle function

The function doesn't like the boolean in parameter to check if a graph has a cycle.

#include <iostream> 
#include <fstream> 
#include <cstdlib>
#include <vector> 
#include <algorithm> 


using namespace std; 

int Cycle(int number_of_vertices ,bool **adj_matrix)
{
    bool** adj = new bool*[number_of_vertices];
    for(int i=0;i<number_of_vertices;i++)
    {
        adj[i] = new bool[number_of_vertices];
    }
    for(int i=0;i<number_of_vertices;i++)
    {
        for(int j=0;j<number_of_vertices;j++)
        {
            adj[i][j] = adj_matrix[i+1][j+1];
        }
    }
    for(int k=0;k<number_of_vertices;k++)
    {  // transitiv closure
        for(int i=0;i<number_of_vertices;i++)
        {
            for(int j=0;j<number_of_vertices;j++)
            {
                if(adj[i][k]&&adj[k][j])
                {
                    adj[i][j] = true;
                }
            }
        }
    }

    int count = 0;
    for(int i=0;i<number_of_vertices;i++)
    {
        if(adj[i][i])
        {
            count++;
        }
    }
    return count;
}


int main()
{
    string first_line, second_line; 
    int initial_extremity, final_extremity, value, number_of_vertices, nombre_arcs;
    ifstream fichier("test.txt"); 
    bool** adj_matrix; 
    int** val_matrix; 
    vector<int> vertice_names; 

    if (fichier.is_open())
    {
        getline(fichier, first_line); 
        number_of_vertices = atoi(first_line.c_str()); 
        getline(fichier, second_line); 
        nombre_arcs = atoi(second_line.c_str()); 
        adj_matrix = new bool*[number_of_vertices]; 
        val_matrix = new int*[number_of_vertices];


        for (int i=0; i<number_of_vertices;i++)
        {
            adj_matrix[i] = new bool[number_of_vertices];
            val_matrix[i] = new int[number_of_vertices];

            for (int j=0; j<number_of_vertices; j++)
            {
                adj_matrix[i][j] = false; 
                val_matrix[i][j] = 0; 
            }
        }


        while(fichier >> initial_extremity >> final_extremity >> value)
        {
            adj_matrix[initial_extremity][final_extremity] = true;
            val_matrix[initial_extremity][final_extremity] = value;
            int c = Cycle(number_of_vertices, adj_matrix);
            printf("number of cycles: %d\n\n", c);
            if(c!=0)
            {
                printf("%d --[%d]--> %d\n",initial_extremity,value,final_extremity);
            }
            else
            {
                printf("%d --[%d]--> %d\n",initial_extremity,value,final_extremity);
            }

            if ( find(vertice_names.begin(), vertice_names.end(), initial_extremity) != vertice_names.end() )
            {
                //nothing
            }
            else
            {
                vertice_names.push_back(initial_extremity); 
            }
            if ( find(vertice_names.begin(), vertice_names.end(), final_extremity) != vertice_names.end() )
            {
                //nothing
            }
            else
            {
                vertice_names.push_back(final_extremity);
            }
        }
        fichier.close(); 
    }

    else
    {
        cout << "error while opening the file" << '\n';
        cin.get();
    }

    printf("%d arcs\n",nombre_arcs);
    printf("%d vertices\n\n\n",number_of_vertices);
    printf("Adj Matrix \n");
    printf("    ");
    for(int i = 0; i<number_of_vertices; i++)
    {
        printf("%3d",vertice_names.at(i));
    }
    printf("\n");
    for (int i = 0; i<number_of_vertices; i++)
    {
        printf("%3d ", vertice_names.at(i));
        for (int j = 0; j <number_of_vertices; j++)
        {
            printf("%3d",adj_matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n\n");


    sort(vertice_names.begin(), vertice_names.end(), less<int>()); 
    printf("Adj and value matrix\n");
    printf("    ");
    for(int i = 0; i<number_of_vertices; i++)
    {
        printf("%3d",vertice_names.at(i));
    }
    printf("\n");
    for (int i = 0; i<number_of_vertices; i++)
    {
        printf("%3d ", vertice_names.at(i));
        for (int j = 0; j <number_of_vertices; j++)
        {
            if (adj_matrix[i][j])
            {
                printf("%3d",val_matrix[i][j]);
            }
            else
            {
                printf("   ");
            }
        }
        printf("\n");
    }



    return 0;
}

format of the .txt file:

first line : number of vertices second : number of arcs the other lines : Inital Arc Final Arc Value

3
4
0 1 0
1 0 12
1 2 25
2 0 6

By the way, if someone have a better idea to check if a graph has a cycle let me know

Best regards

Looks to me like you are stepping out of bounds in the Cycle function:

for(int i=0;i<number_of_vertices;i++)
{
    for(int j=0;j<number_of_vertices;j++)
    {
        adj[i][j] = adj_matrix[i+1][j+1]; // i + 1 and j + 1 go out of bounds
    }
}

Suppose number_of_vertices is 3. Then the index of the last element is 2. When i = 2, then i + 1 = 3. Out of bounds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM