
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Variable Length Array
Variable length arrays can have a size as required by the user i.e they can have a variable size.
A program to implement variable length arrays in C++ is given as follows −
Example
#include <iostream> #include <string> using namespace std; int main() { int *array, size; cout<<"Enter size of array: "<<endl; cin>>size; array = new int [size]; cout<<"Enter array elements: "<<endl; for (int i = 0; i < size; i++) cin>>array[i]; cout<<"The array elements are: "; for(int i = 0; i < size; i++) cout<<array[i]<<" "; cout<<endl; delete []array; return 0; }
The output of the above program is as follows −
Enter size of array: 10 Enter array elements: 11 54 7 87 90 2 56 12 36 80 The array elements are: 11 54 7 87 90 2 56 12 36 80
In the above program, first the array is initialized. Then the array size and array elements are requested from the user. This is given below −
cout<<"Enter size of array: "<<endl; cin>>size; array = new int [size]; cout<<"Enter array elements: "<<endl; for (int i = 0; i < size; i++) cin>>array[i];
Finally, the array elements are displayed and the array is deleted. This is given below −
cout<<"The array elements are: "; for(int i = 0; i < size; i++) cout<<array[i]<<" "; cout<<endl; delete []array;
- Related Articles
- C++ Program to Implement Parallel Array
- C++ Program to Implement Sorted Array
- C++ Program to Implement Bit Array
- C++ Program to Implement Sparse Array
- C program to demonstrate usage of variable-length arrays
- C++ Program to Implement Queue using Array
- C++ Program to Implement Stack using array
- C++ Program to Implement Array in STL
- C++ program to implement Run Length Encoding using Linked Lists
- Program to implement run length string decoding iterator class in Python
- Java Program to implement Binary Search on char array
- Java Program to implement Binary Search on long array
- Java Program to implement Binary Search on double array
- Java Program to implement Binary Search on float array
- Java Program to implement Binary Search on an array

Advertisements