0

I've written a problem for an algorithm problem. I'm new to C++, and I'm getting the following error message when I try to run my code: "array initializer must be an initializer list". Here's the code itself:

#include <iostream>
#include <algorithm>
#include <array>
using namespace std;

int main(){
    int n;
    cin>>n;
    int a[n][2];
    int b[n];

    for(int i=0;i<n;i++){
        cin>>a[i][0];a[i][1]=i;
        b[i]=a[i][0];
    }

    sort(a,a+n);

    for(int i=1;i<n;i++)
    {
        if(a[i][0]<=a[i-1][0]){
            a[i][0]=a[i-1][i]+1;
            b[i]=a[i-1][i]+1;
        }
    }

    for(int i=0;i<n;i++)
        cout<<b[i]<<" ";

}

I don't know why I'm getting this error message. I've Googled it and couldn't find anything useful. If someone could explain to me why I'm getting this message and how to solve it, I'd really appreciate it. Thanks in advance.

discovery
  • 43
  • 6
  • Use `std::array` with a sort functor. – Ghasem Ramezani Aug 30 '21 at 17:19
  • 3
    What line is the error reported on? Variable length arrays eg `int a[n][2];` are a compiler extension and may not be available with the compiler you are using. Have a look here - live - https://godbolt.org/z/vePbT5GMT – Richard Critten Aug 30 '21 at 17:20
  • 1
    VLAs are not allowed in c++ https://stackoverflow.com/questions/39334435/variable-length-array-vla-in-c-compilers which is probably the issue (though the error message seems a bit odd) – cigien Aug 30 '21 at 17:20
  • 1
    For those wondering, it's the `sort(a,a+n);` line that's triggering the error. Because how do you know if an `int[2]` is ordered before a different `int[2]`? – Drew Dormann Aug 30 '21 at 17:21
  • The error message (as I could reproduce it) was from this call to sort: `sort(a,a+n);` It looks like the library function is having a hard time initializing the iterator from pointer arithmetic. – GandhiGandhi Aug 30 '21 at 17:23
  • @discovery the compiler does not know how you want `a` to be sorted. Neither do we. If `a` contains `{ {1, 0}, {0, 1} }` is that sorted? How did you determine that? It may help to [edit] your question to describe what you wanted that problematic line to do. – Drew Dormann Aug 30 '21 at 17:28
  • @cigien I don't understand why C++ wouldn't allow such a thing. What alternative do I have? – discovery Aug 30 '21 at 17:39
  • The problem is coming from the fact that `int[2]` does not satisfy the [ValueSwappable](https://en.cppreference.com/w/cpp/named_req/ValueSwappable) requirement. As noted by others, using `std::array` instead of `int[2]` would solve that problem. Changing the line from `int a[n][2];` to `std:array a[n];` gets the program to compile, although it is still nonstandard. (Variable-length arrays are permitted by gcc but are not standard-conforming.) – Raymond Chen Aug 30 '21 at 17:41
  • Use a `std::vector` if you don't know the size up front (at compile time). – cigien Aug 30 '21 at 17:41

2 Answers2

1
cin>>n;
int a[n][2];

The size of an array variable must be compile time constant. n is not compile time constant. The program is ill-formed.

What alternative do I have?

If you want a runtime size array, then you must create the array in dynamic storage. Simplest approach is to use std::vector. Elements of vector cannot be arrays, but you can use std::array as the element type instead. Example:

std::vector<std::array<int, 2>> a;
sort(a,a+n);

a is an array of arrays. Elements of tha array are themselves arrays. The arguments decay to be pointers to arrays. Pointer to array does not satisfy the requirement of being "value swappable" that is required by std::sort. Iterators to std::array are "value swappable", so this issue is solved by using that as the inner array as suggested above.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

If you want to create an array, the size must be constant number or you should use vector instead. In addition, compiler says you have to assign your array to 0 with curly brackets like that:

int a[5][2]={{0}};
int b[5]={0};