Arrays and Functions

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

const double UPPER_BOUND = 100;
const double LOWER_BOUND = 0;
const int 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
}
Last edited on
Seed only once in your main() function. You won't get pseudo-random numbers.

Explanation: http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once

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.

Small example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstdlib>
#include <ctime>


void FillArray(int arr[], int size);
void PrintArray(int arr[], int size);

int main()
{
	const int Size = 10;
	srand(time(0));
	int arr[Size] = { 0 }; // Set all values in array to 0.

	PrintArray(arr, Size); // Showing original array
	FillArray(arr, Size); // Filling array with random numbers.
	PrintArray(arr, Size); // Re-printing array to show that we've modifed the array.
	std::cin.get();


}

void FillArray(int arr[], int size)
{
	for (int i = 0; i < size; i++)
	{
		arr[i] = rand() % 100 + 1;
	}
}

void PrintArray(int arr[], int size)
{
	for (int i = 0; i < size; i++)
	{
		std::cout << arr[i] << " ";
	}

	std::cout << std::endl;
}

Last edited on
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.
By returning the array I meant returning the pointer to the array. I edited my post to make it more clear
Topic archived. No new replies allowed.