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. }

advertisement
$ 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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn