Array is a collection of more than one data items of same type. These data items are stored in contiguous memory locations and not randomly.
Suppose, if we want to store the marks of 100 students, then by declaring 100 variables to store respective marks is not a feasible method at all.
To get rid of such problem, we can declare an array with size 100.
Basic syntax to declare an array:
data_type array_name[array_size];
Example,
int a[5]; (an array 'a' that can store 5 integer values)
char b[6]; (an array 'b' that can store 6 characters)
Initialization of an array:
We can initialize an array in two ways i.e at compile time or at run time.
1) At compile time: when we assign the values before running the code. i.e.,
int a[5]={2,4,5,7,8};
or
int a[5];
a[0]=2;
a[1]=4;
a[2]=5;
a[3]=7;
a[4]=8;
2) At run time: when we use a loop and input the value at run time. i.e.
int a[5];
cout<<"Enter the elements of array:"<<endl;
for(int i=0;i<5;i++){
cin>>a[i];
}
Some basic information related to array:
- In array the elements are stored in contiguous memory location.
- The elements of an array can be accessed simply by index i.e if we have an array: int a[5]={1,2,3,4,5};
So, to access first element, we can simply write a[0] and to access second element we can write a[1], and so on.
- Arrays do not have dynamic size.
- The index of array starts from 0.
- The address of first element of the array is know as base address.
ACCESSING ELEMENTS OF ARRAY USING POINTERS.
In case of arrays to store the address, “the address of operator(&)” is not required. Simply if we want to store the address of an array in a pointer we can write:
int a[5]={1,2,3,4,5};
int* p =a;
since, ‘a’ is an array we we will avoid using address of operator.
Code Example:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[5]={1,2,3,4,5};
int*p= a;
cout<<"First element: "<<*p<<endl;
cout<<"Second element: "<<*(p+1)<<endl;
cout<<"Third element: "<<*(p+2)<<endl;
cout<<"Fourth element: "<<*(p+3)<<endl;
cout<<"Fifth element: "<<*(p+4)<<endl;
return 0;
}
Output:
First element: 1
Second element: 2
Third element: 3
Fourth element: 4
Fifth element: 5
Let us have a look at some of the beginner friendly programs, using the array data structure.
1) A program to store 6 numbers in an array and find it’s sum.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
int numb[6],sum=0;
for(int i=0;i<6;i++){
cin>>numb[i];
}
cout<<"The numbers are:"<<endl;
for(int i=0;i<6;i++){
cout<<numb[i]<<" ";
sum+=numb[i];
}
cout<<endl<<"The sum of the number is: "<<sum<<endl;
return 0;
}
Input:
2 6 4 8 5 9
Output:
The numbers are:
2 6 4 8 5 9
The sum of the number is: 34
2) A program to separate odd and even integers into separate arrays.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[10],e[10],o[10],j=0,k=0,u=0,v=0;
for(int i=0;i<10;i++){
cin>>a[i];
}
cout<<"The array is: "<<endl;
for(int i=0;i<10;i++){
cout<<a[i]<<" ";
}
for(int i=0;i<10;i++){
if(a[i]%2==0){
e[j]=a[i];
j++;
u++;
}
else{
o[k]=a[i];
k++;
v++;
}
}
cout<<endl<<"The array of even integer is: "<<endl;
for(int j=0;j<u;j++){
cout<<e[j]<<" ";
}
cout<<endl<<"The array of odd integer is: "<<endl;
for(int k=0;k<v;k++){
cout<<o[k]<<" ";
}
return 0;
}
Input:
7 4 3 2 9 6 14 1 5 8
Output:
The array is:
7 4 3 2 9 6 14 1 5 8
The array of even integer is:
4 2 6 14 8
The array of odd integer is:
7 3 9 1 5
2 Dimensional Arrays:
Till now we were just discussing about a 1-D array in which the elememts were organised in a row but now we will discuss about a 2-D array in which the elements will be printed with well organised rows as well as column.
In a simple language we can say that a 2-D array is nothing but an array of arrays.
Basic syntax to declare a 2-D array:
data_type array_name[row_size][column_size];
ex:
int a[3][4];
(an array 'a' that can store 3 arrays(containing integers) of size 4)
char b[5][6];
(an array 'b' that can store 5 arrays(containing characters) of sizr 6)
Initialization of a 2-D array:
Just like a 1-D array, 2-D arrays can also be initialized in two ways i.e at compile time or at run time.
1) At compile time: when we assign the values before running the code.
i.e.
int a[2][4] = { {1,2,3,4} , {2,5,8,5} };
2) At run time: when we use loop and input value at run time. i.e.
int a[2][4];
cout<<"Enter the elements of array: "<<endl;
for(int i=0;i<2;i++){
for(int j=0;j<4;j++){
cin>>a[i][j];
}
}
Let us have a look at some of the beginner friendly programs, using the 2 dimensional array data structure.
- A program to print 3×4 matrix and find the sum of each element.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[3][4];
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
cin>>a[i][j];
}
}
cout<<"The 3x4 matrix is: "<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
cout<<a[i][j]<<"\t";
sum+=a[i][j];
}
cout<<endl;
}
cout<<"The sum of all elements is: "<<sum<<endl;
return 0;
}
Input:
2 3 4 5 6 7 8 9 1 0 5 4
Output:
The 3x4 matrix is:
2 3 4 5
6 7 8 9
1 0 5 4
The sum of all elements is: 54
This article was exclusively contributed by @sadhana_sharma | LinkedIn
Read Next: Pointer in C++