LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ help needed (https://www.linuxquestions.org/questions/programming-9/c-help-needed-118514/)

sabeel 11-21-2003 09:46 AM

C++ help needed
 
Hi

I needed some help with C++. Any ideas would be greatly appreciated.

I have a bunch of objects which i create.
Now, each object needs to do some computation in relation to all the other objects that i created. I wanted to know how each object can refer to the other objects.

i want each object to do this computation locally by itself (- to make it a distributed, rather than me centrally computing something - directly from main() )

Thanks in advance

einherjar 11-21-2003 10:04 AM

Its kind of hard to say without knowing the specifics of your program design but...

Say you have objects A, B and C, and C needs to do a computation that requires data from A and B.

I would make a method in C that takes the data you need from A and B by using 'get methods'. e.g

PHP Code:

C.doComputation(A.getData(), B.getData

Hope that helped, my OO experience is all Java but (afaik) the same principles apply.

sabeel 11-21-2003 10:18 AM

let me give you the complete picture.

i am working on a simulation for wireless networks. so there are a bunch of nodes (each of which is an object). Each node has some co-ordinate values assigned to it randomly.

Now, each node (object) needs to compute the distance from itself to all other nodes (objects).

As i said earlier, i want each object to do it independently. so each object (say A) calls
A.find_neighbors();

now, inside find_neighbors() - i want a pointer to other objects so that i can get that object's co-ordinates.


thanks
sabeel


ps: each node (object) has an ID - which is just an integer value.

einherjar 11-21-2003 11:04 AM

Nice project :) Thanks for the clarification on the design.

I'm assuming that every node has a list of (pointers to) known nodes. What you could do is when A calls get_neighbours() it could call getList() for all the nodes in its list. getList() could return an array of pointers to nodes and change an int to inidcate how big the array is. Then you could add those pointers to A's known nodes list. You could use a linked list for the known nodes list to solve the duplicate known nodes issue.

Sorry for the convoluted description.. maybe some pseudocode might be better...

PHP Code:

get_Neighbours   {
   
Node temp[maxNodes];
   
int size;

   for (
each known node)   {
      
temp knownNode.getList(&size);  // size is set by knownNode
       
for(int x 0sizex++)   {
          
add temp[xto linked list of known node pointers;
           }
    }



There is also the issue of discovering a node when you start with an empty list but I assume you already have some way of dealing with that.

I'm sure some OO guru will give you a better and more simple solution but thats probably how I would approach the problem :)

sabeel 11-21-2003 11:14 AM

hey einherjar,

Thanks for the suggestion.


sabeel

kev82 11-21-2003 03:24 PM

my C++ is a bit dodgy but how about this, the Node class keeps track of all nodes in a static vector, obviously get rid of position and distance and re-implement them your own way. also consider a list instead of a vector but that really depends on what your doing.

Code:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class Node {
private:
        static vector<Node *> Nodes;
        int position;
public:
        Node(int p) : position(p) {
                Nodes.push_back(this);
        }
        ~Node() {
                Nodes.erase(find(Nodes.begin(), Nodes.end(), this));
        }

        void distance() const {
                for(vector<Node *>::iterator it = Nodes.begin(); it != Nodes.end(); it++) {
                        if(*it == this) continue;
                        cout << *this - **it << endl;
                }
        }

        int operator-(const Node& n) const {
                return position-n.position;
        }
};

vector<Node *> Node::Nodes;

int main()
{
        Node A(5), B(3), C(13);
        C.distance();
        A.distance();
        return 0;
}
~
~


flange 11-27-2003 12:19 PM

it might be a bit of work to set up but if you want a 'good' solution you should try using a graph structure for the network and putting the distance calculating bit in there.


class Network
{
private:
set<Node> nodes
public:
set<Node> getNeighbours(Node n)
{
set<Node> res;
insert_iterator<set<Node> > nodeInsert(res);
set_difference(nodes.begin(), nodes.end(), &n, &n +1, nodeInsert);
}

void addConnection(Node &n1, Node &n2)
{
nodes.insert(pair<Node, Node>(n1, n2));
nodes.insert(pair<Node, Node>(n2, n1));
}
}


with this you can centralise the functionallity but have it hidden from the main program.


All times are GMT -5. The time now is 04:38 AM.