Category: Education

Presentation Description

No description available.


Presentation Transcript

Slide 1:

Arrays The C language provides a capability that enables the user to define a set of ordered data items known as an array. .

Slide 2:

Suppose we had a set of grades that we wished to read into the computer and suppose we wished to perform some operations on these grades, we will quickly realize that we cannot perform such an operation until each and every grade has been entered since it would be quite a tedious task to declare each and every student grade as a variable especially since there may be a very large number.

Slide 3:

In C we can define variable called grades, which represents not a single value of grade but a entire set of grades. Each element of the set can then be referenced by means of a number called as index number or subscript

Slide 4:

Just as variables arrays must also be declared before they are used. The declaration of an array involves the type of the element that will be contained in the array such as int, float, char as well as maximum number of elements that will be stored inside the array. The C system needs this latter information in order to determine how much memory space to reserve for the particular array.

Slide 5:

The declaration int values[10]; would reserve enough space for an array called values that could hold up to 10 integers. Refer to the below given picture to conceptualize the reserved storage space.

Slide 6:

values[0] 50 values[1] 45 values[2] 75 values[3] 80 values[4] 45 values[5] 75 values[6] 35 values[7] 20 values[8] 86 values[9] 55 The array values stored in the memory. ELEMENT 0 ELEMENT 1 ELEMENT 2

Slide 7:

Declaration of arrays: Like any other variable arrays must be declared before they are used. The general form of declaration is: type variable-name[50]; The type specifies the type of the elements that will be contained in the array, such as int float or char and the size indicates the maximum number of elements that can be stored inside the array for ex:

Slide 8:

EXAMPLE : float height[50]; declares the height to be an array containing 50 real elements. Any subscripts 0 to 49 are valid. In C the array elements index or subscript begins with number zero. So height [0] refers to the first element of the array. (For this reason, it is easier to think of it as referring to element number zero, rather than as referring to the first element).

Slide 9:

Initialization of arrays: We can initialize the elements in the array in the same way as the ordinary variables when they are declared. The general form of initialization off arrays is: type array_name[size]={list of values}; The values in the list care separated by commas, for example the statement int number[3]={0,0,0};

Slide 10:

Multi dimensional Arrays: Often there is a need to store and manipulate two dimensional data structure such as matrices & tables. Here the array has two subscripts. One subscript denotes the row & the other the column. The declaration of two dimension arrays is as follows:

Slide 11:

data_type array_name[row_size][column_size]; int m[10][20] Here m is declared as a matrix having 10 rows( numbered from 0 to 9) and 20 columns(numbered 0 through 19). The first element of the matrix is m[0][0] and the last row last column is m[9][19]

Slide 12:

Elements of multi dimension arrays : A 2 dimensional array marks [4][3] is shown below figure. The first element is given by marks [0][0] contains 35.5 & second element is marks [0][1] and contains 40.5 and so on.

Slide 13:

marks [0][0] 35.5 Marks [0][1] 40.5 Marks [0][2] 45.5 marks [1][0] 50.5 Marks [1][1] 55.5 Marks [1][2] 60.5 marks [2][0] Marks [2][1] Marks [2][2] marks [3][0] Marks [3][1] Marks [3][2]

Slide 14:

Initialization of multidimensional arrays: Like the one dimension arrays, 2 dimension arrays may be initialized by following their declaration with a list of initial values enclosed in braces Example: int table[2][3]={0,0,01,1,1};

Slide 15:

Initializes the elements of first row to zero and second row to 1. The initialization is done row by row. The above statement can be equivalently written as int table[2][3]={{0,0,0},{1,1,1}}

authorStream Live Help