### How does one pass an Array to a Function?

A program I am working on requires me to:

"Write a three function program. The main function of the program should create an array that can store 10 integers and then pass the array to another function that gets the integers from the user and stores them in the array and finally calls a third function that displays the elements in the array in the reverse order. You may not use global variables. Pass the array and the number of elements in the array as arguments to both of the called functions."

I had no trouble creating the array and passing the inputs through a function <i>into</i> an array, but I just can't seem to grasp the concept of passing an array through a function.

Nothing I have tried seems to work.

I'm a very beginner student, so anything outside the most basic commands is beyond my reach, most likely.

My code is currently:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````int numGet(int); #include using namespace std; int main() { const int NUM_INT = 9; double numbers[NUM_INT]; int input; int output; for (int count = 0; count <= NUM_INT; count++) { numbers[count] = numGet(input); } cout << "Your numbers reversed are: " << endl; for (int count = 9; count >= 0; count--) { cout << numbers[count]; if (count != 0) { cout << ", "; } else { cout << "."; } } } int numGet(int input) { int input2; cout << "Please input a number: "; cin >> input2; return input2; }``````
should give you an idea

 ``123456789101112131415`` ``````int func(int A[2]); int func(int A[2]) { return (A[0] + A[1]); } int main () { int A[2] = {1,2}; int j = func(A); }``````
I honestly have no idea what I'm looking at there, sorry.

Could you explain it a little bit?
you look at an example for a function, that uses an array as parameter.

i thought that's what this topic is about.

if you are totally stuck with that programm i can give you the code. but i assume you dont learn much that way
Last edited on
Well, my first function needed to make the array (double numbers(NUM_INT)), and then the second function would need me to pass the array into it, and then save the input numbers to it from there, passing it to the 3rd, where it will be output in reverse order.
give me a minute or 2, to finish this
<3. Could I message you via something and ask you a few more questions about string arrays as well?

I've understood every single thing this semester, but the combination of arrays and functions has left me stopped in my tracks.
you can also just start a new topic
Word, I just don't want to be that guy who's asked 3 questions in 3 hours.
 ``12345678910111213141516171819202122232425262728293031`` ``````#include void get_input(int A[], int size); void display_in_reverse_order(int A[], int size); void get_input(int A[], int size) //second function { for(int i = 0; i < size; ++i) { std::cout << "Number " << i+1 << ":"; std::cin >> A[i]; std::cout << std::endl; } display_in_reverse_order(A, size); } void display_in_reverse_order(int A[], int size) //third function { for(int i = size-1; i>= 0; --i) { std::cout << "Number " << i+1 << " = " << A[i] << std::endl; } } int main () //first function { int A[10]; get_input(A, sizeof(A)/sizeof(A[0])); //you could also write 10, instead of the sizeof stuff }``````

if there is anything not clear, just ask
Last edited on
Topic archived. No new replies allowed.