C++ Arrays

Array are useful storage they are provide a capability to combine similar type of data. Most of cases they are reliable to implements similar type of variables. array is an data structure of programming language. In c++ program create an array of using this syntax.

data_type tag_name[size_of_array];
Key Point Description
data_type Predefined and user defined data type are allowed
tag_name Name of Array
size_of_array Any positive integer value

for example

int conis[5];//array of 5 integers variable
char name[15];//array of 15 char variable.
float salary[10];//array of 10 float variable

Advantage of array is only one name are capable to store multiple similar type of variable. only difference are arise on its index. subscript operator ([]) are used to defined the size of array. There is optional to provide the size of array when provide initialization and declaration into single statements. see the few examples to initialize value of array.

//Assign the value of array element 
#include<iostream>
using namespace std;

int main(){
  
  //arr1 is an array of 10 integer elements
  //initilize all zero
  int arr1[10]{}; //no value assign

  //arr2 is an array of 5 character elements
  char arr2[]={'a','b','c','d','e'}; //no size provide
  
  //arr3 is an array of 5 character elements
  char arr3[]="Hello";

  //arr4 is an array of 3 float elements
  float arr4[3]={10.9f,20.9f,30.8f};

  //arr1 is an array of 5 integer elements
  //initialize first element is 1 and remaining are zero
  int arr5[5]={1}; //same as int arr5[5]{1};
 
  //initial value is garbage
  char arr6[4];//uninitialized array element

  int index=0,size=0;
  //get the size of arr1 array
  size=sizeof(arr1)/sizeof(arr1[0]);
  cout<<"Size of arr1 is :"<<size<<endl;
  //print all elements in arr1
  for(index=0;index<size;index++){
    //get the element of array index 
    cout<<"arr1["<<index<<"] : "<<arr1[index]<<endl;
  }

    //get the size of arr2 array
  size=sizeof(arr2)/sizeof(arr2[0]);
  cout<<"Size of arr2 is :"<<size<<endl;
  //print all elements in arr1
  for(index=0;index<size;index++){
    //get the element of array index 
    cout<<"arr2["<<index<<"] : "<<arr2[index]<<endl;
  }
  //display arr3 elements
  cout<<"arr3 :"<<arr3<<endl;
  
  //get the size of arr4 array
  size=sizeof(arr4)/sizeof(arr4[0]);
  cout<<"Size of arr4 is :"<<size<<endl;
  //print all elements in arr1
  for(index=0;index<size;index++){
    //get the element of array index 
    cout<<"arr4["<<index<<"] : "<<arr4[index]<<endl;
  }

  //get the size of arr5 array
  size=sizeof(arr5)/sizeof(arr5[0]);
  cout<<"Size of arr5 is :"<<size<<endl;
  //print all elements in arr1
  for(index=0;index<size;index++){
    //get the element of array index 
    cout<<"arr5["<<index<<"] : "<<arr5[index]<<endl;
  }
  return 0;
}
Output
Size of arr1 is :10
arr1[0] : 0
arr1[1] : 0
arr1[2] : 0
arr1[3] : 0
arr1[4] : 0
arr1[5] : 0
arr1[6] : 0
arr1[7] : 0
arr1[8] : 0
arr1[9] : 0
Size of arr2 is :5
arr2[0] : a
arr2[1] : b
arr2[2] : c
arr2[3] : d
arr2[4] : e
arr3 :Hello
Size of arr4 is :3
arr4[0] : 10.9
arr4[1] : 20.9
arr4[2] : 30.8
Size of arr5 is :5
arr5[0] : 1
arr5[1] : 0
arr5[2] : 0
arr5[3] : 0
arr5[4] : 0

In above program given five different way to initialize array element in c++ programming. That is big program but try to understand its logic.

In this program arr1 is array of 10 integers. given size of array but array body portion are empty. that means compiler are providing those array value in compilation of program. this is an integer array so provides zero of all array elements. if this are char array then provides empty character.

