10
Array Topics Array Dimension of Array Array initialization Multidimensional array

Chap 7(array)

Embed Size (px)

Citation preview

Page 1: Chap 7(array)

ArrayTopics Array Dimension of Array Array initialization Multidimensional array

Page 2: Chap 7(array)

Array Array: An array is a group of related data

items that share a common name. A particular value is indicated by writing a number called index number or subscript in brackets after array name. For example: info[100] represent 100th information.

While the complete set of values is referred to as an array, the individual values are called elements. Arrays can be of any data type.

Page 3: Chap 7(array)

One Dimensional Array A list of items can be given one variable

name using only one subscript and such a variable is called a one dimensional array.

In C, one dimensional can be represented as: x[1], x[2], …….x[n]

The subscript can begin with number 0. That is x[0] is allowed.

Declaration of Array:

data_type var_name[size];

Page 4: Chap 7(array)

Continue.. Ex: int x[10]; declares x as a one

dimensional array which can contain maximum 10 integer numbers. The subscript can from 0 to 9.

C language treats character strings simply as arrays of characters.

Ex: char name[10]; declares the name as a character array (string) variable that can hold a maximum of 10 characters.

Page 5: Chap 7(array)

Continue… Initialization of Arrays: Like the ordinary variable

we can initialize the elements of arrays at the time of declaration.

General form:static type var_name[size]={list of values};

The values in the list are separated by commas. Ex: static int x[5]={1,5,6,10,7};

If the number of values in the list is less than the number of elements, then only that many elements will be initialized. The remaining elements will be set to zero automatically.

Ex: static int x[5]={10, 5, 15}; will initialize the first three elements to 10, 5, 15 and the remaining two elements to zero.

Page 6: Chap 7(array)

Continue.. The size may be omitted. Ex: static int x[ ] ={1,2,3,4}; Character arrays may be initialized in a

similar manner. Thus the statement:

static char name[ ]= {‘T’,’O’,’M’};

Page 7: Chap 7(array)

Two dimensional Array C allows us to define tables (which has row

and column) of items by using two dimensional array. General form:type array_name[row_size][col_size];

Ex: int num[10][5]; declares a variable num of 10 rows and 5 columns that is 50 values can be stored in the array. Each value can be accessed by using two subscripts (row number and column number).

So num[4][2] mean the 3rd element in the 5th row of the array num.

Page 8: Chap 7(array)

Initializing two dimensional Array Two-dimensional arrays may be initialized

by following their declaration with a list of initial values enclosed in braces.

Ex: static int table[3][2]={0,0,1,1,2,2}; initialize the elements to the first row to 0, 2nd row to 1 and 3rd row to 2.

The initialization is done row by row. Ex: static int table[3][2]={{0,0},{1,1},{2,2}};

When all the elements are to be initialized to zero, we can write:static int table[3][5]={{0},{0},{0}};

Page 9: Chap 7(array)

Multidimensional Array C allows arrays of three or more

dimensions. The general form is:

type array_name[s1][s2]……[sn]; where si is the size of the ith dimension.

Ex: int x[5][4][6]; float y[5][5][4][3];

Page 10: Chap 7(array)

Program for average of N numbers#define N 10

main()

{

int k, x[10], total=0;

float avg;

for(k=0;k<N;k++)

scanf(“%d”,&x[k]);

for(k=0;k<N;k++)

total = total+x[k];

avg=(float)total/N;

printf(“Average = %f”, avg);

}