Trying to deal with a random number generator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
int main()
{
int amount; 
int IntegerarArray[amount];
unsigned seed;
int randominteger, rndm;

{cout << "Please enter the number of integers you would like to use." << endl;
	cin >> amount;
while(amount < 19)
	{
		cout << "You have entered an integer value that is not below 19. Please enter a larger number..." << endl;
	cin >> amount;
	}
}
for (int step=1; step < amount; step++)
{
	int randomnumber=0;
	srand(unsigned (time(0)));
	randomnumber = rand()%1001;
	IntegerarArray[step]=randomnumber;
	if (step < amount)
	{
	step++;
	cout << IntegerarArray[step] << endl;
	system ("pause");
	}
}
}






Above is the code I'm trying to work with.
I hate asking, but I've been trying to figure this out for awhile and I've not been getting anywhere. This is part of a bigger program where I have to:


Your program must implement the following functions and you must use pointer notation for accessing array elements:

createArray:This function receives an integer as argument, dynamically create an array of integers fills the array with randomly generated integers between 1 and 1000. The function should call the function get RandomIntegr()described next, to randomly generate an integer. The function returns the pointer of the dynamically created array.

getRandomInteger:This function randomly generate and returns aninteger between 1 and 1000 (inclusive)countIntegers:This function should receive an array of integers and number of elements in the array as arguments. It should dynamically create an other array of integers of size 10. The function should count the number of integers in each range (described in the 2ndparagraph)store the count in appropriate location in the dynamically created integer
array.The function should return an integer pointer.

displayIntegers:This function receives an array of integers and size of the array as arguments and display the integers in the array. This function should display 15 integers in
a line separated by space.The function should return nothing.


displayCounts:This function receives the count array and display the count of each range. The function should return nothing.

Last edited on
There are a number of errors in the program. It also doesn't follow the instructions very well.

8
9
int amount; 
int IntegerarArray[amount];

Line 8, amount is not initialised, it contains a garbage value.
Line 9, this does not compile under standard C++. amount must be a compile-time constant but instead it is a variable. Because of this error, the program cannot even be run.

Line 17,
cout << "You have entered an integer value that is not below 19. Please enter a larger number..." instructions are confusing - should the user enter a value larger or smaller than 19?

Lines 13 to 20 are out of sequence, the array IntegerarArray has already been assigned and so getting the value of amount at this point is shutting the stable door after the horse has bolted.

Line 24 srand(unsigned (time(0)));
don't call srand inside a loop. Call it just once at the start of the program. Because the loop will repeat many times during a single second, the value of time won't change, so the identical seed is used over and over again, meaning the same unchanging 'random' number will be generated by rand() each time.

Line 25, randomnumber = rand()%1001; this generates a random number in the range 0 to 1000, not 1 to 1000 as instructed.

Lines 21 and 27 to 29. The loop variable step is incremented in two separate places, meaning the loop will proceed in steps of 2, not 1.

Those are just a few things I noticed.

The instructions ask you to use these functions
createArray:
getRandomInteger:
countIntegers:
displayIntegers:
displayCounts:

In the first function, createArray, you will need to use the operator new to dynamically allocate an array of the required size.
something like this:
1
2
3
4
5
6
7
8
int * createArray(int size)
{
    int * array = new int[size];

    // to do - set each element to the required value 
    //         by calling function getRandomInteger()    
    return array;
}



Remember also that there should be a corresponding delete [] to deallocate the memory when you are finished.

So the code in main() might look like this:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    // get value of size from user first of all

    int * IntegerArray = createArray(size);
    

    // add the other processing here

    //  release the dynamically allocated memory when finished
    delete [] IntegerArray;    
}


See the tutorial:
http://www.cplusplus.com/doc/tutorial/dynamic/

Last edited on
Topic archived. No new replies allowed.