1 of 16

Dr. S. Sridevy

Associate Professor (CS)

Dr. M. Ramanan

Teaching Assistant (CS)

Dept. of PS&IT, AEC&RI, TNAU, Coimbatore -3.

Concept of Arrays: One-Dimensional - Two Dimensional arrays

COM 102 Theory Schedule 8

2 of 16

Concept of Arrays

  • Consider a situation where we need to store five integer numbers. How will you create?

  • Now let's assume we have to store 500 integer numbers. Will you use 5000 variables?

S. Sridevy

6/3/2022

3 of 16

Arrays

  • An array is a collection of elements of the same type that are referenced by a common name.
  • Array is a derived data type.
  • All the elements of an array occupy a set of contiguous memory locations.

S. Sridevy

6/3/2022

4 of 16

Why need to use array type?�

  • We have a list of 1000 students' marks of an integer type. If using the basic data type (int), we will declare something like the following…“

int  studMark0, studMark1, studMark2, ..., studMark999;

  • Can you imagine how long we have to write the declaration part by using normal variable declaration?

S. Sridevy

6/3/2022

5 of 16

Arrays

  • By using an array, we just declare like this,

int  studMark[1000];

  • This will reserve 1000 contiguous memory locations for storing the students’ marks.
  • Graphically, this can be depicted as in the following figure.

S. Sridevy

6/3/2022

6 of 16

Dimensions in Array

  • Dimension refers to the array's size, which is how big the array is.

  • One Dimensional Arrays
  • Two Dimensional Arrays
  • Multi Dimensional arrays

S. Sridevy

6/3/2022

7 of 16

One / Single Dimensional Array

  • An array with a single (one) subscript is known as one dimensional array
  • A list of items can be given one variable name using only one subscript

Eg: int age[5];

and the computer reserves five storage locations as shown below

S. Sridevy

6/3/2022

8 of 16

One / Single Dimensional Array

  • This would cause the array age to store the values as shown below:

S. Sridevy

6/3/2022

9 of 16

One / Single Dimensional Array

  • Declaration of One (Single) Dimensional Array

The general form of array declaration is

data_type array-name[size];

  • The type specifies the type of element 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.

Eg: 1. int age[50];

2. float height[50];

S. Sridevy

6/3/2022

10 of 16

One / Single Dimensional Array

  • The C language treats character strings simply as arrays of characters.

Example: char name[10];

values to array elements can be assigned as follows

strcpy(name,”Raj Kumar”);

S. Sridevy

6/3/2022

When the compiler sees a character string, it terminates it with an additional null character (‘\0’) at the end

11 of 16

One / Single Dimensional Array

  • Initialization of One Dimensional Arrays

The general form of initialization of arrays is:

static data_type array-name[size] = {list of values};

Eg:1 static int number[3] = {0,0,0};

Eg:2 static float total [5] = {0.0,12.50,-15};

Will initialize the first three elements to 0.0, 12.50 and -15.0 and the remaining two elements to zero.

S. Sridevy

6/3/2022

12 of 16

One / Single Dimensional Array

Eg: 3 static int count[ ] = {1,1,1,1};

If size may be omitted, the compiler allocates enough space for all initialized elements.

Eg: 4 static char name [ ] = {‘T’,’N’,’A’,’U’,’\0’};

S. Sridevy

6/3/2022

13 of 16

Two Dimensional Array

  • An array with two subscripts is known as two dimensional array.

Declaration of Two-Dimensional Array

Two-dimensional arrays are declared as follows

data_type array-name[row_size][column_size];

S. Sridevy

6/3/2022

14 of 16

Two Dimensional Array

S. Sridevy

6/3/2022

Example 1 : int a[3][3];

15 of 16

Two Dimensional Array

Initialization of Two – Dimensional Arrays  

The general form of initialization of arrays is:

static data_type array name[row_size][column_size] = {list ofvalues};

Eg: 1) static int number[2][3] = {0,0,0,1,1,1};

Or

static int number[2][3] = { {0,0,0},{1,1,1} };

Or

static int number[2][3] = {

{0,0,0}, {1,1,1}

};

16 of 16

Multi Dimensional Array

  • C allows arrays of three or more dimensions. The exact limit is determined by the compiler.

The general form of a multidimensional array is

data_type array-name[S1][S2][S3] …. [Sn];