C++ Arrays - One & Two Dimensional Arrays

- January 31, 2018
For a few variables in a computer program, we define them individually according to the needed data types. However, if we need to use hundred or thousand 1 variables of same data type in a program, it will become difficult to handle them. For example, to store marks of thousand students, we need thousand variables of integer type It is a difficult and time-consuming process to define and use these variables individually. The program size becomes too large to handle. The logic of the program also becomes complex Such problems can be solved by using arrays. Almost every computer language provides the feature of arrays.

What is Array?

      An array is a consecutive group of memory locations with the same name and data type. The memory locations in an array are called elements of the array. The total number of elements in the array is called the size of array (or its length).

      Each element in the array is accessed by specifying the name of the array and its position number m the array. This position number is called index or subscript. The index is written within square brackets ([]) with the name of array. The index must be an unsigned integer value.
      In C++, first element of the array has an index value of 0. In an array with ‘n’ elements, the index 0, 1......, n -1. The ‘n-1’ represents the index value of last element. The index of first element of array is called its Lower Bound (LB) and the index of the last element of array is called its Upper Bound (UB).
      Arrays are used to process a large amount of data or same data type. Data of the defined type is stored in the array. Like simple variable, the array is accessed by a single variable name. However, the index values are used to access individual elements of the array. Typically, loop statement is used to access data of elements of an array by changing index value in the body of loop. Only a few statements are required to process data in an array. Therefore, to process data using arrays in a program reduces the size of
the program.
      Arrays are divided into two types:

  • One-dimensional arrays 
  • Multi-dimensional arrays 


What is One-Dimensional Arrays?

      One-dimensional array is also known as linear array. It consists of only one row or one column. For example, temperature of each day of a month can be stored m an array. Suppose we declare the array ‘Temp‘ of 31 elements to store the temperature of each day of a month. Its elements will be Temp[0], Tempm[1], Temp[2], ----.Temp[30].
One dimensional array diagram
     This array “Temp” contains floating-point data (or real data). It has 31 elements. The first element of the array is Temp[0] that is in position 0 and Temp[1] is the second element of array and is in position 1. Similarly, the last element of the array is Temp[(30] and is in position 30.
      We can easily compute the size of one-dimensional array. The formula to compute the size of one-dimensional array is as under:
 Size of one-dimensional array = UB - LB + 1
      Each element of an array has a unique memory address. The starting address of the array is called the base address of the array. Since the elements of an array occupy a continuous block of memory, if base address of the array is known, the address of any element of the array can be computed easily. The formula to compute the address of any element of array is as follows:

A [e - 1] = Ab + size * (e - 1)

Where:

A[e -1]
It represents the name of element whose memory address is to be found.

e
It represents the element number of the array.

Ab
It represents the base address of the array.
size
It represents the size of the array.

Suppose an afray ‘abc’ has' 3 elements each of 2 bytes length. Then to find out the address of second element of the array ‘abc’ when the base address is 300 (value of Ab).
abc [2 - 1] = 300 + 2 * (2 - 1) 

abc [1] = 300 + 2 * 1 

 = 300 + 2

 = 302 
C++ arrays elements
      Here we can see that each memory location has a umque address. However, an element of an array may occupy more than one memory locations also. It depends upon the data type of the array. In case of ‘int' type data, each element takes 2 bytes in memory. The starting address of element is considered as address of the element. In the above array, the base address of array 'abc' is 300, so the addresses of other elements will be as follows:

  • The address of abc[0] is 300
  • The address of abc[1] is 302
  • The address of abc[2] is 304

How to Declare One-Dimensional-array?

      Like other variables in C++, an array must be declared before it can be used in the program. Defining the name of array, its total number of elements and data type is called declaration of array. When an array is declared, a memory block with required number of consecutive memory locations is reserved in the computer memory for storing data values.
      The general syntax to declare one-dimensional array is as follows;
 type arrayname [n];

In the above syntax:
type: It indicates the data type df the array. It can be int, float, double, char or any user-defined data type.
arraynsme: It indicates the name of array. The rules for naming an array are the same as those for an ordinary variable.
n:It indicates the total number of elements of the array. It is unsigned integer value.

Following statement declares one-dimensional array named "num" of type double with 31 elements:

double num [31] ;

      We can declare more than one array variables of the same data type with a single statement. Following statement declares array "x" of 15 elements and array 'y' of 10 elements, both of ‘float’ data type:

float x[15], y[10];

      Similarly, we can also declare simple variables and array variables of same type with a single statement. Following statement declares array variables 'x', 'y', 'z' and  simple variables ‘a’, ‘b’, ‘c’ all of ‘int’ data type;

int x[15], y[10], z[12], a, b, c;

How to access data in One-dimensional Arrays?

      Each element of an array is accessed by specifying the name of the array and its index value. The index value is written within square brackets [] after the array name. We can access data of all the elements of an array very easily with the help of loop structure.

Input/Output Data in One-Dimensional 

Array

      Data is input into individual elements of an array using input statements. In C++, “cin” stream object is used to input data into the elements of the array during program execution.
      An assignment statement can also be used to assign values to the individual elements of an array. Following assignment statements assign values to an integer type array “temp” of 4 elements:
temp[0] = 15 ;

temp[1] = 20 ;

temp[2] = 28 ;

temp[3] = 30 ;

      Similarly, output statement is used to display data of an array. In C++, “cout” stream object is used to display data of individual elements of an array.
      An array may have several elements. Loop structure is commonly used to input and output data in an array. The input / output statement is written in the body of the loop to access data of elements of the array. The loop structure repeats the input/output statements for each individual element of the array by changing the index value of the array.

Example Programs of One-Dimensional Array:


The following program inputs values into the individual elements of an array during program execution and displays the value of the array. (Without any loop statement).

#include<iostream.h>

#include<canio.h>

main()

{

int temp [5];

clrscr();

cout<<"Enter value in 1st element of temp:";

cin>>temp[0];

cout<<"Enter value in 2nd element of temp:";

cin>>temp[1];

cout<<"Enter value in 3rd element of temp:";

cin>>temp[3];

cout<<"Enter value in 5th element of temp:";

cin>>temp[4];

cout<<"values in array temp are:"<<endl;

cout<<temp [0]<<endl;

cout<<temp [1]<<endl;

cout<<temp [2]<<endl;

cout<<temp [3]<<endl;

cout<<temp [4]<<endl;

getch();

}