I have used MATLAB to simulate Wireless Sensor Networks (WSNs) for a long time, but since it is too slow, I plan to simulate them using C++. In this regards, I have following questions:
1- In MATLAB, I employed a struct (called S) in order to save the information of different sensors. For example, sensor 1 has some information, such as the residual energy (S(1).Eres), distance to base station (S(1).distance), etc. Now, in C++, I have written a struct as follows:
struct sensor {
char type;
int nhop, nhop_dist, Rc, CH, CH_dist, Nd, CM, Lv, T, relay, loc;
float Eres, xd, yd, distance;
int nbr[30];
void tracking();
};
In my main, I have defined an object called S and used rand function to create a random WSN:
int main()
{
struct sensor S[N]; // N is the number of nodes
int i;
for (i=1; i <= N; i++)
{
S[i].xd= rand() % M + 1; // location of nodes
S[i].yd= rand() % M + 1;
S[i].Eres=Emax;
}
return 0;
}
Now, my question is that how can I define multiple sensor nodes as what I used to define in MATLAB? Is what I have used correct?
2- How can I pass S to a function? Since I want to access all sensor nodes, I should have access to all nodes in the function. Could you please write an example?
First things first, in C++, an array with N items is accessed using indexes 0 through N-1, not 1 through N.
Second, unless memory is a concern, use type double instead of float. It's higher precision and there's really no down side.
You can pass the the array to a function with something like this:
1 2 3 4
void func(sensor S[], unsigned size)
{
do stuff with S;
}
But you will probably find it easier to use a vector<S> instead of an array. The advantage of a vector is that it can be as big (or small) as you want.
Now, I am collecting the results in order to depict the figures. To do so, I use MATLAB, which is very professional in plotting the results. In order to collect the results and pass them to MATLAB, I first define some global output variables, as text files, at the beginning of the code as:
The problem with this is that the program creates an alive1001.txt, then adds the result of the second run to alive1002.txt. That is, alive1002.txt contains the information of both alive1001 and alive1002 txt files, and so on. Could you please tell me what is the problem?
Moreover, is there a more convenient way to collect all results, e.g., using a loop to create files, and pass them to MATLAB? I just found txt files as a simple method to do so.
alive1002.txt contains the information of both alive1001 and alive1002 txt files
You say you're running the program 10 times. Does that mean you are literally executing the program 10 separate times, or is there a big for (run =0; run < 10; ++run) loop somewhere?
If you have a forloop for run then the problem is that you aren't clearing the ALIVE array (and probably other arrays too) between runs. So each run appends data to ALIVE instead of creating an entirely new set.
Just one question. Is there a simpler way to define the name of each output (like using a loop) file rather than defining it by if-then? If I want to run the program for 50 times, I have to use a lot of if-then so.
Hey Jack... I have a library that lets you convert M files to cpp files with some hands-on changes but not as many as you might think.... I converted about 100 over a couple of years back in the day...