0

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;
            }
    }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

2

Your function declarations and your function invocations are both wrong.

You need to change this:

void print( string, double, int );
void sort( double, int );

To this instead, which matches your function implementations:

void print( string, double[], int );
void sort( double[], int );

And, you need to change this:

print( "Random Values", array[], num );
sort( array[], num );

To this instead:

print( "Random Values", array, num );
sort( array, num );

There are other problems with your code:

In main(), you are creating array as a variable-length array, which is a non-standard feature that you should avoid. When you need an array whose size is not known until runtime, use std::vector instead.

Also, in randDouble() and buildRandom(), you are re-seeding the RNG with the same seed every time, so rand() will return the same value every time. You are not going to get any random values at all. You need to call srand() only one time, ie at the beginning of main(). A better solution would be to not use rand() at all, but instead use C++-style RNGs from the <random> library.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770