Arrays in C++

Arrays in C++

  • C++ offers derived data type namely ‘arrays’, that can hold several values of the same type.
  • Derived Data Types are built from basic data types, i.e. from integer, float, double, etc..

An Array is a collection of variables of the same type that are referenced by a common name. In other words, an array definition specifies a variable type and a name along with one more feature size to specify how many data items the array will contain.

Need for C++ Arrays :

  • Consider a situation in which we have to write a program to store marks of 50 students of a class, calculate their average marks and then calculate the difference of each student’s marks with average marks.
  • So the program needs 101 variables
    • 50 variable for storing students marks and
    • one variable to store average marks and
    • 50 more variables to store different marks.
  • Managing the 101 variables is not an easy task and it will make the program complex.
  • Using arrays we just need 3 variables i.e.
    • two arrays each having 50 elements ( 1 for students marks and another for difference marks) and
    • one variable for storing average marks.

So, arrays are very much useful in the case where quite elements of the same (data) types need to be stored and processed.

Types of arrays :

Arrays are of different types :

  • One dimensional arrays, comprised of finite homogeneous elements
  • Multidimensional array, comprised of elements, each of which is itself an array. A two dimensional array is the simplest of multidimensional arrays.

Syntax of 1-D array :

data_type arrayname[arraysize];
  • data_type declares the base type of the array, which is the type of each element in the array.
  • arrayname is the name of the array,
  • arraysize defines how many elements the array will hold.
  • Elements of the array will be refer to as arrayname[n] where n is the element number in the array.
    • The element numbers in [ ] are called subscripts or indices.
    • The subscript, or index of an element designates its position in the array’s ordering.

Syntax of Multidimensional Array :

type arrayname [rows] [columns];
  • data_type declares the base type of the array, which is the type of each element in the array.
  • arrayname is the name of the array,
  • rows, the first index refers to the number of rows in the array and
  • columns, the second index, refers to the number of columns in the array.

Array Initialization :

  • C++ provides the facility of Array initialization at the time of declaration.
  • The arrays are initialised in the same way as other variables are.
  • The general form of array initialisation is as shown below :
  • data_type array_name [rows][columns] = {value_list};
    • where data_type i the data type of the array
    • arrayname is the name of the array,
    • rows, the first index refers to the number of rows in the array and
    • columns, the second index, refers to the number of columns in the array.
    • The value_list is a comma separated list of arrays ‘elements’ values. The element values in the value_list must have the same data type as that of type, the base type of the array.
  • The first value is placed in the first position of the array, the second value in the second position, and so on.
  • A semicolon(;) must follows the closing brace }.

Example of Array Initialization :

One Dimensional Array :
  • Following code initializes an integer array with 12 elements :
  • int days-of-month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    • This would place first value 31 is month[0], 28 in month[2], and so on.
  • Character arrays can also initializd like this as shown below :
  • char string[10] = "Program";
    
                           //or
    
    char string[10] = {'P', 'r', 'o', 'g', 'r', 'a', 'm', '\0'};
    • Both the statements are same and It will initialize the string with “program”.
    • Because all strings in C++ terminate with a null, you must make sure that the array you declare is long enough to include the null.
2 dimensional array :

Two dimensional arrays are also initialized in the same way as single dimensional ones. For example, the following code initializes a two dimensional array queue with numbers 1 through 5 and their cubes :

cube[5][2] = { 1,1,
                         2, 8.
                         3, 27,
                         4, 64,
                         5, 125
                       };

The element having values :

  • cube[0][0] = 1,
  • cube[0][1] = 1,
  • cube[1][0] = 2,
  • cube[1][1] = 8,
  • cube[2][0] = 3 and so on.

Array of Strings can be initialised as it is given below :

char str[7][11] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Un-sized Array Initializations :
  • C++ allows you to save the size of the array in an array initialisation statement.
  • The C++, then automatically create an Array big enough to hold all the initializer present. This is called an un-sized array.
  • C++ automatically calculates the dimensions of 1 sized arrays.
  • Following are some examples of an size array initialisation :
  • Char S1[] = "First String";
  • If you skip the size, you must give list of initializers as here, so that C++ can we calculate the size of Array by counting them.
  • int val[] = {2,3,4,5,6,7,8,9};
    float amount[] = {2341.57, 1900.70, 3968.65, 4466.80, 5191.00}
  • Like 1 dimensional arrays, two-dimensional array can also be initialized in unsized manner. However, only the first index can be skipped, the second index must be given.
  • The following code initializes a 2 dimensional int un-sized array :
  • int cube[ ][2] = {1, 1, 2, 8, 3, 27, 4, 64, 5, 125};
  • Besides being less tedious, un-sized array initializations allow you to change any of the values without fear of using incorrect error dimensions.
  • The advantage of this declaration is that you may lengthen or shorten the value list without changing the array dimensions.
     

Leave a Reply