![]() |
How To Use Array in C++ With Example in Details |
How To Use Array in C++ With Example in Details
Array Definition: Array is a collection of data of the same data type.
It can be used to store groups of data simultaneously.
the array can store data of the same data type means an integer Array
can store only integer value character array can store only character value and so on.
We can not fetch knowledge from an array directly thus we tend to use index purposes.
The array could be an assortment of comparable styles of parts that have a contiguous memory location.
Important Topic in C++ Language
👉if-else statementsTypes of Array in C++?
There are two types of Array.
1. Single Dimensional Array
2. Multidimensional Array
Code Optimization: Array makes the code optimized, we can retrieve or sort data efficiently.
Random access: we can get any knowledge placed at an index position.
Size Limit: Array can store only the fixed size of elements in the array.
Syntax of Array
int ar[]=new int[5];
int ar[]=new int[5];
Here ar is the name of the array.
int is the data type of array.
The size of the array is five suggests that we can store most five values during this array.
How To Initialization of array in C++?
int ar[]={10,20,30,40,50};
int ar[]=new int[5];
ar[0]=50;
ar[1]=40;
ar[2]=30;
ar[3]=20;
ar[4]=10;
int ar[]={10,20,30,40,50};
int ar[]=new int[5];
ar[0]=50;
ar[1]=40;
ar[2]=30;
ar[3]=20;
ar[4]=10;
How To Use Array in C++ Program?
#include<iostream>
using namespace std;
int main()
{
int ar[5]={ 80,77,14,78,01 };
cout<<"value is ar[0]:"<< ar[0]<<"\n";
cout<<"value is ar[1]:"<<ar[1]<<"\n";
cout<<"value is ar[2]:"<<ar[2]<<"\n";
cout<<"value is ar[3]:"<<ar[3]<<"\n";
cout<<"value is ar[4]:"<<ar[4];
}
*****OUTPUT*****
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
#include<iostream>
using namespace std;
*****OUTPUT*****
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
How To Use Loop Array in C++ Program?
#include<iostream>
using namespace std;
int main ()
{
int ar[]={80,77,14,78,01};
for(int i=0;i<5;i++)
{
cout<<"value is ar[%d]:"<<i<<ar[i]<<"\n";
}
}
*****OUTPUT*****
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
#include<iostream>
using namespace std;
int main ()
{
int ar[]={80,77,14,78,01};
for(int i=0;i<5;i++)
{
cout<<"value is ar[%d]:"<<i<<ar[i]<<"\n";
}
}
*****OUTPUT*****
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
How To Use User input Method Array in CPP Program?
#include<iostream>
using namespace std;
int main()
{
int ar[5];
cout<<"Enter element 1:";
cin>>ar[0];
cout<<"Enter element 2:";
cin>>ar[1];
cout<<"Enter element 3:";
cin>>ar[2];
cout<<"Enter element 4:";
cin>>ar[3];
cout<<"Enter element 5:";
cin>>ar[4];
for(int i=0;i<=4;i++)
{
cout<<"value is a[%d]="<<i<<ar[i]<<"\n";
}
}
*****OUTPUT*****
Enter element 1:80
Enter element 2:77
Enter element 3:14
Enter element 4:78
Enter element 5:01
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
#include<iostream>
using namespace std;
cin>>ar[2];
cin>>ar[4];
*****OUTPUT*****
Enter element 1:80
Enter element 2:77
Enter element 3:14
Enter element 4:78
Enter element 5:01
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
Two Dimensional Array
Array: Array is a collection of data of the same data type. It can be used to store groups of data simultaneously.
The array can store data of the same data type means an integer array can store only integer value character array can store only character value and so on.
We can not fetch data from the array directly therefore we use index point.
The array is a collection of similar types of elements that have a contiguous memory location.
An array is an object which contains elements of a similar data type. It is a data structure where we store similar elements.
We can store only a fixed set of elements in an array.
The array is index-based, the first element of the array is stored at the 0 indexes.
Syntax of Two Dimensional C++ Array
int ar[][]=new int[3][3];
int ar[][]=new int[3][3];
Here ar is the name of the array.
int is the data type of array.
The size of the array is 3x3 means we can store a maximum of 9 values in this array.
How To Initialization 2-D array in C?
int ar[][]=new int[3][3];
ar[0][0]=80;
ar[0][1]=77;
ar[0][2]=14;
ar[1][0]=78;
ar[1][1]=01;
ar[1][2]=96;
ar[2][0]=90;
ar[2][1]=40;
ar[2][2]=23;
int ar[][]=new int[3][3];
ar[0][0]=80;
ar[0][1]=77;
ar[0][2]=14;
ar[1][0]=78;
ar[1][1]=01;
ar[1][2]=96;
ar[2][0]=90;
ar[2][1]=40;
ar[2][2]=23;
How To Use 2-D Array in C++ Program?
#include<iostream>
using namespace std;
int main()
{
int ar[3][3]={{80,77,14},{78,01,96},{90,40,23}};
cout<<"value is a[0][0]:"<<ar[0][0]<<"\n";
cout<<"value is a[0][1]:"<<ar[0][1]<<"\n";
cout<<"value is a[0][2]:"<<ar[0][2]<<"\n";
cout<<"value is a[1][0]:"<<ar[1][0]<<"\n";
cout<<"value is a[1][1]:"<<ar[1][1]<<"\n";
cout<<"value is a[1][2]:"<<ar[1][2]<<"\n";
cout<<"value is a[2][0]:"<<ar[2][0]<<"\n";
cout<<"value is a[2][1]:"<<ar[2][1]<<"\n";
cout<<"value is a[2][2]:"<<ar[2][2]<<"\n";
}
*****OUTPUT*****
value at a[0][0]:80
value at a[0][1]:77
value at a[0][2]:14
value at a[1][0]:78
value at a[1][1]:01
value at a[1][2]:96
value at a[2][0]:90
value at a[2][1]:40
value at a[2][2]:23
#include<iostream>
using namespace std;
int main()
{
int ar[3][3]={{80,77,14},{78,01,96},{90,40,23}};
cout<<"value is a[0][0]:"<<ar[0][0]<<"\n";
cout<<"value is a[0][1]:"<<ar[0][1]<<"\n";
cout<<"value is a[0][2]:"<<ar[0][2]<<"\n";
cout<<"value is a[1][0]:"<<ar[1][0]<<"\n";
cout<<"value is a[1][1]:"<<ar[1][1]<<"\n";
cout<<"value is a[1][2]:"<<ar[1][2]<<"\n";
cout<<"value is a[2][0]:"<<ar[2][0]<<"\n";
cout<<"value is a[2][1]:"<<ar[2][1]<<"\n";
cout<<"value is a[2][2]:"<<ar[2][2]<<"\n";
}
*****OUTPUT*****
value at a[0][0]:80
value at a[0][1]:77
value at a[0][2]:14
value at a[1][0]:78
value at a[1][1]:01
value at a[1][2]:96
value at a[2][0]:90
value at a[2][1]:40
value at a[2][2]:23
How To Use Loop. 2-D Array in CPP Program?
#include<iostream>
using namespace std;
int main()
{
int ar[3][3]={{80,77,14},{78,01,96},{90,40,23}};
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
cout<<ar[i][j]<<" ";
}
cout<<"\n";
}
}
*****OUTPUT*****
80 77 14
78 01 96
90 40 23
#include<iostream>
using namespace std;
int main()
{
int ar[3][3]={{80,77,14},{78,01,96},{90,40,23}};
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
cout<<ar[i][j]<<" ";
}
cout<<"\n";
}
}
*****OUTPUT*****
80 77 14
78 01 96
90 40 23
How To Use User input Method 2-D Array in C++ Program?
#include<iostream>
using namespace std;
int main()
{
int a[3][3];
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<"Enter Element:a[%d][%d]:"<<i<<j<<"=";
cin>>a[i][j];
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}
*****OUTPUT*****
Enter Element:a[0][0]=80
Enter Element:a[0][1]=77
Enter Element:a[0][2]=14
Enter Element:a[1][0]=78
Enter Element:a[1][1]=01
Enter Element:a[1][2]=96
Enter Element:a[2][0]=90
Enter Element:a[2][1]=40
Enter Element:a[2][2]=23
80 77 14
78 01 96
90 40 23
#include<iostream>
using namespace std;
int main()
{
int a[3][3];
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<"Enter Element:a[%d][%d]:"<<i<<j<<"=";
cin>>a[i][j];
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}
*****OUTPUT*****
Enter Element:a[0][0]=80
Enter Element:a[0][1]=77
Enter Element:a[0][2]=14
Enter Element:a[1][0]=78
Enter Element:a[1][1]=01
Enter Element:a[1][2]=96
Enter Element:a[2][0]=90
Enter Element:a[2][1]=40
Enter Element:a[2][2]=23
80 77 14
78 01 96
90 40 23