implementing functions :/

The following is my program for an assignment and somehow its not working..can someone tell me what is wrong..:)

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

const int N = 10;
const int M = 20;
const int WAIT = 1;
const int SENDING = 3;
const int DONE = 4;

void initialse(int nodes[][N]){
/*
This function sets all values in nodes to WAIT; then it randomly choses
an entry an initialises it to RECEIVED.
*/

//Exercise 3: Please extend this function.

//This loops set all values in nodes to WAIT.
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
nodes[i][j] = WAIT;

int a, b;
a = rand();0 + N-1;
b = rand();0 + N-1;
}
}
}

void display(int nodes[][N]){
}

int main() {
/*
This main function intialises nodes, and displays it
*/

int nodes[N][N];

initialse(nodes);
display(nodes);
cout << "\n Initial network state \n\n";
system("pause");
return 0;

}
Last edited on
Your rand functions are in the wrong place. They should be outside the for loops, assuming you don't want to call it 100 times :P And the syntax you are using is incorrect:
 ``1234`` ``````a = rand();0 + N-1; //These are actually two statements. One that generates a number from 0 to RAND_MAX //and stores it in a, and another one that does absolutely nothing. //You want something like this: a = rand()%N; //This will gen a number from 0 to N-1 using the modulo operator. ``````

As for the templated function, first of all your syntax is wrong, the template declaration should come before the function definition, and second of all I have no idea what you're trying to accomplish.
Last edited on
okay okay my bad, still new to this c++ stuff. but thanks for helping :) I'm trying to make a 10 by 10 matrix that displays nodes that send messages. Something like a wireless network simulation.
All right, well your code for setting all of the nodes to WAIT should be working as long as you moved the rand() stuff outside of the for loops and changed them to what I showed you.
As for the templated function "display" I still don't understand what you wanted your code to do.
 ``1234`` ``````void display(int nodes[][N]){ template cout << nodes[WAIT][N] << nodes[RECEIVED][N]; }``````

I don't think you quite understand templates. The "template" part of the code should go before the function declaration. A template is used to allow a class or function to act on any arbitrary data:
 ``1234567891011121314`` ``````template class vector { private: T *array; //Here we have an array of type T, which is defined by the user. public: T& operator[](unsigned); //Here we have a return value of type T. } template T vector::operator[](unsigned i) { //code }``````

This is probably close to what the declaration of std::vector looks like, a standard container that can hold any data type.
If you just want your function to print the data in the array, then you can just leave out the template stuff. Although right now, your cout is only printing nodes[WAIT=1][N=10] and nodes [RECEIVED=2][N=10].
Last edited on
yes it is displaying a 10x10 matrix of WAIT=1. but I also need it to display RECEIVED = 2 in the 10x10 matrix. I have done what you have told me, but I can't seem to understand template.

I need something like this:

1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111211111

RECEIVED node can displayed anywhere in this 10x10 matrix.
Well you've already set your "received" node to 2 during the initialize() function, so all you need to do is loop through the array and print the values instead of writing to them. To format the array properly you can just print an std::endl at the end of every row. You don't need templates for this unless your assignment calls for it.
okay. the thing is it doesn't display 2 in the 10x10 matrix. its confusing..argh!

..this is what our professor wants us to do.in the display function: Use a similar nested for loop for function display, but rather than assign a new value, print the value.

-but mine is not working..

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

const int N = 10;
const int M = 20;
const int WAIT = 1;
const int SENDING = 3;
const int DONE = 4;

void initialse(int nodes[][N]){
/*
This function sets all values in nodes to WAIT; then it randomly choses
an entry an initialises it to RECEIVED.
*/

//Exercise 3: Please extend this function.

//This loops set all values in nodes to WAIT.
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
nodes[i][j] = WAIT;
}
}
int a, b;
a = rand()%N;
b = rand()%N;

}

void display(int nodes[][N])
{
for(int a=0;a<N;a++){
for(int b=0;b<N;b++){
nodes[a][b] = WAIT;
cout << nodes[WAIT][N];
}
cout << endl;
}
int p,q;
srand(time(0));
p = rand()%N;
q = rand()%N;
cout << endl;
}

int main() {
/*
This main function intialises nodes, and displays it
*/

int nodes[N][N];

initialse(nodes);
display(nodes);
cout << "\n Initial network state \n\n";
system("pause");
return 0;

}
You're changing all the values to WAIT before you print them:
 ``1234`` ``````nodes[a][b] = WAIT; //Here you change everything back to WAIT (1) cout << nodes[WAIT][N]; //Here you print nodes[1(WAIT)][10(N)] N*N times //You want to print every array element: std::cout<

The std::endl is good, but why is the section to set a random node to received also in the print function? Just get rid of all that.
okay.okay. it is printing a 10x10 matrix of WAIT but i also need a RECEIVED node in the 10by10matrix and that i cant get:( i am seriously confused.

void display(int nodes[][N])
{
for(int a=0;a<N;a++){
for(int b=0;b<N;b++){
nodes[a][b] = WAIT;
cout << nodes[WAIT][N]; // this prints values 1 in a 10x10 matrix
}
cout << endl;
}
}

how to i insert a function for the RECEIVED node ?
You don't need a function for RECEIVED mode. RECEIVED and WAIT are just two const ints that you declared at the top of your code. They literally mean the number you assigned to them, so whenever you call anything with WAIT, your compiler will just replace WAIT with 1. So calls like this:
 ``1234567`` ``````cout<

Also, again you're setting all your nodes to WAIT or 1:
 ``12345`` ``````nodes[a][b] = WAIT; //This is the same thing as: nodes[a][b] = 1; //So you're essentially just setting everything to 1, because a and b loop through //your whole array. Take this line out, it's only for initialization. ``````
THANK YOU SO MUCH FOR HELPING !

okay, everything is working well but im abit confused in changing the values of from wait2received, received2send, send2done...any idea how i can implement it?

void sending2done(int nodes[][N]){

??????????

}

????????????????

}

??????????????????

}

Last edited on
Well that depends on how the functions are supposed to work. I'll give you some tools to help you out, and when you post the code for the functions and what they're supposed to accomplish we can go over them.
So to loop over every item in your array you can use for loops like in your initialization:
 ``12345678910111213141516171819`` ``````for(int i = 0; i < height; ++i) { for(int w = 0; w < width; ++w) { array[i][w]; //Do whatever you need to here. //You can test for something: if(array[i][w] == condition) { //Do something } //Or you can assign a value: array[i][w] = x; //Or you can print it: std::cout<< array[i][w]; //Or you can use it to call a function: func(array[i][w]); //It just depends on what you need to do. } }``````

Remember, the const int's you declared will just be replaced by the number assigned to them.
So to test a value you just compare it:
 `` `` ``if(array[i][w] == WAIT) //Here WAIT will be replaced by 1. ``

Remember not to use your const values as any sort of index while looping through the array since they will limit you to that one row/column.
Last edited on
okay, but i need it to run on the same 10by10 matrix instead of making new ones.

something like this:
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111211111
1112321111
1123432111

I HAVE DONE THIS :
BUT IT CREATES NEW MATRICES AND DOES NOT WORK LIKE ABOVE :(

void sent2done(int nodes[][N]){
/*
This function sets all nodes with the value SENDING to value DONE.
*/
//Exercise 6: Please implement this function.
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(nodes[i][j] == SENDING){
nodes [i][j] = DONE;
}
}
}
}

/*
This function sets all nodes with the value RECEIVED to value SENDING.
*/
//Exercise 6: Please implement this function.
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
nodes[i][j] = SENDING;
}
}
}
}

/*
This function sets all nodes with the value WAIT to value RECEIVED, if they
have a neighbor that has value SENDING.
*/

for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(nodes[i][j] == WAIT){
}
}
}
}
Last edited on
What do you mean it must run on the same matrix? Passing an array to a function is similar to passing a pointer, so you will actually operate on the same array, and not a copy:
 ``12345678910111213`` ``````#include void func(int arr[][10]) { arr[0][0] = 10; } int main() { int arr[10][10] = {0}; func(arr); std::cout<

So when you make the function calls just pass the same matrix you used earlier.
 ``12345678`` ``````int main() { int arr[10][10]; ClearArray(arr); InitArray(arr); sent2done(arr); //And so on. }``````

Your functions themselves look fine, except for "wait2received". Read over the instructions again and try to implement the second requirement (must have a neighbour with SENDING). Be careful when checking for neighbours though, since attempting to index something outside the array will cause pain.
well, it still displays the matrix 20 times:

https://ideone.com/XV3UF

Well yeah, you called display 20 times. C++ doesn't clear the command prompt for you automatically. The actual matrix itself is the exact same. Also, your wait2received function is still a bit off:
 ``12345678910`` ``````if(i+1 == SENDING || i-1 == SENDING || j+1 == SENDING || j-1 == SENDING) //Here you are testing if the values of i+1/-1 are equal to SENDING //which as we know from last time is 3. //You don't want to check the actual i and j values, but the array at those //locations. You even wrote a helper function for this "neighbour_sends". //So instead do something like this: if(nodes[i][j] == WAIT && neighbour_sends(nodes,i,j)) { //Code }``````

If you want to look into clearing the command prompt, read this topic, and the one that Duoas links to:
http://www.cplusplus.com/forum/beginner/3304/
yes..finally figured it out...
thnx for so much for helping. God Bless !
Topic archived. No new replies allowed.