Vous êtes sur la page 1sur 16

Chapter 10

Structured Data Type: ARRAY

Need for Arrays

Where several values are to be entered of same data type. For E:g A program that accepts Marks to 40 student of 5 subjects. We require 200 variables and managing variables will be difficult task. To make program simple we can define five subject arrays with size 40.

ARRAY

An array is a collection of variables of the same type that are referenced by a common name. In C++, all arrays consists of contiguous memory locations. Arrays can have data items like int or float, or even of user-defined types likes structures and objects.

TYPES OF ARRAY

One Dimension arrays: Are comprised of finite homogeneous elements. Two Dimension arrays: Are the simplest of multidimensional arrays. Multi-Dimensional arrays: Are comprised of elements, each of which is itself an array.

SINGLE DIMENSION ARRAY

The array is given a name and its elements are referred to by their subscripts or indices. C++ arrays index numbering starts with 0. The general form of an array declaration is:

type array_name [size]; int marks[50];

MEMORY REPRESENTATION

char grade[5]
grade[0] grade[1] grade[2] grade[3] grade[4] Grade Address

3000

3001

3002

3003

3004

Total size in bytes can be computed as:

Total bytes= sizeof (type) * size_of_array


Total bytes= 1 * 5 = 5 Bytes

MEMORY REPRESENTATION

int sal[5]
sal[0] sal Address sal[1] sal[2] sal[3] sal[4]

3000

3002

3004

3006

3008

Total size in bytes can be computed as:

Total bytes= sizeof (type) * size_of_array


Total bytes= 2 * 5 = 10 Bytes

STRING AS AN ARRAY

Strings is implemented as singledimension character array. A string is defined as a character array that is terminated by a null character \0. For Eg: char name[21]; which can store 20 max characters and last character is null character.

TWO DIMENSION (2-D) ARRAYS


A

two dimension array is an array in which each element is itself an array. For instance, an array arr[m] [n] is an M by N table with M rows and N columns containing M x N elements. type array_name [rows] [cols];

MEMORY REPRESENTATION OF 2 D ARRAY

float arr[6] [5];


0 0 1 . . N-1

1
2 : M-1

arr[2][1]

Total bytes = 4 * 6 * 5 = 120 Bytes

MATRICES AS 2-D ARRAYS

Matrix is a useful concept of mathematics. A matrix is a set of mn numbers arranged in the form of a rectangular array of m rows and n columns. Such a matrix is called m x n (m by n) matrix. Matrix can be processed through two nested loops outer for rows and inner for columns

ARRAY OF STRINGS

An array of strings is a two-dimension character array. The size of first index (rows) determines the number of strings and the size of second index (columns) determines maximum length of each string. char strings[5][10]; which can store five string of max length 9 characters and last null character.

MEMORY REPRESENTATION OF STRING

char name[5][7];
0
0 1 2 3 4 F S T F F

1
I E H O I

2
R C I U F

3
S O R R T

4
T N D T H

5
\0 D \0 H \0

\0

\0

MULTI-DIMENSIONL ARRAYS

C++ allows to have arrays with dimensions more than two. The maximum limit of dimensions is compiler dependent. The general form of multi-dimensional array is: type name [a] [b] [c].[z]; Manipulation of multidimensional arrays takes more time compared to singledimensional and two dimensional arrays.

ARRAY INITIALIZATION

Arrays can be initialized at time of declaration. The general form of array initialization is: type array_name[size1][sizeN]={value_list}; int day_of_mon[12] = {31,28,31,30,31,30,31,31,30,31,30,31} char name[10]={R,A,M,A,\0}; int cube[5][2]={1,1,2,8,3,27,4,64,5,125}; char star[2][11]={Mars, Moon};

UNSIZED ARRAY INITIALIZATIONS

C++ allows to skip the size of the array in an array initialization statement. C++ automatically creates an array big enough to hold all in the initializers present. If you skip the size, you must give the list of initializers. Only the first index can be skipped but the second index must be given. int val [ ]={2,3,4,5,6,7,8}; float amt[ ] ={23.45, 345.56, 5677.55}; int cube[ ] [2]={1,1, 2,8, 3, 27};

Vous aimerez peut-être aussi