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

New features in XCode 10 Supported Configurations Xcode 10 beta requires a Mac running macOS 10.13.4 or later Xcode 10 beta includes SDKs for iOS 12, watchOS 5, macOS ...
Variables in Java Variables in Java A variable is something which is associated with a value can be changed. For example: int i=10; Here variable name “i” wh...
Asset Color Catalog | Swift Tutorial Asset Color Catalog is the feature of XCode 9. BY the use of this feature we can maintain the colors easily and avoid the mistakes. We can create the ...
Control Flow Statements | Swift tutorial Control Flow : Swift tutorial Swift Provides diffrent type of controll flow statements like: Loops Statements : Loops are used for perform the t...
Strings String can be represented as a single-dimensional character type array. Strings in C are group of characters, digits, and symbols enclosed in quotatio...
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...

Leave a Reply

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