Is it possible to allow the user to enter the size of an array with a keyboard?
I know that arrays can't change size. The only solution I could think of is this:
int userSize;
cin >> userSize;
const int SIZE = userSize;
int array[SIZE];
How can I verify that this works? Should I use a vector instead?
To ask the user to enter the size of an array use: int size; cout<<"enter size of the array"; cin>>size; You can then use a for loop to have the user enter the values of the array.
In C++, we use sizeof() operator to find the size of desired data type, variables, and constants. It is a compile-time execution operator. We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);
To take input of an array, we must ask the user about the length of the array. After that, we use a Java for loop to take the input from the user and the same for loop is also used for retrieving the elements from the array.
You can declare one-dimensional (1D) arrays with any non-negative size. int [] arr = new int[ 10 ]; // Array of size 10 int [] arr2 = new int[ 100 ]; // Array of size 100 int [] arr3 = new int[ 1 ]; // Array of size 1 int [] arr4 = new int[ 0 ]; // Array of size 0!
Variable Length Arrays are not approved by C++ standard. C++ Standard mandates that the size of an array must be an compile time constant.
You can use VLA's through compiler extension provided in gcc but note the your code is not portable if you do so.
Standard approved way of doing this is to use std::vector.
Many of the other answers are correct, but to show your possible choices. Each one will fit into different situations better.
The first one is most similar to your code. It makes a constant size array and lets the user choose how much of it to use. This is simple, but limiting. It can waste unused space, but has it places to be used. (not likely based on user input though)
const int SIZE=100;
int array[SIZE];
int userSize;
cin >> userSize;
if (userSize>SIZE){
cerr << "Array requested too large";
}
// use userSize as size of array
The second choice is dynamic memory allocation and using a regular C pointer to keep track of it. If you forget (or are unable) to delete it you have a memory leak.
cin >> userSize;
int* array = new int[userSize];
...
delete [] array;
The third choice is writing your own smart pointer, boost, or in C++ 0x a unique_ptr
. It will keep track of the pointer for you and delete it when it goes out of scope.
cin >> userSize;
unique_ptr<int[]> array(new int[userSize]);
And finally you probably just want a vector. As you hinted to in your question, a vector is probably the way to go here. They are simple, efficient, and commonly used. Learning one stl container makes learning the others easier. You should research which container fits your current problem best. Vector is a good choice most of the time.
#include <vector>
...
std::vector<int> vec;
cin >> userSize;
vec.resize(userSize);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With