Monday, January 21, 2019

Array in c++ programming

in this post, you will learn about the array in c++ programming and also learn how to use an array in programming.


an array is a contiguous group of memory locations that all have the same type. to refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array.
one dimensional array in c++ programming, learn c++ programming
learn c++ programming

In c++ we have two types of array:

one-dimensional array.
two and multidimensional array.

one dimensional array:

the index number of a one-dimensional array starts from the 0 and continue to by increment of 1 in index number.

Declaration of one dimensional array:


            <return_type> <array_name>[ size_of_array]; 


return type indicates that we can only enter the value of the same return type in the array.

we define a particular name to the array.

it's defined the maximum number of elements in the array.

               int arr[100];                               

In the above example:

int = int indicate that we can only enter the integer type of values in an array.

arr = arr is the name of the array.

100 = it's the size of an array. it means we can maximum enter the 100 values in the array.

Another Example:
                            char name[10];             
                            float arr[20];                 

Initialize the value for an array:

in the array we can initialize the values using two methods:

at declaration time:
we can initialize the array's values at the declaration time of array.
to declare the value for the array at declaration time you just need to initialize all value in { } and just separate them using the [,] sign. see the example for full explanation.

              int arr[10]={0,1,2,3,4,5,6,7,8,9};   

in the above example, you can see we have an array name arr size of 10. and we initialize the array value at the declaration time of the array.
likewise, you can also initialize a value for a particular location.

            arr[index_number]= value;              

               arr[2]= 30;                                     

in the above example, we can assign the value 30 to the third element of the array. because the array index starts from 0. 

at run time:
we can use for loop to initialize the value to the array at run time.


                   std::cin>>arr[i]; // always initialize the same type of                       value to the array:                                                           

Print the values of the array:
In c++ we can print the values of an array using the for loop.

                   std::cout<<"\n"<< arr[i];                                                  

Example program to initialize the value to the array and print the value of array:

In this example we will create two arrays in one array we can initialize the value at the declaration time of array, and in the second array, we can initialize the value at the run time. so you can fully understand both methods of initialization of values in an array.

 #include <iostream>

 int main()
    // initialize the value at the declaration time 
   int first[5]={1,2,3,4,5};                                                 
   int second[5];

   std::cout<<"\n enter the values for second array:";

         // set the values for second array using for loop
        std::cout<<"\n enter the value for"<< i<< "element";
   std::cout<<"\n print the values of first and second array:";


   std::cout<<"\n"<< i<<" element of the first array "<<first[i];



    std::cout<<"\n"<< i<<"element of the second array"<<                second[i];        
  return 0;

I think, now you understood, how to declare a one-dimensional array and how to initialize the values to the array and how to print the values of the one-dimensional array.

"Please share this knowledge as much possible as you can and also comment me your queries and questions related to this topic. I will really grateful to help you."