

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- C++ program to implement a variable sized array
- C program to demonstrate usage of variable-length arrays
- C++ Program to Implement Sorted Array
- C++ Program to Implement Parallel Array
- C++ Program to Implement Bit Array
- C++ Program to Implement Sparse Array
- C++ program to implement Run Length Encoding using Linked Lists
- C++ Program to Implement Stack using array
- C++ Program to Implement Queue using Array
- C++ Program to Implement Array in STL
- Program to implement run length string decoding iterator class in Python
- Variable Length Argument in C
- Variable-length arguments in Python
- Demonstrating variable-length arguments in Java
- Does C++ support Variable Length Arrays
Advertisements