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

First Java Program First Java Program NOTE: You need to have java installed on your system. Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. W...
Firebase Push notification | Swift tutorial Push Notification by firebase in IOS.For Firebase Push notification we need to have paid apple developer account. You need to Enroll into the Apple D...
Introduction to JVM Introduction to JVM JVM stands for Java Virtual Machine. It works as an engine that derives the Java Code. The JVM does not understand Java ty...
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...
Compile C/C++ program (Dev-C/C++) what is Dev-C/C++? Dev-C/C++, developed by Bloodshed Software, is a fully featured graphical IDE(Integrated Development Environment),which ...
Create Firebase single Page Website Create firebase single page Website tutorial In this tutorial we are study how to create single page website by the use of firebase hosting its very ...

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.