LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2003, 09:46 AM   #1
sabeel
LQ Newbie
 
Registered: Aug 2001
Location: montreal, canada
Distribution: redhat
Posts: 22

Rep: Reputation: 15
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
 
Old 11-21-2003, 10:04 AM   #2
einherjar
LQ Newbie
 
Registered: Nov 2003
Posts: 6

Rep: Reputation: 0
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.
 
Old 11-21-2003, 10:18 AM   #3
sabeel
LQ Newbie
 
Registered: Aug 2001
Location: montreal, canada
Distribution: redhat
Posts: 22

Original Poster
Rep: Reputation: 15
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.
 
Old 11-21-2003, 11:04 AM   #4
einherjar
LQ Newbie
 
Registered: Nov 2003
Posts: 6

Rep: Reputation: 0
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
 
Old 11-21-2003, 11:14 AM   #5
sabeel
LQ Newbie
 
Registered: Aug 2001
Location: montreal, canada
Distribution: redhat
Posts: 22

Original Poster
Rep: Reputation: 15
hey einherjar,

Thanks for the suggestion.


sabeel
 
Old 11-21-2003, 03:24 PM   #6
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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;
}
~
~

Last edited by kev82; 11-21-2003 at 03:32 PM.
 
Old 11-27-2003, 12:19 PM   #7
flange
LQ Newbie
 
Registered: Apr 2003
Posts: 1

Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help needed sailu_mvn Programming 2 03-24-2005 06:50 AM
Help needed! =) kith Linux - General 14 02-10-2004 12:44 PM
help needed with QT sabeel_ansari Programming 1 01-21-2004 02:14 PM
help needed here... c12ayon Programming 2 10-29-2003 10:59 AM
Needed Help rajesh.s Linux - Hardware 0 12-17-2002 02:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration