Sending many copies of an array to a function

I have an array (or vector or some other object which stores many numbers, I'm not fussy). It has L elements and I call it 'bits'. I want to make N copies of bits and send them out to be processed by a function I call 'randomize'.

In matlab I would use a column vector to store bits, and then store N copies of this in a matrix Nbits for which each column is equal to bits. I could then send each column of Nbits out to the function randomize. It would look like this:

%this loop copies bits to each column of Nbits
for i=1:N

Nbits(:,i) = bits; %Nbits(:,i) represents the column vector composed of the elements of the ith row of the matrix Nbits

end

%this loop sends the N copies of bits out to the function randomize (can be done sequentially or parallelized)
for i=1:N

Nbits(:,i) = randomize(L,Nbits(:,i)); %we give the function L, the length of bits, and Nbits(:,i), the ith column of Nbits. It outputs a new Nbits which overwrites the original

end

My question is how I could do this in c++
You can do something very similar in C++. Just study the good old C arrays (which could be your best bet for simple data types as you can copy bits with a single memcpy() statement), or std::vector. With STL (std::vector), you can create a vector of vectors that would represent the copies of the original std::vector.

I know you requested code, but I usually don't give out ready-made code to avoid freeloaders that just want their homework done. It may very well not be the case here, but you know, better safe than sorry.

If after you study the topics you get stuck at some point, feel free to post specific questions about the code you have done (and potentially not working) and most people here will be glad to assist.
Thanks for your suggestions. I've been trying to do it with arrays, but they don't seem to be doing what I want them to. I have prepared a bit of example code, as you suggest. This creates an L by N array with the ith column full of N i's. I then try to send the entire array to randomize, which does nothing to Nbits at the moment, it just says "hello".


#include <iostream>
using namespace std;

void randomize (int Nbits[][10]){

cout << "hello";

}

int main()
{

int L, N;

L = 5;

N = 10;

int Nbits[L][N];

for (int j=0;j<L;j++){
for (int i=0;i<N;i++){

Nbits[j][i] = i;

}
}

randomize(Nbits);

return(0);

}

Even this simple code I cannot get to work. It gives the error

test.cpp|29|error: cannot convert ‘int (*)[(((unsigned int)(((int)N) + -0x00000000000000001)) + 1)]’ to ‘int (*)[10]’ for argument ‘1’ to ‘void randomize(int (*)[10])’|

I feel I should master this before attempting to send the columns off individually. Could you gives some pointers?

Thanks
Works for me. http://ideone.com/g2Osi .

Note that your way of dimensioning the array is only valid in maybe one compiler and it is not standard C. The example in the link above shows the correct way using constants.
Well done that man! Works for me too with the constants in. Now I need to know how to send only a single column at a time. I'm more than willing too look it up myself, I don't want to leech off you too much, but I don't know where to start. Could you give me some keywords?
Study all about arrays, what they are, how they look like in memory, etc. You'll get to the answer. You can also try things out yourself by experimenting a bit. It is not too far different from what you have right now.
Topic archived. No new replies allowed.