简体   繁体   English

将双指针布尔数组传递给函数

[英]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 ... 该代码用于图论,以创建adj矩阵,检查图是否具有循环...

The problem comes from the cycle function 问题来自循环功能

The function doesn't like the boolean in parameter to check if a graph has a cycle. 该函数不喜欢布尔值in参数来检查图形是否具有循环。

#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: .txt文件的格式:

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: 在我看来,您正在超越Cycle函数的范围:

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. 假设number_of_vertices为3,则最后一个元素的索引为2。当i = 2时,i + 1 = 3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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