I wrote a program that includes one main()
function and 4 calling functions.
I start off with a random number generator to find the max value of an array. I assign the max value to the array. I find the specified amount of random numbers and put them into the array.
I now want to pass the array to a void
function that prints the array. Instead, I get an error saying:
line 61 [Error] expected primary-expression before ']' token
And the same thing for line 65.
What should I do in order to pass an array to my void
print function?
Line 61: print( "Random Values", array[], num ); //prints the array
Line 65: sort( array[], num ); //sorts then prints the array
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
const double lowerbound = 10;
const double upperbound = 950;
const int minimumvalues = 75;
const int maximumvalues = 150;
const int maximumvalues_file = 40;
const int maxperline = 12;
/********* Put the function prototypes below this line *********/
double randDouble();
int buildRandom();
void print( string, double, int );
void sort( double, int );
int main()
{
int loopcntrl, num;
double randnum;
num = buildRandom(); //assigns the random max value for the array to num
double array[num];
for ( loopcntrl = 0; loopcntrl <= num; loopcntrl++) //assigns values to the array
{
randnum = randDouble();
array[loopcntrl] = randnum;
}
print( "Random Values", array[], num ); //prints the array
cout << endl;
sort( array[], num ); //sorts then prints the array
cout << endl;
return 0;
}
/***************************************************************
Function: double randDouble()
Use: this function chooses the numbers that are in the array using
a random value generator.
Returns: a random number.
***************************************************************/
double randDouble()
{
srand(19);
double num;
num = lowerbound + ( rand() / maximumvalues / ( upperbound - lowerbound ));
return ( num );
}
/***************************************************************
Function: int buildRandom()
Use: This function assigns the array to a specific amount of elements
Returns: a number that shows the total amount of numbers in an array.
***************************************************************/
int buildRandom ()
{
srand(19);
int num;
num = minimumvalues + ( rand() % ( maximumvalues - minimumvalues + 1 ));
return ( num );
}
/***************************************************************
Function: void print( string, double, int)
Use: This function prints out the array.
Returns: Nothing is returned.
***************************************************************/
void print( string RandomValues, double array[], int numberOfValues )
{
int loopcntrl;
double num;
cout << RandomValues << endl << endl;
for (loopcntrl = 0; loopcntrl <= (numberOfValues-1); loopcntrl++)
{
num = array[loopcntrl];
//checks if 12 digits per line
//it ends the line if there are
if ( (loopcntrl % 12) == 0)
{
cout << endl;
}
cout << setw(9) << right << fixed << setprecision(3) << num;
}
}
/***************************************************************
Function: void sort( double, int)
Use: This function sorts the array from smallest to largest.
Then it prints the array
Returns: Nothing is returned.
***************************************************************/
void sort( double array[], int numberOfValues )
{
int i, ii, min;
double num, temp;
for ( i = 0; i <= (numberOfValues - 1); i++)
{
min = i;
for (ii = i+1; ii < numberOfValues; ii++)
if (array[ii] < array[min])
{
min = ii;
}
if (min = i)
{
temp = array[min];
array[min] = array[i];
array[i] = temp;
}
}
cout << "Sorted Random Values" << endl << endl;
for (int a = 0; a <= (numberOfValues-1); a++)
{
num = array[a];
if ( (a % 12) == 0)
{
cout << endl;
}
cout << setw(9) << right << fixed << setprecision(3) << num;
}
}