arr2 is an array of 5 character elements. note that here not given array size. but initialize the value of array. so compiler are provide the size of this array according to array elements.

arr3 is an character array so we can provide constant string directly. this is not allowed to other data type. and array index are start with zero that means if given the size of an array then provided string(character) length is less of size of array.

int main(){
  char data[5]="Hello";
  /*----
    size =5
    data[0]='H'
    data[1]='e'
    data[2]='l'
    data[3]='l'
    data[4]='0'
  */
  return 0;
}

In this example data[5]='\0' that are represented end of array. so try to provide sufficient size of array.

arr4 is very simple there are 3 float element and provided the size of array. arr5 are something different. here given the size of array are 5 elements but only passing one element value. in this situation compiler are allocated this value to first element . and remaining element are initialized by zero because that is integer array.

One another way to assign array element value. this is normally used to programming. in previous example not mention. using of array index we can provide specific element value in any array. see example.


#include<iostream>
using namespace std;
int main(){
  char info[5];
  //assign at index value
  info[5]='\0'; //terminating character
  info[4]='S'; 
  info[3]='E';
  info[2]='D';
  info[1]='O';
  info[0]='C';
  cout<<info;//display value
  return 0;
}
Output
CODES

Note that help of array index we get exact location of array so we can access any element value by array index. are is contiguous allocation of memory so we can access and modify array element value by its index.

Why use array?

Assume that we are create a large program and we are need to store 10 integers value. then there are two possibility to create 10 integers. first method is to creating number of 10 different name of integers. and another method is create of array of 10 integers. first method are not reliable when create more then 100 of variables.because this process are waste more time to create variables. and providing 100 unique name and remember of this name are not easy. so array are best suitable of this situations.

Multidimensional Array

Array of array is called multidimensional array. That are used to create 2d, 3d and multidimensional array are supported in c++ programming. note that we any create any number of multi dimensional array of using on requirement. View the syntax.

data_type array_name[size][size][size]..[size-n];

For example

int size=3;
int student[size][size];//2d array
int employee[size][size][size];//3d array
int big_data[size][size][size][size]; //4d array

In this example declare 3 different dimensional array. first array are stores [3]*[3]= 6 elements. similarly other are capable to store [3]*[3]*[3]=27 and so on. let create an 2d array.


#include<iostream>
using namespace std;
int main(){
  //2D Array
  //3X4 =12 elements
  //3 rows and 4 columns
  int records[3][4]={
      {1,2,3,4},
      {5,6,7,8},
      {9,10,11,12}
  };

  int rows=0,cols=0;
  //iterate the loop of numbers of rows in array
  for(rows=0;rows<3;rows++){
    //iterate the loop in number of columns of array
    for(cols=0;cols<4;cols++){
      cout<<"records["<<rows<<"]["<<cols<<"] :";
      //display the elements
      cout<<records[rows][cols]<<endl;
    }
  }
  return 0;
}

Note that initialization the value of a array using nested braces. that is standard way to assign multidimensional array. we can also used of 1D array initialization. see the example

#include<iostream>
using namespace std;
int main(){
  //2D Array
  //3X4 =12 elements
  //3 rows and 4 columns
  int records[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};

  int rows=0,cols=0;
  //iterate the loop of numbers of rows in array
  for(rows=0;rows<3;rows++){
    //iterate the loop in number of columns of array
    for(cols=0;cols<4;cols++){
      cout<<"records["<<rows<<"]["<<cols<<"] :";
      //display the elements
      cout<<records[rows][cols]<<endl;
    }
  }
  return 0;
}
Output
records[0][0] :1
records[0][1] :2
records[0][2] :3
records[0][3] :4
records[1][0] :5
records[1][1] :6
records[1][2] :7
records[1][3] :8
records[2][0] :9
records[2][1] :10
records[2][2] :11
records[2][3] :12

both are program produce same output. but this program are not provide clear indication of how many rows and how many number of column in array. when removed of inner braces. so try first approach.

© 2019, regularcodes.com, All rights reserved