C++ Arrays

What are arrays?


For understanding the arrays properly, let us consider the following program. main( )

{int x;
x = 5;
x = 10; cout<<x;

}

• This program will print the value of x as 10. Why so? Because when a value 10 is assigned to x the earlier value of x, ie., 5, is lost. This ordinary variables are capable of holding only one value at a time..

• However, there are situations in which we want to store more than one value at a time in a single variable.

• For example, suppose we wish to arrange the percentage of marks obtained by 100 students in ascending order.

• In such a case we have two options to store these marks in memory:

(i) Construct 100 variables to store percentage of marks obtained by 100 different student ie., each variable containing one student’s marks.

(ii) Construct one variable capable of storing or holding all the hundred values.

• The second one is better. The reason is it would be much easier to handle one variable than handling 100 different variables.

• An array is a group of logically related data items of the same data type addressed

by a common name, and all the items are stored in contiguous memory locations.

Array Declaration

Like other normal variables, the array variable must be defined before it use. Syntax:

Datatype Arrayname[array-size];

Arraysize – indicates the maximum number of elements the array can hold.

Example:

int marks[100]; // Integer array of size 100 float salary[25]; //Floating print array of size 25 char name[50]; //character array of size 50

Accessing Array Elements

• Once an array variable is defined, its element can be accessed by using an index or

position.

Syntax: 

Arrayname[index];

• To access a particular element in the array, specify the array name followed by an integer constant or variable (array index) enclosed within square braces.

• Array index indicates the element of the array, which has to be accessed.

Example:

name[4]; //Accesses the 5th element of the array name.
• Note that, in an array of N elements, the first element is indexed by zero & the last element of an array is indexed by N-1

• The loop used to read the elements of the array is

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

{

cin>>name[i];

}

• The variable i varies from 0 to N-1.

• Note that, the expression age[i] can also be represented as i[age], similarly, the

expression age[3] is equivalent to 3[age].

Array Initialization at Definition (at compile time)

Arrays can be initialized at the point of their definition as follows: datatype array-name[size] = {list of values separated by comma};

For instance, the statement, int age[5] = {19,21,16,1,50};

• Defines an array of integers of size 5.

• In this case, the 1st element of the array age is initialized with 19, 2nd with 21, and so on.

• The array size is omitted when the array is initialized during at compile time.

int age[ ] = {19,21,16,1,50};

• In such a cases, the compiler assumes the array size to be equal to the number of elements enclosed within the curly braces.

• Hence, the above statement, size of the array is considered as five.



int age[ ] = {19,21,16,1,50};
(Or) int age[5]={19,21,16,1,5};

Example: Sum of Array Elements

#include<iostream.h> void main( )

{

int a[10];

cout<<”Enter the no. of elements, max<10>”;



cin>>n;

cout<<”Enter elements”; for(int I = 0; I<n; I++)

{

cin>>a[I];

}

int sum = 0;

for(int I = 0; I<n; I++)

{

sum = sum + a[I];

}

cout<<”Sum of entered elements”<<sum;

}
Two Dimensional Array

Matrix is a two dimensional array & two subscripts are required to access each element.

Declaration:
Data type arrayname [size1][size2];
Example:

int x[3][3];

Representation of 2-D array in memory:

Matrix is allocated into the memory according to row wise (row by row allocation).

Example:

A = 1 5 3 4
Accessing 2-D Array elements:

The elements of a 2-D array can be accessed by the following statement A[i][j] i – row number

j – column number

Initialization of 2-D Array

A 2-dimenstional array can be initialized during its definition.

datatype matrixname [row size][col size] = {elements of first row, elements of 2nd row… elements of n-1 row};

Example:

int a[3][3] = {1,2,3,4,5,6,7,8,9} or

for more readability each row elements can be grouped: int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; or

int a[ ][3] = {{1,2,3},{4,5,6},{7,8,9}}; Row size can be omitted.

أحدث أقدم