1

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?

Mysterai
  • 19
  • 3
  • Can't be done in Standard C++ unless you use `new` to make a dynamic array of arrays or use `std::vector`. Trust me, use `std::vector`. If you are not allowed to because of assignment restrictions, write your own simple `vector`. – user4581301 May 09 '22 at 17:24
  • `int N; std::cin >> N; int arr[N][N];` is not standard C++. Use `std::vector` instead. – Jason May 09 '22 at 17:24
  • Even if this were supported, it should stand out that you try to make an array with dimensions `N, N`, and pass it to something that - you expect - can handle an array with dimensions `(anything), 10`. Of course it doesn't matter for the first dimension what N is equal to, but for the second dimension, what if it isn't equal to 10? Now, keep in mind that these kinds of type checks are performed before the program runs. – Karl Knechtel May 09 '22 at 17:26
  • `cin >> N;int arr[N][N];` -- You will be surprised that this is not valid C++. Unless you have a predetermined (compile-time) size, and your application knows about these sizes, then traditional arrays cannot be used -- it's as simple as that. You have to use some other means, such as `std::vector`, or less-advised `new[] / delete[]` (preferably wrapped in your own home-made vector class, as already suggested). – PaulMcKenzie May 09 '22 at 17:29
  • [Here is a simple `vector`-based approach](https://stackoverflow.com/a/2076668/4581301). And [here's how to do it with `new`](https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op). Much more complicated. – user4581301 May 09 '22 at 17:30
  • Good to read: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Ted Lyngmo May 09 '22 at 17:42

2 Answers2

1

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;
 }

Demo

Jason
  • 36,170
  • 5
  • 26
  • 60
1

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';
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335