NIOS Computer Science: Chapter 11 – Array Computer Part 1

Download PDF of This Page (Size: 218K)

Image of Array

Image of Array

Image of Array

Introduction

The ordered collection of identical elements is called an array. Sometimes, it is required to store a large number of variables of the same type, one can use an array.

For example:

int A[5] ;

The above statement declares an array A which reserves 5 memory locations for storing integer data type.

These locations are A[0], A[1], A[2], A[3] and A[4]. The number within the square bracket is called index or subscript.

The array can be one dimensional (one subscript) or two dimensional (two subscripts).

Objectives

After going through this lesson, you would be able to:

  • explain the concept of an array

  • define, access and initialize array elements

  • perform common operations on arrays

  • explain two dimensional array

  • define, access and initialize two dimensional array elements

Initialization of One Dimensional Array

An array can be initialized along with declaration.

For array initialization it is required to place the elements separated by commas enclosed within braces.

int ;

It is possible to leave the array size open. The compiler will count the array size.

int ;

Initialization of String

An array of characters known as character string may be initialized by placing the string in double quotes.

Char “Computer Science”;

Char “Calcutta”;

A string is a series of characters stored in consecutive bytes of memory.

This implies that one can store a string in an array of characters, with each character kept in its own array element.

The last character of every C++ string is Null character.

char city ;

is an array of characters while

char city ;

is a string because it is terminated by a null character.