What are different ways of initializing the size of an array with user input in c++

arrays in c
user input array size c++
how to input an array of unknown length in c++
store user input in array c
array in c pdf
user input array in c
user defined array size - c++
dynamic array in c

I was wondering what are some alternatives to doing the following code snippet in c++.

int i;
cin >> i;
int arr[i];

I have recently started looking into competitive programming and trying to learn more.

EDIT: For those comments about this not being cpp. It compiles successfully with the makefile I am using in my class which is using gcc -std=c++11 -o a.exe main.cpp and returns to the console when I input a length of 1

array length: 1

You should use std::vector instead in C++, e.g.

int i;
cin >> i;
std::vector<int> arr(i);

BTW: VLA is not supported by C++ standard. Also see Variable Length Array (VLA) in C++ compilers

How to give an array size through a user input in C/C++, Variable length array #include <stdio.h> int main() { int size; scanf(“%d” How do you initialize an array of structures in C? printf("input size of array "); now a user input can never be a constexpr because either we use cin or any other� How to declare an array in C? Data_type array_name[size_of_array]; For example, float num[10]; Below are some of the different ways in which all elements of an array can be initialized to the same value: Initializer List: To initialize an array in C with the same value, the naive way is to provide an initializer list. We use this with small arrays.

One way is:

int i = 0;

cin >> i;

auto arr = std::make_unique<int[]>(i); // #include <memory>
        // Or: std::make_shared<int[]>(i);

Another way is to use std::vector:

int i = 0;

cin >> i;

std::vector<int> arr(i); // #include <vector>

C Arrays (With Examples), You will learn to declare, initialize and access array elements of an array with the help If the size of an array is n , to access the last element, the n-1 index is used. Here's how you can take input from the user and store it in an array element. Sometimes you might get an error and some other time your program may run� 'C' also allows us to initialize a string variable without defining the size of the character array. It can be done in the following way, char first_name[ ] = "NATHAN"; The name of a string acts as a pointer because it is basically an array. String Input: Read a String. When writing interactive programs which ask the user for input, C provides

Your code is not C++. It uses a C language feature (from the C99 version of the C standard) called "variable-length arrays" - where arrays on the stack can have a length determined at run-time.

Variable-length arrays are considered are dangerous and considered a rather bad idea; see:

Why aren't variable-length arrays part of the C++ standard?

If you compile your code while telling the compiler to only accept standard-compliant code, it will fail: gcc -std=c++11 -pedantic-error. Try it on GodBolt.

In C++ the size of an array is set at compile-time, period. Like other users suggested, you can use dynamic allocation to obtain a run-time-determined-length contiguous area in memory, of your choice of size: Using std::vector, std::unique_ptr, std::shared_ptr or even plain allocation with new (although the latter is not recommended).

Two dimensional (2D) arrays in C programming with example, For now don't worry how to initialize a two dimensional array, we will discuss that This program demonstrates how to store the elements entered by user in a 2d array dimensional array) during declaration, we need not to specify the size of it. We can calculate how many elements a two dimensional array can have by� Write a C program to input elements in array and print array. How to input and display elements in an array using for loop in C programming. Learn C programming, Data Structures tutorials, exercises, examples, programs, hacks, tips and tricks online.

Arrays in C programming with examples, For example an int array holds the elements of int types while a float array holds int x=0; /* Array- declaration – length 4*/ int num[4]; /* We are using a for loop to just declared the array and later we initialized it with the values input by user. learn how to declare, read and write data in 2D array along with various other� The java.util.Arrays class has several methods named fill() which accept different types of arguments and fill the whole array with the same value: long array[] = new long[5]; Arrays.fill(array, 30); The method also has several alternatives which set a range of an array to a particular value: int array[] = new int[5]; Arrays.fill(array, 0, 3, -50);

C program to declare, initialize, input and print array elements , C program to input and print array elements using loop. Example. Input. Input size: 10 Input elements: 1 2 3 4 5 6 7 8 9 10. Output How to initialize an array? It is possible to initialize an array during declaration. For example, int mark[5] = {19, 10, 8, 17, 9}; You can also initialize an array like this. int mark[] = {19, 10, 8, 17, 9}; Here, we haven't specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements. Here,

C Programming Course Notes - Arrays, The array does NOT change sizes later if the variable used to declare it changes. ) Arrays may be initialized when they are declared, just as any other variables. Array indices start at zero in C, and go to one less than the size of the array. Here we are learning the different ways of compile time initialization of an array. Ways Of Array Initializing 1-D Array : Size is Specified Directly; Size is Specified Indirectly; A. Method 1 : Array Size Specified Directly . In this method , we try to specify the Array Size directly. int num [5] = {2, 8, 7, 6, 0};

Comments
  • You can also use something like this: int number = 0; while (cin >> number) { myVector.push_back(number); }, but, here, you are specifying the size directly.
  • This code snippet is not valid C++; many compilers allow variable-length arrays as an extension, but in standard C++ you need to know the size at compile time unless you want to mess with dynamic arrays using new[].
  • What are different ways... -- The way you're showing us in the question isn't C++, so you can forget about the way you're showing us in your question.
  • Learn C++, then start competing.You are at a serious competitive disadvantage if you try to do it the other way around. Here is a list of good references.
  • In CP I usually allocate all my arrays with fixed sizes set at the maximum specified in the problem.
  • This should be the accepted answer. Don't know why the OP hasn't done so.
  • It seems like the code snippet above works because of my gcc version and not specifically the c++ explanation
  • @PaulMcKenzie - the OP probably won't accept the answer because of a mistaken belief that "gcc accepts his code with VLAs so the code is correct".
  • This is why I wished gcc (and other) compilers would turn the VLA option off by default. Many new programmers have the mistaken belief that their "VLA" code is correct when it really isn't.