C- ARRAY

Arrays are widely used data type in ‘C’ language. It is a collection of elements of similar data type. These similar elements could be of all integers, all floats or all characters. An array of character is called as string whereas and array of integer or float is simply called as an array. So array may be defined as a group of elements that share a common name and that are defined by position or index. The elements of an arrays are store in sequential order in memory.

There are mainly two types of Arrays are used:

  • One dimensional Array
  • Multidimensional Array

One dimensional Array

declaration

int i;

declares a single variable, named i, of type int. It is also possible to declare an array of several elements. The declaration

int a[10];

the ten elements of a 10-element array are numbered from 0 to 9.

 

Multidimensional Array

The declaration of an array of arrays looks like this:

int a[size][size];

 

int i, j;

for(i = 0; i < 5; i = i + 1)

{

for(j = 0; j < 7; j = j + 1)

a[i][j] =  i + j;

}

Program to fill in a 2D array with numbers 1 to 6 and to print it out row-wise.

#include <stdio.h>

void main( )

{

int i, j, num[2][3] ;

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

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

num[i][j] =  i * 3 + j + 1 ;

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

{

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

printf(“%d “,num[i][j]  ) ;

printf(“\n” );

}

}

error: Content is protected !!