Modal Box

Multi Dimensional Array

In simple words, an array created with more than one dimensions (size) is called as multi dimensional array.
Multi dimensional array can be of two dimensional array or three dimensional array or four dimensional array or more. Here we discuss 2 Dimensional Array only.

Two dimensional array may be declared as following:

syntax:[no-of-rows][no-of-cols]; //indexes may be more in case of 3D or more dimensional array.

int a[3][4]; // declaration of 2D array, here no. of rows is 3 and no. of cols is 4.

How 2 D array is arranged in the memory, we can understand with the following diagram.

Program to take input in a matrix (2 D array) and print only the diagonal elements

#include<stdio.h>
#include<conio.h> 
void main()
{
      int a[3][3], i=0, j=0;
       printf("enter the matrix elements");
           for(i=0; i<3; i++)
             {
                 for(j=0; j<3; j++)
                     {   
                         scanf("%d", &a[i][j]);
                       }
               }
         // loop to display only the diagonal elements.
         for(i=0; i<3; i++)
            {
                for(j=0; j<3; j++)
                      {
                          if(i==j)    //condition for the diagonal elements.
                            printf("%d ", a[i][j]);
else
printf(" ");
}
printf("\n");                                     } }  // end of main

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial