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/