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
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.