Arrays In C++





Arrays in C++
Array is a collection of similar type of data’s that are referenced by a common name. It helps to develop efficient program. It provides the ability to use a single name to represent a collection of items.

Single dimensional array

A single dimensional array has only a single subscript. The general form of single dimensional array declaration is:-

 Data_type array_name [size];

Where size defines how many elements the array will hold.
E.g.

Int a [50]

Float b [100]

Initialization of array

The general format of the array initialization is:-

Data_type array_name [size] = {element1, element2……..element n};

E.g.

Int marks [5] = {92, 82, 70, 60, 90};

The result of each of the above array is:

Marks [0] =92
Marks [1] =82
Marks [2] =70
Marks [3] =60
Marks [4] = 90

A program to initialize a set of numbers in the array and to display it in standard output device.

#include<iostream.h>
#include<conio.h>
Void main ()
{
Int a [7] = {11, 12, 13, 14, 15, 16, 17};
Int I;
Cout<<”\n contents of the array”;
For (i=0; i<7; i++)
{
Cout<<”\n “<<a[i];
}
getch ();
}

Multidimensional array

The general form of a multidimensional array is:

Data_type array_name [s1][s2]……….. [Sn];

Where Sn is the size of the [i]th dimensions.

E.g.

Int a [3] [3];
Int b [5] [7];

A  program to initialize a set of numbers in a two dimensional array and to display the content of the array.

#include<iostream.h>
#include<conio.h>
Void main ()
{
Int I, n;
Int a[3][4]={{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
Cout<<”\n Contents of array: \n”;
For (i=0; i<3; i++)
{
For (j=0; j<4; j++)
{
Cout<<”\n \t”<<a[i] [j];
Cout<<endl;
}
}
getch ();
}



Related posts:

  1. Array Of Class Object Array of class objects Array is a collection of similar...
  2. C++ Functions C++ Functions A function is meant for performing a specified...
  3. Manipulators In C++ Manipulators Manipulators are operators used with the insertion operator <<...
  4. Switch Statement In C++ Switch statement in c++ C++ provides a multi way decision...
  5. Constructor in C++ Constructor in C++ Constructor is a member function with the...