0

I tried to write this code but it says expression did not evaluate to a constant. I learn that this is because VS does not allow an undeclared array, as "n" is not understood by VS. How can i fix this code with a declared array?

#include<iostream>
using namespace std;


int main()
{

    int i, n;
    cout << "Enter size of array:";
    cin >> n;
    int a[n];
    cout << "Enter elements of array:" << endl;

    for (i = 0; i < n; i++)
        cin >> a[(i + n - 1) % n];

    cout << "Result after left shift:" << endl;
    for (i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;


    return 0;
}
  • 4
    `int a[n];` In standard `c++` the number of elements `n` must be a compile time constant. This differs from modern `c` where VLAs can be used. [https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – drescherjm May 11 '20 at 21:13
  • That code is not legal. The size of a C-style array must be a compile-time constant. Use `std::vector` instead. – Pete Becker May 11 '20 at 21:14

3 Answers3

2

How can i fix this code with a declared array?

Option 1

Declare the array with sufficiently large size and make sure that n is less than or equal to the size before using the array.

 int i, n;
 int a[1000];
 cout << "Enter size of array (less than or equal to 1000):";
 cin >> n;
 if ( n > 1000 )
 {
    // Deal with the problem.
 }
 else
 {
    // Use the array.
 }

Option 2

Use std::vector.

 int i, n;
 cout << "Enter size of array:";
 cin >> n;
 std::vector<int> a(n);
R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

Variable length arrays (VLAs) are not part of the C++ language, although some compilers (like g++) support them as an extension.

You should be using the std::vector container from the Standard Template Library, instead. Once declared and properly initialized, a std::vector can be used much like a plain array:

#include<iostream>
#include <vector>
using std::cout; using std::cin; using std::endl;

int main()
{
    int i, n;
    cout << "Enter size of array:";
    cin >> n;
    std::vector<int> a(n);
    cout << "Enter elements of array:" << endl;

    for (i = 0; i < n; i++)
        cin >> a[(i + n - 1) % n];

    cout << "Result after left shift:" << endl;
    for (i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;


    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

You have to allocate the array on the heap like this:

int* a = new int[n];

But when you do heap allocations, always remember to delete the allocated memory after you are done using it:

delete[] a;

If you don't want to worry about deleting the memory, you can look into std::vector.

  • Sorry I was thinking heap but typed the wrong thing. I edited it. – Ranjeeth Mahankali May 11 '20 at 21:54
  • Side note: Even if you do remember to `delete` you're going to find cases where it STILL doesn't get deleted. There's an unexpected path through the code. An exception got thrown. The code never gets to the delete. Dozens more very broad reasons. `new` and `delete` is just so error prone that it's not worth using most of the time. – user4581301 May 11 '20 at 23:26