C++ Program to Implement Variable Length Array

This C++ Program demonstrates the implementation of Variable length array.

Here is source code of the C++ Program to demonstrate the implementation of Variable length array. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Variable length array
  3.  */
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int *array, size;
  11.     cout<<"Enter size of array: ";
  12.     cin>>size;
  13.     array = new int [size];
  14.     for (int i = 0; i < size; i++)
  15.     {
  16.         cout<<"Enter an integer to be inserted: ";
  17.         cin>>array[i];
  18.     }
  19.     for(int i = 0; i < size; i++)
  20.     {
  21.         cout<<array[i]<<"  ";
  22.     }
  23.     cout<<endl;
  24.     delete []array;
  25.     return 0;
  26. }

$ g++ variablelength_array.cpp
$ a.out
 
Enter size of array: 5
Enter an integer to be inserted: 3
Enter an integer to be inserted: 4
Enter an integer to be inserted: 2
Enter an integer to be inserted: 5
Enter an integer to be inserted: 1
3  4  2  5  1
 
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.