How to pass an array to user function.

I need create an array and pass it to several different user defined functions and then pass that information back to the main to be displayed.
I have found you can not pass an array in C++ but you can use a pointer.
How can I use a pointer for my problem. I have read many examples but none that relate to mine.

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 <cmath>
#include <array>
using namespace std;

double *f1(double ar1[])
{
	double ran = rand() % 100 + 0;
	const int size = 10;
	ar1[size] = { ran };
	for (int x = 0; x < 10; x++)
	{
		return(&ar1[x]);
	}	
}

int main()
{
	const int size = 10;
	int choice = 0;
	double a = 0;
	cout << "Main Menu:" << endl;
	cout << "1. Create a new array with random elements." << endl;
	cout << "2. Display the elements of the array." << endl;
	cout << "3. Find and display the largest value in the array." << endl;
	cout << "4. Find and display the smallest value in the array." << endl;
	cout << "5. Sort the array in descending order and display the array." << endl;
	cout << "6. Find and display the mean value." << endl;
	cout << "7. Find and display the median value." << endl;
	cout << "8. Find and display the variance." << endl;
	cout << "9. End" << endl;
	cin >> choice;

	switch (choice)
	{
		case 1:
			cout << "%d" << *f1;
			break;
	}
}
Last edited on
In C/C++ an array is effectively passed to a function as a pointer, yes. That pointer is pointing to the base address of the array. The pointer can be used to access the array from inside of the function. Any modifications you make to the array (via the pointer) inside of the function will be reflected in the original array.

1
2
3
4
5
6
7
8
9
10
11
void foo(int *array)
{
    array[3] = 42;
}

int main()
{
    int array[5];
    foo(array);
    printf("%d\n", array[3]);
}


Note: You will often see that the size of the array is passed to the function as a separate parameter, because otherwise the function has no way to "know" the size – unless the size is fixed.

1
2
3
4
5
6
7
8
9
10
11
12
13
void bar(int *array, const size_t n)
{
    for (size_t i = 0; i < n; ++i)
    {
        array[i] = i;
    }
}

int main()
{
    int array[42];
    bar(array, 42);
}


Note²: In C++, consider using the std::vector (or std::array) class instead of a "raw" C array.
https://www.cplusplus.com/reference/vector/vector/vector/#example
Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
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
// filling a variable sized array with random numbers the C Way

#include <iostream>
#include <cstdio>
#include <ctime>

void f1(int arr[], size_t size)
{
   for (int x = 0; x < size; x++)
   {
      // generate a random number between 0 & 99
      arr[x] = rand() % 100 + 0;
   }
}

int main()
{
   // seed the random generator with the current clock time
   srand(static_cast<unsigned>(time(0)));

   std::cout << "What size is your array? ";
   size_t arr_size;
   std::cin >> arr_size;

   // create a dynamic array on the heap
   int* arr = new int[arr_size];

   // fill the array with random values
   f1(arr, arr_size);

   // display the array
   for (size_t i { }; i < arr_size; ++i)
   {
      std::cout << arr[i] << ' ';
   }
   std::cout << '\n';

   // it pays to be neat, delete the heap memory
   delete[] arr;
}
What size is your array? 15
66 31 89 57 30 59 92 48 31 55 35 94 27 61 66
Filling a C++ container with random real numbers:
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
// filling a vector with random number, one of the C++ Ways

// C++ makes generaing non-integral random numbers MUCH easier

#include <iostream>
#include <random>
#include <vector>

// a function that fills a vector with random numbers
// the vector is passed as a reference
void f1(std::vector<double>& vec)
{
   // create a C++ random engine, seeding it with std::random_device
   // it is static so the engine is created only once and reused
   static std::default_random_engine rng(std::random_device{}());

   // create a real number (double) distribution 0 - 99
   std::uniform_real_distribution<double> dis(0, 99);

   // loop through the vector
   for (size_t i { }; i < vec.size(); ++i)
   {
      vec[i] = dis(rng);
   }
}

int main()
{
   // create an empty vector
   std::vector<double> vec;

   std::cout << "How big is your vector? ";
   size_t vec_size;
   std::cin >> vec_size;

   // create elements for the random numbers
   vec.resize(vec_size);

   f1(vec);

   // display the vector's contents using a range-based for loop
   for (const auto& itr : vec)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';
}
How big is your vector? 7
4.92967 12.4709 60.2464 12.0015 44.3592 90.7727 19.8549
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
#include <iostream>
#include <random>
#include <vector>

void f1(std::vector<double>& vec) {
	static std::default_random_engine rng(std::random_device {}());
	const std::uniform_real_distribution<double> dis(0, 99);

	for (auto& v : vec)
		v = dis(rng);
}

int main() {
	size_t vec_size {};

	std::cout << "How big is your vector? ";
	std::cin >> vec_size;

	std::vector<double> vec(vec_size);

	f1(vec);

	for (const auto& itr : vec)
		std::cout << itr << ' ';

	std::cout << '\n';
}

the function has no way to "know" the size – unless the size is fixed.


Not true in C++. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

template<size_t N>
void show(const int(&arr)[N]) {
	for (const auto a : arr)
		std::cout << a << ' ';

	std::cout << '\n';
}

int main() {
	const int arr[] {1,2,3,4,5,6};

	show(arr);
}



1 2 3 4 5 6

Topic archived. No new replies allowed.