Wednesday, April 22, 2020

Array in C++ programming



Array in C++ programming:


array in c++ language
Array in c++


An array may be a fixed number of elements of the identical type stored sequentially in memory. Therefore, an integer array holds some number of integers, a personality array holds some number of characters, and so on. the scale of the array is noted as its dimension.


To declare an array in C++, we write the following:

                           

Syntax of declaration:


Type array Name [dimension];  

 For example;

char arr[5]={1,2,3,4,5}


Access of one dimensional Array:


The elements in an array can be accessed by using the index number at which that number is present.

For example:

{23,54,67,32,45}

Here if we'd like to access “67” we are able to get access by its  indicator which “2”  here because array’s index always starts from “0”.


Initialization of 1-Dimensional Array:



 The elements of an array also like every other variables must be initialized before they'll be used otherwise it'll show unexpected ends up in our program. There are two ways to initialize the array.


int arr[5];


Declaration of 1D Array:


An array can be declared within the following two ways:

One way is to declare the array and then initialize some or all of the elements:

 arr[0] = 87;

 arr[1] = 98;

 arr[2] = 34;

 arr[3] = 55;

 arr[4] = 66;


And the other way is to initialize some or all of the values at the time of declaration:

 int arr[4] = { 6, 0, 9, 6 };


EXAMPLE:


Let’s study an easy program to initialize and declare the 

worth of array:


Source code:


 #include<iostream>

using namespace std;

 int main()

{

 int arr[7];

 cout << “ enter the number:“ << endl;

 for(int i = 0; i < 7; i++)

 {

 cin >> arr[i];

}

 cout << “values assigned to array are:“;

  for(int i = 0; i < 7; i++)    
    
{

  cout << arr[i]<<endl;

}

  return 0;

 }


Output:

enter the number.

45
32
67
54
78
65
43

Values assigned to array are:

arr[0] = 45;
 arr[1] = 32;
 arr[2] = 67;
 arr[3] = 54;
 arr[4] = 78;
arr[5] = 65;
 arr[6] = 43;

OR

Int arr[7]={45,32,67,54,78,65,43}


Explanation:


In this above program first of all we initialized the data type and length of array and then we initialize the values that are stored in array by the user with the assistance of For loop and then we've used another For Loop to display the values stored in array.


Multidimensional Array/2D Array:


C++ also supports the creation of multidimensional array, through the addition of over one set of brackets. Thus, a two-dimensional array also created by the following:

Syntax:


 Type array Name[row][column];

Total no. of elements in 2-D array is calculated as row*column


For example:

int a[2][3];

Total no of elements=row*column is 2*3 =6

 It means the matrix consist of 2 rows and 3 column



Positions of 2-D array elements in an array are as below :

00     01     02
10      11     12


For example:

  6       2        8
  48     66     15


Initialization of 2D Array:


2D array are also initialized exceedingly in a similar way as the one-dimensional array are initialized:


Type array Name[row][column];          for example:

Char arr[2][3]


Note that dimensions should be provided when initializing multidimensional arrays, because it is otherwise impossible for the compiler to see what the intended element partitioning is.


Declaration of 2D Array:


2D Array can be declared by following ways:

int Array[2][4] = { 9, 2, 4, 22, 42,50, 11, 61 };

 int Array[2][4] = { { 9, 2, 4, 22 } , { 42, 50, 11, 61 };


EXAMPLE:

Let’s study an easy example in which we've initialized and declared a two


Dimensional/2D array:


Source code:


#include<iostream>

 using namespace std;

int main()

 {

int Array[1][2]=17;

 Array[0][0] =7 ;

 Array[0][1] = 8;

 Array[0][2] = 45;

 Array[1][0] = 12;

 Array[1][1] = 33;

 Array[1][2] = 11;

 for(int k = 0; i < 1; i++)

{

 for(int j = 0; j < 2; j++)

{

 cout << Array[k][j]<<endl;

}

}

return 0;

}


Output: 

7     8    45
12    33  11

Explanation:


In this program we've first initialized the length of the array and so then we've declared the values of Array by giving it the hardcoded values and then we've used 2 for loop to display the values stored in two dimensional array.




THANK YOU FOR READING THE ARTICLE I HOPE THAT IT HELPED YOU.

No comments:

Post a Comment

If else in c

IF Else in C: If-else in c: If else is the most used condition in our life. Whenever we need to make any decision we use...