Wednesday 11 January 2017

ARRAY

Share & Comment
One of the frequently arising problem is to handle similar types of data. For example: If the users want to store marks of 100 students. This can be done by creating 100 variables individually but, this process is rather tedious and impracticable. These type of problem can be handled in C++ programming using arrays.

C++ provides a data structure, the array, which stores a fixed-sized sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Declaring Array

To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:

type array Nam e [ array Size ];

This is called a single-dimension array. The array Size must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement:
double balance[10];

Arrays are of two types:

  1. One dimensional array
  2. Two dimensional array


Initializing one dimensional array

Arrays can be initialized at declaration time in this source code as:
int age[5]={2,4,34,3,4};
It is not necessary to define the size of arrays during initialization.
int age[]={2,4,34,3,4};
In this case, the compiler determines the size of array by calculating the number of elements of an array.

Initializing Multidimensional array

C++ supports multidimensional arrays. The simplest form of the multidimensional array is the two dimensional array.

int age[2][2]={1,2,3,4}  /*valid declaration*/
int age[][2]={1,2,3,4}  /*valid declaration*/
int age[2][]={1,2,3,4}  /*Invalid declaration-must specified second dimension*/
int age[][]={1,2,3,4}  /*Invalid declaration*/



Tags:

Written by

Hy, i am a Code Geek ! And like to eduate others.

0 comments:

Post a Comment

 
Copyright © C++ Programming | Designed by Templateism.com