Dynamic allocation of array problem for histogram. Looking for some hints.

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!
Last edited on
I think that you didn't initialize ptr: after its definition add something like it
for (int i = 0; i < arraySize; i++) ptr[i] = 0;

Also, call srand(time(0)) just once;
I am not sure about your intentions, but maybe it is worth removing 1 in face = (1 + rand() % 9); (numbering in c++ arrays starts from 0).
Thank you so much! Thankfully it was a simple fix (such as not being initialized), and I fixed my loops so that: i <= arraySize. And I need to keep that 1 in the random functions because I need to generate from 1 to 9, otherwise it will produce 0 to 8. I also adjusted my array to be: ptr[face - 1]++; to make up for this difference that you pointed out. Thanks alot! :)
Topic archived. No new replies allowed.