Arrays in 'C' Language

Array in 'C' Language: 
  • Array is a collection of more than one elements having same data type.
  • Elements having same data type are called as homogeneous elements.
  • We can also say that array is a list of elements. 
  • There are three types of arrays :
1) One-dimensional Array : 
  • An array which store its elements into a single row is called one dimensional array.
  • This dimension is basically row.

2) Two-dimensional Array : 
  • An array which store its elements using row and column (two dimensions) that means in tabular or matrix form is called two-dimensional array.
  • These two dimensions are basically row and colum.

3) Multi-dimensional Array : An array which have more than two dimensions to store its elements is called multi-dimensional array.

Array Operations :
  • We can perform three basic operations with an array variable such as declaring array, initializing array and accessing elements of an array.
  • Way of performing these operations is as below
Declaration of Array : 
  • Since 'C' is a compiled language we must declare an array before using it.
  • An array can be declared by using following syntax 
     data_type array_name[size];
 Where
     data_type specifies type of elements that       we want to store into array.
     array_name indicates name of array it is       an identifier.
     size specifies no of elements that can be         stored into respective array.
  • For example int num[20]; determines that num is an array of type integer with size 20 that means num can store 20 integer values in it.
  • For 2-D array syntax is 
     data_type array_name[row][col];
 Where
     data_type specifies type of elements that       we want to store into array.
     array_name indicates name of array it is       an identifier.
     row specifies no. of rows in array.
     col specifies no. of  columns in array.


Initialization of Array : 
  • Array can be initialized at the time of declaring array itself or after declaration.
  • Every element of array has an index.
  • Array index always starts with 0.
  • We can assign multiple values to array indices at a time or can assign single element to array index individually.
  • Syntax
     data_tye array_name[size]={ value1,value2,value3,....};
           Or
     array_name[index] = value;
  • For example : 
      int num[5]={10,20,30,45,56};
  • Here we are storing 5 elements into array num while declaring it.
  • Now consider 
     int num[5];
     num[0]=10;
     num[1]=20;
     num[2]=30;
     num[3]=45;
     num[4]=56;
  • Here we store array elements individually. Both of the above two examples results in same array.

Accessing elements of an Array :
  • Array elements can be accessed by using index of array element.
  • Syntax : array_name[index];
  • We can also iterate through array by using loop.
Example : Consider following programs
Program 1: 
int main()
{
   int num[5]={10,20,30,45,56};
   printf(" %d  ",num[2];
   return 0;
}

Output :
 30

Program 2:
int main()
{
   int num[5]={10,20,30,45,56};
   int i;
   for(i=0;i<=4;i++)

     printf("%d\n ",num[i]);
   return 0;
}

Output :
10
20
30
45
56


Memory Representation of a Two-Dimensional Array : 
  • Array elements always gets stored at a consecutive (Sequencial or serial) memory allocations.
  • Name of array always represents a base address (starting address or address of element at 0th position) of an array.
  • There are two ways how computer system stores an array elements into memory.
  • Those ways are called as row major form and column major form of memory allocation.
  • In row major form elements of array gets stored at consecutive memory locations row-wise.
  • Where as in column major form elements of array gets stored at consecutive memory locations column-wise.
  • For example : Consider two dimensional array as 
     int num[2][3]={(2,4,6),(13,3,7)};                 An array with two rows and three columns.
 Assume that base address of array is 100   then in row major form elements will gets   stored as :
In column major form above array will get stored as :


Passing Array to Function as an Argument :
  • We can also pass entire array to function as a argument to perform operations on it.
  • Consider following program to find maximum element of array 
Output :
Note : Similarly we can write program to find minimum element of an array do it as an assignment.
                

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.