Tutorialspoint

Dynamic Array of Integers

cpp

#include <iostream>
using namespace std;
int main()
{
		
	int i,n;
	
	cout<<"Enter total number of elements:"<<"\n";
	cin>>n;
     	int  *a=new int(n);
	
	cout<<"Enter "<<n<<" elements"<<endl;
	for(i=0;i<n;i++)
	{
	
		cin>>a[i];
	}
	
	cout<<"Entered elements are: ";
	for(i=0;i<n;i++)
	{
		cout<<a[i]<<" ";
	}
	cout<<endl;
	
	delete (a);
	return 0;	
}

Passing Arrays to Function

cpp

#include <iostream>
using namespace std;
void show(int elements[4]);

int main()
{
    int elements[4] = {10,60,70,20};
    show(elements);
    return 0;
}

void show(int a[4])
{
    cout << "Array Elements are: "<< endl;

    for (int i = 0; i < 4; ++i)
    {
        cout << a[i] << endl;
    }
}

Initialize a Dynamic Array

cpp

#include <iostream> 
using namespace std; 

int* Array() 
{ 
	int* a = new int[100]; 
	a[0] = 7; 
	a[1] = 6; 
           a[2] = 4; 
	a[3] = 5; 
	return a; 
} 

int main() 
{ 
	int* p = Array(); 
	cout <<"The elements are:"<< p[0] << " " << p[1]<<" " <<p[2] << " " << p[3]; 
	return 0; 
} 

Use std::sort to Sort Array

cpp

#include <iostream> 
#include <algorithm> 
  
using namespace std; 
  
void display(int a[]) 
{ 
    for(int i = 0; i < 5; ++i) 
        cout << a[i] << " "; 
} 
  
int main() 
{ 
    int a[5]= {4, 2, 7, 9, 6}; 
    cout << "\n The array before sorting is : "; 
    display(a); 
  
    sort(a, a+5); 
  
    cout << "\n\n The array after sorting is : "; 
    display(a); 
  
    return 0; 
} 

Pass an Array by Reference

cpp

#include <iostream>
using namespace std;

void show( int *num)
{
    cout<<*num;
}

int main()
{
     int a[] = {3,2,1,6,7,4,5,0,10,8};
     for (int i=0; i<10; i++)
     {
         show (&a[i]);
     }

     return 0;
}

Find Size of Array Without Using sizeof

cpp

#include <iostream> 
using namespace std; 
  
int main() 
{ 
    int  a[] = {6,7,5,3,1,4,2,10,9}; 
    int s = *(&a + 1) - a; 
    cout << "Number of elements in array is "<< s; 
} 

Dynamic 2D Array Inside Class

cpp

#include <iostream>
#include <stdlib.h>
using namespace std;

const int M=7;

class Arr
{
	private: 
	int A[M];
	int size;  
	public:

	Arr(int s, int v)
	{
		if(s>M)
		{
			cout<<endl<<"This is beyond maximum size";
			exit(1);
		}                
		size = s;

		for(int i=0;i<size;i++)
			A[i] = v;
	}

	int& operator[](int i)
	{
		if( (i<0) || (i>=size) )
		{
			cout<<endl<<"Error: Array out of bound";
			exit(1);
		}
		return A[i];
	}
};

int main()
{
	int i=0;

	Arr a(7,0); 

	for(i=0;i<7;i++) 
		a[i] = i*10;

	cout<<"Array elements are:"<<endl;

	for(i=0;i<7;i++) 
	{
		int val = a[i];  
		cout<<val<<endl;
	}
	
	return 0;
}

Convert String to char array Example3

cpp

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str = "Tutorialspoint";

	char c[str.size() + 1];
	str.copy(c, str.size() + 1);
	c[str.size()] = '\0';

	cout << c << '\n';

	return 0;
}

Compile and Execute C++ Online

cpp

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
   string str = "Tutorialspoint";

	char c[str.size() + 1];
	strcpy(c, str.c_str());

	cout << c << '\n';

	return 0;
}

String to char Array Example1

cpp

#include<iostream> 
#include<string.h> 
  
using namespace std; 

int main() 
{ 

char m[]="Tutorialspoint"; 
string str; 
int i; 
for(i=0;i<sizeof(m);i++) 
    { 
        str[i]=m[i]; 
        cout<<str[i]; 
    } 
return 0; 
}

1 2 3 4 5 6 7 ... 2142 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.