C++:Pointers-new and delete operator examples

Example:1
Write a C++ program to take input for “n” element using an array further display all the elements. Allocate the memory dynamically.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
	int n,i;
	char ch;
	do
	{
		cout<<"Enter total elements ";
		cin>>n;
		//dynamic memory allocation
		int *p=new int[n];
		//input
		for(i=0;i<n;i++)
		{
			cout<<"Enter "<<i<<" element ";
			cin>>p[i];
		}
		//display
		for(i=0;i<n;i++)
		{
			cout<<p[i]<<endl;
		}
		//release memory
		delete []p;
		cout<<"Lke to cont .... (y/n) ";
		cin>>ch;
	}while(ch=='y' || ch=='Y');
	

	return(0);
	
}

Output:

Enter total elements 3
Enter 0 element 10
Enter 1 element 20
Enter 2 element 30
10
20
30
Lke to cont …. (y/n) y
Enter total elements 5
Enter 0 element 14
Enter 1 element 52
Enter 2 element 63
Enter 3 element 87
Enter 4 element 55
14
52
63
87
55
Lke to cont …. (y/n) n

Example:2
Write a C++ program to take input for “n” element using an array further display all the elements. Also, calculate and print the sum of all the elements. Allocate the memory dynamically.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
	int n,i,s;
	char ch;
	do
	{
		cout<<"Enter total elements ";
		cin>>n;
		//dynamic memory allocation
		int *p=new int[n];
		//input
		for(i=0;i<n;i++)
		{
			cout<<"Enter "<<i<<" element ";
			cin>>p[i];
		}
		//display
		s=0;
		for(i=0;i<n;i++)
		{
			cout<<p[i]<<endl;
			s=s+p[i];
		}
		//release memory
		delete []p;
		cout<<"Sum = "<<s<<endl;
		cout<<"Lke to cont .... (y/n) ";
		cin>>ch;
	}while(ch=='y' || ch=='Y');
	

	return(0);
	
}

Output:

Enter total elements 3
Enter 0 element 12
Enter 1 element 32
Enter 2 element 45
12
32
45
Sum = 89
Lke to cont …. (y/n) y
Enter total elements 5
Enter 0 element 10
Enter 1 element 20
Enter 2 element 30
Enter 3 element 40
Enter 4 element 50
10
20
30
40
50
Sum = 150
Lke to cont …. (y/n) n

Example:3
Write a C++ program to take input for “n” element using an array further display all the elements. Also check and print the largest array element. Allocate the memory dynamically.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
	int n,i,m;
	char ch;
	do
	{
		cout<<"Enter total elements ";
		cin>>n;
		//dynamic memory allocation
		int *p=new int[n];
		//input
		for(i=0;i<n;i++)
		{
			cout<<"Enter "<<i<<" element ";
			cin>>p[i];
		}
		//display
		m=p[0];
		//or
		//m=*p;
		for(i=0;i<n;i++)
		{
			cout<<p[i]<<endl;
			if(p[i]>m)
			m=p[i];
		}
		//release memory
		delete []p;
		cout<<"Max element = "<<m<<endl;
		cout<<"Lke to cont .... (y/n) ";
		cin>>ch;
	}while(ch=='y' || ch=='Y');
	

	return(0);
	
}

Output:

Enter total elements 5
Enter 0 element 14
Enter 1 element 52
Enter 2 element 36
Enter 3 element 85
Enter 4 element 32
14
52
36
85
32
Max element = 85
Lke to cont …. (y/n) y
Enter total elements 3
Enter 0 element 25
Enter 1 element 65
Enter 2 element 425
25
65
425
Max element = 425
Lke to cont …. (y/n) n