• Have Any Question?
  • contact@eitworld.com

cpp arrays





Array in C++

Array in C++ Programming is the group of similar data elements having the contiguous memory location.In array, first element represents the lowest address and last element represents the highest address and element of array is uniquely identified via index number that starts with 0. Array in C++ is of two types called as Single dimensional array (1D array) and multi-dimensional array (2D array.) For example, if you want to store the marks of 10 subjects of a student then we have to declare 10 variables to store the 10 subject’s mark and want to store more subject marks then the complexity will be increased and readability losses. To solve it, you can use array concept.

At eitworld.com you can learn C++ Arrays in simple way with lots of explained examples.

  • Array is the group of similar data elements having the contiguous memory location.
  • An array is the indexed collection of fixed number of homogenous data elements.
  • In array, first element represents the lowest address and last element represents the highest address.
  • Each element of array is uniquely identified via index number.
  • In C programming, array index number starts with 0.

Need of Array

  • Array is very useful when you want to store the same type of data elements.
  • For example, if you want to store the marks of 10 subjects of a student then we have to declare 10 variables to store the 10 subject’s marks. Here if we want to store more subject marks then the complexity will be increased and program readability will be down.
  • To overcome this problem, we use array concept to store the same type of data elements into a single variable.

Advantage of C Array

  • By using loop, you can retrieve the elements of array easily.
  • Provide Random Access: We can access any element of array via index number.
  • The main advantage of array is you can represent huge number of values by using single variable so that readability of the code will be improved.

Disadvantage of C Array

  • The biggest disadvantage of array is fixed in size means once you create an array then there is no chance of increasing or decreasing.
  • Suppose you have declared int a[10], then if you store less than 10 elements then memory will waste and if you want to try to store 11th element then you cannot store means automatic size will not increase.

Types of Array

  • One-Dimensional Array
  • Multi-Dimensional Array

One-Dimensional array:

  • It is the simplest form of the array.
  • An array that has only one dimension then it is called as single dimension array or one dimensional array or 1D array.
Declaration of One-Dimensional Array :
data_type array_name [ size_of_array ];

In the above syntax, data_type defines the type of the elements of the array. It can be any valid data type present in C programming such as int, float and char etc.
array_name defines the name of the array and array_name should be unique.
Size_of_array defines the size of array means number of elements present in array.

Examples of One-dimensional Array

int a[2];

char b[3];

float c[4];

In the above example, we have declared three different types of array.

    • int a[2] : int type array named a , have the size 2.
    • char b[3] : char type array named b, have the size 3.
    • float c[4] : float type array named c, have the size 4.
Initialization of one-dimensional array
    • You can initialize the array like:
int a[4] = {1,2,3,4,5};
    • you can initialize the array without specifying the size as follows:
int a[] = {1,2,3,4,5};
  • As we know the elements of array are use the contiguous memory location. So in the above example elements of integer having the size 5 means a[4] are stored in contiguous memory locations. It is assumed that the starting memory location is 100. we know that each integer element requires 2 bytes.
  • The memory representation of one-dimensional array look likes as follows:
Elements a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 a[4] = 5
Address 200 202 204 206 208
Accessing elements of an one-dimensional array
  • You can access the array elements using array name followed by index number.
  • You can access the array elements as follows:
  • int a[3] = {2,3,4,5};
  • a[0] is used to access the 1st index element of array i.e 2
  • a[1] is used to access the 2nd index element of array i.e 3
  • a[2] is used to access the 3rd index element of array i.e 4
  • a[3] is used to access the 4th index element of array i.e 5

Example

#include<iostream.h>

#include<conio.h>

int main()

{

int a[3]={1,2,3}; /*declaration and initialization of array*/

int i;

for(i=0;i<3;i++)

{ cout<<“na[“<<i<<“]=”<<a[i];

} getch();

return 0;

}

In the above program example, we have declared an array int a[3] = {1,2,3}; and also initialization. We have declared a variable i which is used in loop to access the index number by increasing the i variable by one in the for loop.
When i=0 then it will access the 1st index value from the array. In the for loop, cout function is used to print the array elements. This loop will not terminate till the end of the array means the loop will terminate when i<3 means size of array or end of array length.
The output is look like as follows:

array

Two-dimensional array or Multidimensional Array:

  • An array that have two dimensions, called as two-dimension array.
  • This is the simplest form of the multidimensional array.
  • It is said to be a multidimensional array if and only if when an array contains more than two dimensions.
  • The basic syntax of multidimensional array is as follows:
Data_type Array_name [s1][s2][s3]……[sn];
Declaration of two-dimensional array
Data_type array_name [row][column];

For example an integer type two-dimensional array of size 3,3 can be declare as follows:

int a[3][3];

The element of the array a[3][3] can be shown in matrix form as follows:

Column1 Column2 Column3
Row1 a[0][0] a[0][1] a[0][2]
Row3 a[1][0] a[1][1] a[1][2]
Row3 a[2][0] a[2][1] a[2][2]
Initialization of two-dimensional array

int a[3][3]={

{1,2,3},

{4,5,6},

{7,8,9}

};

you can also initialize this as follows:

int a[3][3]={1,2,3,4,5,6,7,8,9};
Accessing elements of two-dimensional array
  • Accessing elements of 2D array is similar to 1D array means array elements are access by array name followed by its two subscripts.
  • The following example show the way to access the element from 2D array:

int a[3][3]=

{

{0,1,2},

{2,3,4},

{3,4,5}

};

 

a[0][0] is used to access the 1st element of array i.e 0

a[0][1] is used to access the 2nd element of array i.e 1

a[0][2] is used to access the 3rd element of array i.e 2

a[1][0] is used to access the 4th element of array i.e 2

a[1][1] is used to access the 5th element of array i.e 3

a[1][2] is used to access the 6th element of array i.e 4

a[2][0] is used to access the 7th element of array i.e 3

a[2][1] is used to access the 8th element of array i.e 4

a[2][2] is used to access the 9th element of array i.e 5

The following program demonstrate you the concept of 2D array accessing elements

#include<stdio.h>

#include<iostream.h>

int main()

{

int a[3][3]={{0,1,2}, {2,3,4}, {3,4,5}};

int i,j;

cout<<“nElements of array:n”;

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

cout<<a[i][j];

}

cout<<“n”;

}

getch();

return 0;

}

In the above example, we have declared 2D array with initialization as int a[3][3]={{0,1,2}, {2,3,4}, {3,4,5}}; . In this example, we have declared i, j variables for loops.

Cout<<“nElements of array:n”; this statement first transfer the control to next line and after that it will print Elements of array: and then control will take place in next line because we used n.

After that control will enter in the outermost loop means for(i=0;i<3;i++) . In this loop, i will be initialized once after that condition will check if condition is true then control will transfer to innermost loop means for(j=0;j<3;j++). In this loop, j will be initialized once, then condition will check, if the condition will true then control will enter into loop otherwise it will exit from loop, or print the array elements.

Cout<<a[i][j]; When the value of j is 3 then the loop will terminate and control will transfer to the next line due to cout<<“n”;. Then i value will be increased by 1 then condition will be checked, if condition will true then control will be transferred inside loop.

This process will be continued till the condition does not false. The output of the above example is shown below:

array



Real Time Web Analytics