Turn Desktop View Off
by Dinesh Thakur

A collection of variables which are all of the same type. It is a data structure, which provides the facility to store a collection of data of same type under single variable name. Just like the ordinary variable, the array should also be declared properly.

 

The declaration of array includes the type of array that is the type of value we are going to store in it, the array name and maximum number of elements.

 

 

Examples:

 

short val[ 200 ];

val[ 12 ] = 5;  

 

Declaration & Data Types

 

Arrays have the same data types as variables, i.e., short, long, float etc.They are similar to variables: they can either be declared global or local.They are declared by the given syntax:

 

Datatype array_name[dimensions]={element1,element2,….,element}

 

The declaration form of one-dimensional array is

 

Data_type array_name [size];

 

The following declares an array called ‘numbers’ to hold 5 integers and sets the first and last elements. C arrays are always indexed from 0. So the first integer in ‘numbers’ array is numbers[0] and the last is numbers[4].

 

int numbers [5]; numbers [0] = 1; // set first element numbers [4] = 5;

 

This array contains 5 elements. Any one of these elements may be referred to by giving the name of the array followed by the position number of the particular element in square brackets ([]). The first element in every array is the zeroth element. Thus, the first element of array ‘numbers’ is referred to as numbers[ 0 ], the second element of array ‘numbers’ is referred to as numbers[ 1 ], the fifth element of array ‘numbers’ is referred to as numbers[ 4 ], and, in general, the n-th element of array ‘numbers’ is referred to as numbers[ n - 1 ].

 

Example:

Example:

#include <stdio.h>

#include <conio.h>

int main( )

{

char name[7]; /* define a string of characters */

name[0] = 'A';

name[1] = 's';

name[2] = 'h';

name[3] = 'r';

name[4] = 'a';

name[5] = 'f';

name[6] = '\0'; /* Null character - end of text */

name[7] = ‘X’;

clrscr();

printf("My name is %s\n",name);

printf("First letter is %c\n",name[0]);

printf("Fifth letter is %c\n",name[4]);

printf("Sixth letter is %c\n",name[5]);

printf("Seventh letter is %c\n",name[6]);

printf("Eight letter is %c\n",name[7]);

getch();

return 0;

}

 

Output

 

My name is Ashraf

First letter is A

Fifth letter is a

Sixth letter is f

Seventh letter is Null

Eight letter is X

 

Two dimensional array

 

Two-dimensional array are those type of array, which has finite number of rows and finite number of columns. The declaration form of 2-dimensional array is

Data_type Array_name [row size][column size];

 

Example:

 

#include<stidio.h>

#include<conio.h>

int main()

{

int matrix[3][3],I,j,r,c;

clrscr();

printf(“Enter the order of matrix\n”);

scanf(“%d%d”,&r,&c);

printf(“Enter the elements of 3x3 matrix\n”,r,c);

for(i=0;i<r;i++)

for(j=0;j<c;j++)

scanf(“%d”,&matrix[i][j]);

printf(“Given matrix:\n”);

for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf(“%d\t”,matrix[i][j]);

printf(“\n”);

}

getch();

return 0;

}

 

Output

1 2 3

2 3 4

5 6 7