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.
#include <iostream>
#include <cmath>
#include <array>
usingnamespace std;
double *f1(double ar1[])
{
double ran = rand() % 100 + 0;
constint size = 10;
ar1[size] = { ran };
for (int x = 0; x < 10; x++)
{
return(&ar1[x]);
}
}
int main()
{
constint 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;
}
}
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);
}
// 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 = newint[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 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 (constauto& 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