I have the following void function devised to assign "+1" or "-1" to each element of a matrix at random. While the function does what I want, when I print the values of the matrix all are set to zero:
#include <vector>
#include "aRand.h"
#include <iostream>
void initConfig(std::vector<std::vector<int> > premat, int nL, int nN) {
int * pnRand;
pnRand = 0;
for (unsignedint i=0; i<nL;++i) {
pnRand = pnRand_plus(1,100,nN,time(NULL) + i);
for (unsignedint j=0;j<nN;++j) {
premat[i][j] = (*(pnRand + j) > 50 ? -1:1);
std::cout<<*(pnRand + j) << " " << premat[i][j] << std::endl;
}
delete[] pnRand;
}
}
// test driver
int main() {
int nN = 5;
int nL = 5;
std::vector<std::vector<int> > premat(nL, std::vector<int>(nN));
initConfig(premat,nL,nN);
for (unsignedint i=0; i<nL; ++i)
for (unsignedint j=0; j<nN;++j) std::cout << "premat[" << i << "][" << j << "] = " << premat[i][j] << std::endl;
return 0;
}
The function pnRand_plus returns a pointer to an array of random numbers from 1 to 100, with seed time(NULL) + i.
The values printed in main are zero, despite the values printed during the function run are fine (-1s and +1s).