C++ is one of the most versatile languages in the world. It is used nearly everywhere for everything… systems programming (operating systems, device drivers, database engines, embedded, Internet of Things, etc.)

Enter Header Image Headline Here

Saturday 15 April 2017

C++ Arrays

                                           C++ Arrays


In programming, one of the frequently arising problem is to handle numerous data of same type.

Consider this situation, you are taking a survay of 100 people and you have to store theirage. To Solve this problem in C++ , you can create an integer array having 100 elements.

An array is a collection of data yhat holds fixed number of values of same type. For example:

int age [100];

here, the age array can hold maximum of 100 elements of integer type.

This size and type of array cannot be changed after its declaration.

How to Declare an array in C++ ?

dataType arrayName[arraySize];

For example,

float mark[5];

here, we declared an array, mark, of floating-point type and size 5.
Meaning, it can hold 5 floating-point values.

Elements of an Array and How to access the,?


You can access element of an array by using indices.

Suppose you declared an array mark as above. the first element is mark [0], second element is mark    [1] and so on.


Few Key Notes:

  • Array have 0 as the first index not 1.In this example, mark[0] is the firs element.
  • If the size of an array is n, to access the last element, (n-1) index is used. In this example, mark [4] is the last element.
  • Suppose the starting address of mark [0]  is 2120d. Then the next address, a[1], will be 2124d, address of a[2] will be 2128 and so on. It's because the size of a float is 4 bytes.

How to initialize an array in C++ programming?

Its possible to initialize an array during declaration. For example,

int mark[5]  = {19, 10, 8, 17, 9};


Another method to initialize an array during declaration.

int mark[]  = { 19, 10, 8, 17, 9};
Here,






















0 comments:

Post a Comment

Popular Posts

Recent Posts

Categories

Unordered List

Text Widget