Hey! At a loss here. So i have to create different functions for a main that will create an array and sort the array. What I have so far is below. My confusion comes at the build array function, on how to store the random values in the array.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
usingnamespace std;
constdouble UPPER_BOUND = 100;
constdouble LOWER_BOUND = 0;
constint ARRAY_SIZE = 100;
double randDouble();
int buildArray( double array[] );
void printArray( double array[], int numberOfValues, int numberPerLine );
void sortArray( double array[], int numberOfValues );
int main()
{
double AR[ARRAY_SIZE];
int numberElements = 0, lineValues = 0;
srand(time(NULL));
cout << "How many values should be displayed per line? ";
cin >> lineValues;
return 0;
}
double randDouble() //Function that will create the random doubles to be
{ //put in the array
double randomDouble;
randomDouble = LOWER_BOUND + ( rand() % 100 + 1 / ( RAND_MAX / ( UPPER_BOUND - LOWER_BOUND)));
return randomDouble;
}
int buildArray(double array[]) //Creates the array. Also creates a random
{ //number from 20 to 100, that will decide how many values go into the array
int i = 0, numberElements = 0;
while(numberElements < 20)
{
numberElements = rand() % 100 + 1;
}
for(i = 0; i < numberElements; i++) //Should place the random values //in array
{
array[i] = randDouble();
}
return i;
}
void sortArray(double array[], int numberOfValues)
{
//will sort the values from the array from lowest to greatest
}
void printArray(double array[], int numberOfValues, int numberPerLine)
{
//will print the array
}
In your buildArray function, you are returning the variable i and not the array you are filling. When passing an array to a function, you are actually passing the address of the 0th element of the array to the function which is what the pointer points to.
You can either have it return the pointer to the array or change the function into a void function. You also want to pass a size parameter like you do in your sortArray function.
Okay, made the change to only seed in main().
So how can I have it return the array? I've tried, but no luck.
I can't pass it another parameter either, I'm stuck with the ones given to me.