Friday, February 1, 2013

Array in c++



What is an array?

C++ provides a data structure, the array, which stores a fixed-size 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.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

How to declare an array?

The general form of declaring a simple (one dimensional) array is
array_type variable_name[array_size];

in your C++ program you can declare an array like
int Age[10];


How to initialize an array?

Initialization of array is very simple in c++ programming. There are two ways you can initialize arrays.
  • Declare and initialize array in one statement.
  • Declare and initialize array separately.
Look at the following C++ code which demonstrates the declaration and initialization of an array.
  int Age [5] = {30, 22, 33, 44, 25};
   int Age [5];
   Age [0]=30;
   Age [1]=22;
   Age [2]=33;
   Age [3]=44;
   Age [4]=25;
Array can also be initialized in a ways that array size is omitted, in such case compiler automatically allocates memory to array.
  int Age [] = {30, 22, 33, 44, 25};
 
Array example
#include <iostream.h>
int main()
{
    //--- Declare array and number of elements in it.
    float a[100];
    int n;   // Number of values currenlty in array.
   
    //--- Read numbers into an array
    n = 0;
    while (cin >> a[n]) {
        n++;
    }
   
    //--- Print array in reverse order
    for (int i=n-1; i>=0; i--) {
        cout << a[i] << endl;
    }
   
    return 0;
}
 

How to declare and initialize multidimensional arrays?

Often there is need to manipulate tabular data or matrices. For example if employee salary is increased by 20% and you are required to store both the salaries in your program. Then you will need to store this information into a two dimensional arrays. C++ gives you the ability to have arrays of any dimension.
 
 

Multi dimension arrays

Consider the example above, you have to store, previous salary, present salary and amount of increment. In that case you will need to store this information in three dimensional arrays.
First I will show you how to declare a two dimensional array and initialize it. Then write a complete program to use multidimensional arrays.
  int Salary[10][2];
This defines an array containing 10 elements of type int. Each of these elements itself is an array of two integers. So to keep track of each element of this array is we have to use two indices. One is to keep track of row and other is to keep track of column.

Elements of multidimensional arrays

Here is a graphical view of multidimensional array that we use to store salary and increment on salary. First column stores the salary element of the array and second column stores increment on salary. We could add another column to store the new salary which adds the increment to the salary.



Initializing multidimensional arrays

Multidimensional arrays can also be initialized in two ways just like one dimensional array. Two braces are used to surround the row element of arrays.
If you are initializing more than one dimension then you will have to use as many braces as the dimensions of the array are.
int Salary [5][2] = 
{
        {2300, 460},
        {3400, 680},
        {3200, 640},
        {1200, 240},
        {3450, 690}
}; 
 
 
 
int Salary [5][2] ={0};  //This will  initialize all the array elements to 0 
 
int Salary [5][2];
Salary [0][0]=2300;
Salary [1][0]=3400;

Multi dimension Array example
 
#include <iostream.h>
int main()
{ 
  int t,i, nums[3][4];
 for(t=0; t < 3; ++t) {
    for(i=0; i < 4; ++i) { 
      nums[t][i] = (t*4)+i+1; 
      cout << nums[t][i] << ' '; 
    } 
    cout << '\n'; 
  }
 return 0;
}