Creation and Deletion of Array Example in C++


Example Program:

Here is the simple program for printing the creation and deletion of array example in C++.

Source Code Example : 1

#include<iostream>
using namespace std;
int main()
{
  int n;
  int *arr=new int[n];
  cout<<"Enter the size of the array : ";
  std::cin>>n;
  cout<<"\nEnter the element : ";
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  cout<<"\nThe elements that you have entered are :";
  for(int i=0;i<n;i++)
  {
    cout<<arr[i]<<",";
  }
  delete arr;
  return 0;
}
To download raw file Click Here

Output

Enter the size of the array : 4
Enter the element : 12
13
14
14
The elements that you have entered are :12,13,14,14,

Source Code Example : 2

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
  int num;
  cout<<"Enter total number of students: ";
  cin>>num;
  float* ptr;
  ptr = new float[num];
  cout<<"Enter GPA of students."<<endl;
  for (int i=0;i<num;++i)
  {
    cout<<"Student"<<i+1<<": ";
    cin>>*(ptr+i);
  }
  cout<<"\nDisplaying GPA of students."<<endl;
  for (int i=0;i<num;++i)
  {
    cout<<"Student"<<i+1<<" :"<<*(ptr + i)<<endl;
  }
  delete [] ptr;
  return 0;
}
To download raw file Click Here

Output

Enter total number of students: 3
Enter GPA of students.
Student1: 89
Student2: 78
Student3: 98
Displaying GPA of students.
Student1 :89
Student2 :78
Student3 :98


Basic Programs


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes