Home>

there is a cycle

#include <
iostream >
Using Namespace STD;
int n;
CIN >
≫
 n;
for (int i= 0; i <
 n; I ++) {
    int a;
}

But the problem is that the same variable is created, and it is necessary to assign different names to them. Tell me please

Use an array of dynamic size.

user78606702021-04-07 18:03:12

What is the essence of the question? Why assign different names to variables in a cycle?

αλεχολυτ2021-04-07 19:46:24
c++
  • Answer # 1

    NO When the variable comes out of the scope (cycle), it is destroyed.

    The most optimal option is to create a dynamic array on a pile. int * arr= new int [n]; . Create a pointer to int. under which allocate through the operator new N. cells.

    You can also static, but then you need to compiler to know the size of the array during compilation, that is, you will not be able to write such INT ARR [N] , because N. Not a constant. Then it will be necessary to create the number of elements in advance, for example, for a hundred cells (to reserve). But it should not be done.

    Thank you, but how does the dynamic array help create n variables?

    AlexMurkin2021-04-07 18:47:53

    Well, the array of size N is this and there are n variables of the same type (by definition)

    KoVadim2021-04-07 20:07:43

    And how to fill an array through CIN? Sorry if a stupid question

    AlexMurkin2021-04-08 03:38:30

    For (int i= 0; i >Miassive name [i]; }

    ТарасПрогер2021-04-08 04:40:49
Trends