Tuesday, 22 May 2012

two dimensional array in c-lang

Pattern :    
                     data type   array name[rows][cols];

example   int roll[3][3];

this declaration creates following memory scenario.

                                   col0                col1              col2
               
    row0                      -----                   ----                  -----

 
    row1                     -----                   -----                  -----


    row2                     ----                     ----                   -----


Accessing this array


Most confusing thing in two dimensional array is how to access it's elements.

-------------------------------------------------------------------------------------------------------------------------------


 Now i am giving it's answer in following way

 In maths if we want to access row0,col0 data then we use(0,0)

  in  case of c language  we access   array_name[row_number][col_number]

   so (0,0)   in c language      roll[0][0]

  if we want to access row0,col1 data then we use(0,1)

   so (0,1)   in c language      roll[0][1]

  if we want to access row0,col2 data then we use(0,2)

   so (0,2)   in c language      roll[0][2]

   using for loop we can access directly. What you notice here  

    row is constant means 0 but column varies 0,1,2
   
    so   for(col=0;col<3;col++)
           { 

    
             do your task using roll[0][col]


            }

 -------------------------------------------------------------------------------------------------------------------------------

  In maths if we want to access row1,col0 data then we use(1,0)


  so (1,0)   in c language      roll[1][0]


  if we want to access row1,col1 data then we use(1,1)

   so (1,1)   in c language      roll[1][1]

  if we want to access row1,col2 data then we use(1,2)

   so (1,2)   in c language      roll[1][2]


   similarly  for row 1 and column 0,1,2

           for(col=0;col<3;col++)
           { 

    
             do your task using roll[1][col]


            }

   what's this thing we just need to change only row rest is same

-------------------------------------------------------------------------------------------------------------------------------


  In maths if we want to access row2,col0 data then we use(2,0)


  so (2,0)   in c language      roll[2][0]


  if we want to access row2,col1 data then we use(1,1)

   so (2,1)   in c language      roll[2][1]

  if we want to access row2,col2 data then we use(1,2)

   so (2,2)   in c language      roll[2][2]

   similarly  for row 2 and column 0,1,2


           for(col=0;col<3;col++)
           { 

    
             do your task using roll[2][col]


            }

  -------------------------------------------------------------------------------------------------------------------------------

  anyone got idea what i want to say. Yes Here we repeating similar loop row 0, row 1
  row2 . We have some way to reduce this task . How it is possible.

   Solution :
               using second loop out side the column .
   
         for(row=0;row<3;row++)
         {
               for(col=0;col<3;col++)
              { 

    
                do your task using roll[row][col]


               }

          }
   


what this thing. It simply repeat column loop for each row . This way we can use          two  dimensional array.
-------------------------------------------------------------------------------------------------------------------------------






Read latest blog







No comments:

Post a Comment