Quote:Original post by laserbeak43
so far, i have this
*** Source Snippet Removed ***
-edit-
it compiles fine, just nothing is shown in the console window.
There's a few things wrong here. First, your for loop statement by itself:
for(int i = 0; i > 12; i++)
i is less than 12, yet your condition is when i is greater than 12. Switch the '>' for '<' and then your loop will run. However, it won't run correctly because:
xInc = rand();
x[xInc++];
rand() returns an integer between 0 and RAND_MAX. x is an array if 511 elements (you put 511, but x[511] means 0 to 510, inclusive. If you want 0 to 511 you need x[512]). So you have a situation where you will often get a result greater than 512 and then you try accessing the array x outside of it's bounds. You'll want to restrict to range of rand(), possibly like this:
xInc = rand() % 512;
The '%' (modulo) operator returns the remainder of integer division and so the remainder will never be 512 or greater.
Actually, that probably won't cause any problems in your program either because... You never assign anything to any array elements. To assign to your array you probably meant to do this:
xInc = rand() % 512;
x
= xInc;
or better yet, just cut out all that junk and do this:
x = rand() % 512;
If you loop from 0 to less than 512 and perform that operation in the loop then you get an array filled with random numbers.
Anyway, I assume that this is homework so I won't give you that solution. Instead I'll give a much more advanced solution that, if you submitted, should raise the grader's 'cheating' flag and that, if it isn't homework, will at least teach you a different way to solve the problem.
#include <iostream>#include <vector>#include <algorithm>#include <cstdlib>#include <ctime>using namespace std;int rand512(){ return rand() % 512;}int main(){ srand(time(0)); std::vector<int> x(512); std::generate(x.begin(), x.end(), rand512); for(std::vector::iterator i = x.begin(); i != x.end(); ++i) { std::cout << *i << "\n"; }}
Hope this helps.