Array

Array

An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first element in the array is numbered 0, so the last element is 1 less than the size of the array. Before using an array its type and dimension must be declared.

  • Array is a data structure storing a group of elements, all of which are of the same data type.
  • all arrays in C are indexed from 0 up to one less than the bound given in the declaration.
  • All the elements of an array share the same name, and they are distinguished from one another with the help of an index.
  • Random access to every element using a numeric index.
  • The declaration of an array is just like any variable declaration with additional size part, indicating the number of elements of the array. Like other variables, arrays must be declared at the beginning of a function.

Array Declaration

 

  • The declaration specifies the base type of the array, its name, and its size or dimension. array is called a homogeneous data structure. The data type of an array applies uniformly to all the elements.
  • The amount of storage for a declared array has to be specified at compile time before execution. This means that an array has a fixed size.

Syntax of Array Declaration

data-type array_name [constant-size];

      Data-type refers to the type of elements you want to store

        Constant-size is the number of elements

Example of declarations for arrays:

int marks [60];

float numbers [100];

static int roll_no [60];

char subject [30];

Initialization of Array Elements in the Declaration

The values are assigned to individual array elements enclosed within the braces and separated by comma. Syntax of array initialization is as follows:

data type array-name [ size ] = {val 1, val 2, …….val n};

val 1 is the value for the first array element, val 2 is the value for the second element, and val n is the value for the n array element. Note that when you are initializing the values at the time of declaration, then there is no need to specify the size. Let us see some of the examples given below:

int digits [10] = {1,2,3,4,5,6,7,8,9,10};

int digits[ ] = {1,2,3,4,5,6,7,8,9,10};

int vector[5] = {12,-2,33,21,13};

float temperature[10] ={ 31.2, 22.3, 41.4, 33.2, 23.3, 32.3, 41.1, 10.8, 11.3, 42.3};

double width[ ] = { 17.33333456, -1.212121213, 222.191345 };

int height[ 10 ] = { 60, 70, 68, 72, 68 };

Multi-Dimensional Arrays

Arrays with more than one dimension are called multi- dimensional arrays. The first number in brackets is the number of rows, the second number in brackets is the number of columns. So, the upper left corner of any grid would be element [0][0]. The element to its right would be [0][1], and so on. Here is a little illustration to help.

 

[0][0]

[0][1]

[0][2]

[1][0]

[1][1]

[1][2]

[2][0]

[2][1]

[2][2]

 

Multi-Dimensional Array Declaration

You can declare an array of two dimensions as follows:

datatype array_name[size1][size2];

variable_type is the name of some type of variable, such as int. size1 and size2 are the sizes of the array’s first and second dimensions, respectively. Here is an example of defining an 8-by-8 array of integers

int chessboard [8][8];

 

Related Post

Create Gradient layer by the use of CAGradientLaye... Create Gradient layer by the use of CAGradientLayer Mostly developers are use the gradient (Combination of colors) colors images for make app more ef...
E-Mail Format Formal Email / Letter Format A formal email is in general sent to someone you don’t know well or to someone who’s in authority. Examples of someone w...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Invitation Letter for Visa Invitation Letter Samples These letter are necessary for taking visa of another country. so these samples are the most important document. Sample 1 ...
Introduction to Java Programming Language JAVA TUTORIALS Introduction to Java Programming Language What is JAVA Java is programming language that produces software for multiple platforms, w...
Java If-else Statement Java If-else Statement The Java if statement is used to test the condition. It checks boolean condition: true or false. For example, if a number i...

Leave a Reply

Your email address will not be published. Required fields are marked *