I'm writing a program that needs to fill two vectors with random ints(less than 1000) using 2 different seeded lists. when I try to use srand with the seeds I am supposed to use it gives me a very strange output.
here is the code I've written so far...
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
void Vectors(vector <int> v1, vector <int> v2, int s1, int s2) {
srand(s1);
for (int i = 0; i < 200; i++) {
v1.push_back(rand () % 1000 + 1);
//v1[i] = rand() % 1000 + 1;
}
srand(s2);
for (int i = 0; i < 100; i++) {
v2.push_back(rand() % 1000 + 1);
//v2[i] = rand() % 1000 + 1;
}
}
void prnt(vector<int> v) {
for (int i = 0; i < 20; i++) {
cout << v[i] << " ";
if (i == 9 || i == 19) {
cout << endl;
}
}
}
int main() {
vector<int> vec1;
vector<int> vec2;
vec1.resize(200);
vec2.resize(100);
Vectors(vec1, vec2, 1, 3);
prnt(vec1);
prnt(vec2);
return 0;
system("pause");
}
however, when I run it the output I get is this...
0 29046 -309340552 29046 32 0 134113 0 0 0
-309339528 29046 64 0 48 0 0 0 986 169
0 0 -309340552 29046 32 0 134113 0 0 0
-309339528 29046 64 0 48 0 0 0 986 169
also, it will not allow me to use the Vectors method if vec1 and 2 are not initialized to some size.
I have just transferred from Java to C++ so any help you can offer me will be very appreciated as it is maddening to be stuck on something so trivial in Java
You're passing in the vectors by value and not reference, so you're changing copies, not the originals.
Instead, pass them by reference:
and you'll be changing the original vectors and you'll see your changes outside the function.