A
A
avion2019-01-18 12:05:10
C++ / C#
avion, 2019-01-18 12:05:10

Const in C++ arrays?

Hello, why doesn't CLion require the size of an array to be const?

#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    int n;

    cout << "Num: ";
    cin >> n;

    int arr[n + 1];

    for (int &i:arr) {
        i = 1;
    }

    for (int k = 2; k*k <= n; k++) {
        if (arr[k] == 1) {
            for (int i = k * k; i <= n; i += k) {
                arr[i] = 0;
            }
        }
    }

     if (arr[n] == 1) {
         cout << "Prime number";
     }
     else {
         cout << "Composite number";
     }

     return 0;

This code works correctly, why doesn't it throw an error?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question