Saturday, November 14, 2015

Array, Multidimensional array

Array

Collection of similar type of data type called array. When we declare an integer or character variables, it requires memory space according to their data types. If program needs multiple variable to store value or to be used in later part of program, it is not good practice to declare similar data type variables again and again. Hence, To overcome this multiple declaration of variables, array is used. In array we can represent group of similar data by a single group name called array name and each item in an array called item or component of the array.


 storage-class data-type array-name[expression]; 

storage-class : type of storage class use(external,static, auto, register).
data-type : indicates types of array integer, character, float or structure type.
array-name : can be any name used to represent the array.
expression : this is the size of array, represents the number of elements in array and can be any positive integer value ranged allowed in programming.

Size of an array is multiplication of index and its size of data type.

Array stores each element sequentially in a fixed location, For example fist element of array stored in first reserved location, second on second reserved location, and so on until the last element stored in the last reserved location of array.

To access individual elements in an array requires some unique means of identifying each element. Since elements in the array are stored sequentially, any individual element can be accessed by giving the name of the array and the element's position. This position is called the element's subscript or index value. Better can be understood with examples given below.

ex:
int arr[10];   
 or  
 char str[100];  
 or  
 float[10];  

array "arr" is an integer array of size 10, but actual size in terms of memory be the 10*sizeof(int).

array indexing starts with zero so fist element of array is

arr[0] ,
2nd element arr[1],
.....
arr[expression-1] is last element of any array.




One Dimensional Array 
  Above arrays are example of One Dimensional array, also known as single-dimension array or a list. This is simplest form of array.

Modification of any element in single dimestion array usually done by directly accessing the element by its index value

Suppose if we want to access 4th element of bwlo array
int arr[8] = {3,5,8,1,7,2,9,4};  

Then,
 int value = arr[3]; //Directly accessing 4th index from 0 
 printf("Value = %d\n",value);  

Output

Value = 1  


Multidimensional Array

Array can be of any dimension, depends on what to implement. A better way to represent all dimension  in single example
 storage-class data-type array-name [expression 1] [expression 2] [expression 3]......[expression n];  

2x2,3x3 or nxn matrix is example of two dimensional array.



Array representation of 3x3 Matrix

int arr[row][column] = { {row0,col0},{row0,col1},{row0,col2},{row1,col0},{row1,col1},{row1,col2}, {row2,col0},{row2,col1},{row2,col2} };


Ex-



#include <stdio.h>  
 #define ROW 3  
 #define COL 3  
 main()  
 {  
     int matrix[ROW][COL] = { {2,6,8},{9,1,4},{7,8,3}};  
     int i,j;  
     printf("Matrix\n");  
     for(i=0; i < ROW; i++)  
     {  
         for(j=0; j < COL; j++)  
         {  
             printf("%d\t",matrix[i][j]);  
         }  
         printf("\n");  
     }  
 }  

Output
 $ gcc matrix.c   
 $ ./a.out   
 Matrix  
 2    6    8      
 9    1    4      
 7    8    3     

Three Dimensional Array 
Adding one more expression to array,

storage-class data-type array-name [expression 1] [expression 2] [expression 3];

If characters in a lines of a page can represent one dimensional array then a page filled with multiple line can represent two dimensional array and number of pages in a book can be a three dimensional array.

#define PAGES 30  //Number of pages in book
#define LINES 15  // Number of lines in one page
#define CHARACTERS 40   //Number of characters in one line
   
int book[PAGES][LINES][CHARACTERS];  


Reference :
Programming with C by Byron Gottfried.


No comments:

Post a Comment