I want to ask the user for the size of a 2D array arr[][], but also pass it through the function initializeArray. However, if I pass it through the function, I would have to have a size declarator for col, which doesn't allow the user to enter their own value for the size
#include<iostream>
using namespace std;
void initializeArray(arr[][10], int N);
int main() {
int N;
cout << "enter an array size: ";
cin >> N;
int arr[N][N];
initializeArray(arr, N); // I get an error here
for(int i = 0; i < N; i ) {
for(int j = 0; j < N; j )
cout << arr[i][j] << " ";
cout << endl;
}
}
void initializeArray(int arr[][10], int N) {
for(int i = 0; i < N; i )
for(int j = 0; j < N; j )
arr[i][j] = 0;
}
The only solution I found was the make arr[][] a global array, but in that case, I would have to still declare the size parameters, and I want the user to enter whatever they want. Is there another way to fix this?
CodePudding user response:
There are two problems with your approach.
First, in Standard C the size of an array must be a compile-time constant. So the following is not standard C in your example:
int N;
cout << "enter an array size: ";
cin >> N;
int arr[N][N];//not standard C because N is not a constant expression
Second, the parameter arr
of function initializeArray
is actually a pointer to an array of size 10
with elements of type int
i.e., int (*)[10]
.
This means that N
(of the array that you're passing in the call initializeArray(arr, N)
) must be 10
.
initializeArray(arr, N); //this only works for N = 10,assuming int arr[N][N] works say if N were a constant expression
Better would be to use a std::vector
as shown below:
int N;
std::cin >> N;
//-------------------------------------------------------v---->pass the value that you want the elements to have
std::vector<std::vector<int>> arr(N, std::vector<int>(N,0));//create a 2D std::vector
for(const auto& row:arr) {
for(const auto& col:row)
std::cout << col << " ";
std::cout << std::endl;
}
CodePudding user response:
You declared a variable length array
int N;
cout << "enter an array size: ";
cin >> N;
int arr[N][N];
//...
Variable length arrays are not a standard C feature.
However some compilers have their own language extensions that allow to define variable length arrays,
In this case the function should be declared at least like
void initializeArray( int n, arr[][n] );
In C you should use vectors instead of variable length arrays as for example
std::vector<std::vector<int>> arr( N, std::vector<int>( N ) );
In this case all elements of the type int
will be zero initialized. So you will not need to call the function to initialize elements of the type int
of the vectors to zero. And your program can look the following way
#include <iostream>
#include <vector>
int main()
{
size_t n;
std::cout << "enter the array size: ";
std::cin >> n;
std::vector<std::vector<int>> arr( n, std::vector<int>( n ) );
for ( const auto &row : arr )
{
for ( const auto &item : row )
{
std::cout << item << ' ';
}
std::cout << '\n';
}
}