Hello everyone, I am a fairly new programmer, I'm gonna explain my problem fairly in depth, but I don't anticipate it should be a very long answer. I am working on a random number generator project following fairly specific specs. I pretty much get the min and max ranges from the user, along with how many random numbers they want to generate. So I store the random numbers in bins, the amount should fluctuate depending on what the user inputs, for this project it will be 9 bins. I've done all the classes and the interface, and I realize that I am getting memory problems after I run the program. A whole bunch of problems.
So first thing I do is break the code down and make it VERY VERY simple and in just one file: main.cpp. By doing this I was able to isolate (through removing and implementing my code) the problem to be the allocation of memory. I won't include all of the program (unless you guys would like to see it), but here is the chunk of code that is giving me problems:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int *ptr = NULL;
ptr = new int[arraySize];
int roll; //input from the user, max of the loop
cout << "How many times would you like to generate random number? ";
cin >> roll;
int face; //current face of the random number generated
int i;
for (i = 0; i < roll; i++)
{
srand(i * time(0));
face = (1 + rand() % 9);
ptr[face]++;
}
|
All variables are declared and defined earlier in the code, and all of the appropriate preprocessor directives are used. This is what the output of what each bin/array looks like:
1: -842150451
2: -842150432
3: -842150429
4: -842150429
5: -824150428
etc. etc.
It is supposed to output how many times each number has been produced from my random number generator (ex: 1 generated 200 times, 2 generate 203 times, etc.). Instead I am getting this mumbo jumbo. And it is not exactly the same numbers that pop up every time I run it. But pretty much the same following the form: -8241504## The only number that differs is the ninth one, which usually turns out: -336859##
The point of all this is to create a histogram. Please help!
P.S.- No vectors, I must do this through an array according to the specs! I've been debating on whether or not I should ask for help. But I could really use a pointer (har har) in the right direction..I've been beating my head over this for a while now and I can't seem to find any documentation anywhere (not even my book!). Thanks!