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

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...
Data Type And Storage And Keywords To store data inside the computer we need to first identify the type of data elements we need in our program. There are several different types of dat...
How to install flutter | Flutter tutorial Flutter is the google’s mobile app SDK for develop the high-quality native app for iOS and android. We can develop the app by use of DART and javascri...
Loops in java Loops in JAVA Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. Loop is used in programming ...
Custom View by use of xib in swift Custom View by use of xib in swift We can create the custom and common view by the use xib in swift 3.1 and xcode 8.3.2 . Custom views are common and...
Use of CollectionViewDataSource method moveItemAt ... Use of CollectionViewDataSource method moveItemAt for Reordering CollectionView cell In IOS (Swift 4) In this tutorial we are study about how we can ...

Leave a Reply

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

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.