samedi 10 décembre 2016

Why am I stuck in a while loop?[c++]

I'm stuck in a loop in here. I have a matrix filled with zeroes and I want to add k edges to a graph. I have no errors showed in Visual Studio. While it goes to this line of code:

int z = 0;
    while (z!=k);
    {
        int a = std::rand() % n;
        int b = std::rand() % n;
        if ((v[a][b] != 1) && (a != b))
        {
            v[a][b] = 1;
            v[b][a] = 1;
            z++;
        }
    }

program gets stuck in infinite loop;

Here is full code:

std::vector<std::vector<int>> random_gnk(int n, int k)
{
    srand(time(NULL));
    int temp = 0;
    for (int i = 0; i < n; i++)
    {
        temp = temp + i;
    }
    std::vector<std::vector<int>> v;
    if (k > temp || k<0)
    {
        std::cout << "Blad. Podano zla liczbe krawedzi." << std::endl;
        return v;
    }
    for (int i = 0; i < n; i++)
    {
        std::vector<int>row;
        for (int j = 0; j < n; j++)
        {
            row.push_back(0);
        }
        v.push_back(row);
    }

    int z = 0;
    while (z!=k);
    {
        int a = std::rand() % n;
        int b = std::rand() % n;
        if ((v[a][b] != 1) && (a != b))
        {
            v[a][b] = 1;
            v[b][a] = 1;
            z++;
        }
    }
    return v;
}
void GNK()
{
    int n, k;
    std::cout << "Podaj wielkosc n grafu: " << std::endl;
    std::cin >> n;
    std::cout << "Podaj liczbe k krawedzi grafu: " << std::endl;
    std::cin >> k;
    print_matrix(random_gnk(n, k));
    return;
}




Aucun commentaire:

Enregistrer un commentaire