0

The code basically takes in number of nodes(N) and the number of neighbors(k) as the input. Then in the main function's for loop the node will be created which will have all the variables declared in the structure. Then the main function calls the initialization function.

The initialization function creates a vector g consisting of N-1 node numbers which doesn't have the node creating it.(That is if N=100 and the 1st node has called the initialization function, then vector g will have 2,3,....,100 ,and if node 2 has called the initialization function, then vector g will have 1,3,4,...,100).

After this random_numbers function is called and this function randomly shuffles this vector g and picks the first k elements from it and returns it to the initialization function which in turn returns it to the main function. Then the main function will update the node1.neighbor_list using the vector it has received.

After the code finishes running, the output has to look something like this suppose, N=5, k=3.

node1
{
userID = 1; 
neighbor_list = [2,5,4];
}
node2
{
userID = 2; 
neighbor_list = [5,4,1];
}
node3 {
userID = 3; 
neighbor_list = [2,5,4];
}
node4
{
userID = 4; 
neighbor_list = [2,5,1];
}
node5
{
userID = 5; 
neighbor_list = [3,2,4];
}

I am able to trace the working till the initialization function returns the vector back to the main function. But I am unable to copy this vector into the neighbor_list vector of the node. Please help!! I have been trying for long at this point.

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

int N;
int k;
vector<int> g;
int j;
int i;

struct nodes
{
    int userID;
    vector<int> neighbor_list;
};

vector<int> initialization(int j);
vector<int> random_numbers(vector<int> &r);

int main()
{
    int f;
    vector<int> received;
    cout << "Enter the number of nodes: "; //enter some number
    cin >> N;
    cout << "Enter the number of neighbors: "; // enter a number less than N
    cin >> k;
    cout << endl;

    for (i = 1; i <= N; i++)
    {
        nodes node[i]; // creates a node 
        node[i].userID = i;
        cout << "creating " << i << " node" << endl;
        cout << "Calling the initialization function" << endl;
        received = initialization(i);
        cout
                << "The vector received back from the initialization function is: ";
        for (f = 1; f <= k; f++)
        {
            cout << received[f - 1] << " ";
        }
        cout << endl;
        for (f = 1; f <= k; f++)
        {
            node[i].neighbor_list.push_back(received[f - 1]); //error(guessing)
        }
        cout << "The neighbor_list for node " << i << " is " << endl;
        for (f = 1; f <= k; f++)
        {
            cout << "f: " << f;
            cout << node[i].neighbor_list[f - 1] << " ";
        }
        cout << endl;
        cout << endl;
    }
    return 0;
}

vector<int> initialization(int j) //passing the value of i 
{
    cout << "Entered the initialization function" << endl;
    cout << "The value of the node created passed in is " << j << endl;
    int f;
    vector<int> g;
    vector<int> receiver;
    int x;
    int n;
    int z;
    int rand_array[N][N - 1]; //to store the neighbor list of the node[i]
    int d;
    int a = 0;
    for (z = 1; z <= N; z++)
    {
        if (j != z)
        {
            rand_array[j - 1][a] = z; //Put all values into array except i
            cout << "The value of rand_array[" << j - 1 << "][" << a << "] is "
                    << z << endl;
            a++;
        }
        else
        {
            continue;
        }
    }
    for (f = 1; f <= N - 1; f++)
    {
        g.push_back(rand_array[j - 1][f - 1]);
    }
    cout << "vector g after push back from the rand_array is: ";
    for (f = 1; f <= N - 1; f++)
    {
        cout << g[f - 1] << " ";
    }
    cout << endl;
    cout << "Calling the random function" << endl;
    receiver = random_numbers(g);
    cout << "value of receiver vector from random_numbers function: ";
    for (f = 1; f <= k; f++)
    {
        cout << receiver[f - 1] << " ";
    }
    cout << endl;
    return receiver;
}

vector<int> random_numbers(vector<int> &r)
{
    int h;
    int f;
    cout << "Entered the random_numbers function" << endl;
    cout << "The vector passed by the initialization function is: ";
    for (f = 1; f <= N - 1; f++)
    {
        cout << r[f - 1] << " ";
    }
    cout << endl;
    vector<int> sender;
    random_shuffle(r.begin(), r.end()); //random shuffle the vector
    for (h = 1; h <= k; h++)
    {
        sender.push_back(r[h - 1]);
    }
    return sender;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • You should indent your code properly, so that it becomes readable. Every line is flushed to the left margin. – PaulMcKenzie Jan 24 '18 at 02:13
  • 2
    I strongly recommend against naming variables with a single letter. Think on which contains more information while debugging: `N` and `k` or `userID` and `neighbor_list`? Also strongly recommend a closer look at `for (i = 1; i <= N; i++)`. – user4581301 Jan 24 '18 at 02:19
  • And `nodes node[i]; // creates a node ` is almost certainly not what you want to do. This isn't "a `node`" it's `i` `node`s. – user4581301 Jan 24 '18 at 02:21
  • Thanks user4581301.. but i don't understand why i should not write nodes node [i] inside a for loop. The node will get created and its value will be updated and then a new node will be created once that is done.. Is my logic wrong? – Shreyas Sreedhara Jan 24 '18 at 02:28
  • @ShreyasSreedhara -- The point is that the line of code you wrote `nodes node[i]; // creates a node` -- does not do what your comment says it does. It isn't even valid C++ syntax. Also, arrays indices start at 0 and go up to `n-1`, where `n` is the total number of entries. Look at your loop indices carefully. – PaulMcKenzie Jan 24 '18 at 02:29
  • Thank you @PaulMcKenzie.. So suppose my N was 100 and I want N nodes, then can I create all the N structures at once rather than calling them inside the loop. By doing nodes node [100] or will I have to create a pointer by doing nodes* node[100]? – Shreyas Sreedhara Jan 24 '18 at 02:37
  • Why not simply use `std::vector node(100);`? You're using `vector` already, why not use it for something as obvious as this? – PaulMcKenzie Jan 24 '18 at 03:30

0 Answers0