Hi there,
I'm having a problem with the code below:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void setArray( float [], const int Size );
void displayArray( const float * );
int main()
{
const int arraySize = 20;
float array[ arraySize ];
// Initialise the array:
setArray( array, arraySize );
displayArray( array );
}
void setArray( float a[], const int Size )
{
srand( time(0) );
for ( int i = 0; i < Size; i++ )
a[ i ] = 1 + rand() % 50; // Number between 1 and 50
}
void displayArray( const float *aPtr )
{
for ( ; *aPtr != '\0'; aPtr++ )
cout << *aPtr << endl;
}
It's meant to generate and fill and array with 20 randomly generated FLOATING POINT numbers between 1 and 50 and then print the array using pointers. I get a very strange output:
20
50
14
7
35
24
45
26
26
31
38
13
3
29
32
29
30
5
8
5
4.59024e+33
5.95096e-39
3.21401e-39
2.8026e-44
5.95096e-39
NaN
3.21409e-39
5.88403e-39
1.4013e-45
3.86253e-39
3.86527e-39
6.27351e-39
3.21407e-39
NaN
3.21407e-39
4.48515e+33
5.87747e-39
3.86527e-39
Please help! Thanks :)