Fill arrays in separate function

Hello,

I am a total beginner in c++ and I need some help, with school homework.


In main I defined 2 dimensional array but it needs to be "filled" from seperate function(with user input). I know how to make this function read user input and everything, problem is I dont know how to make it write into array which is in other function.
Ok so this would "fill" the array

1
2
3
4
5
6
7
 for(int i=0; i<5;i++){
        for(int j=0;j<7;j++){
            cout<<"competitor :"<<i+1<<" task :"<<j+1<<endl;
            cin>>tabela[i][j];
                   
        }
    }


but my definition of array has to be in main and this code to fill it has to be in some other function. How can I make this function write in main functions array ? thanks
Why don't you pass the array as argument to the function.

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

const int N = 10;

void fill_array(int arr[N][N])
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			std::cin >> arr[i][j];
		}
	}
}

int main()
{
	int arr[N][N];
	fill_array(arr);
}
Thank you for your help, works like a charm
Topic archived. No new replies allowed.