Skip to main content

Multi Dimensional Array(2D and 3D ) in C Program #DOTC_Mdb

Multi Dimensional Array in C.

Hi Everyone,

Today we will discuss about Multi Dimensional Array in C. In Previous blog we discussed about Array so before going through Multi Dimensional Array please go through my below blog first about one dimensional Array so that it’s easy for you to understand 2D array and 3D arrays.

 

https://dheeraj60.blogspot.com/2020/06/array-and-its-beneifts-in-c-program.html

 

Let’s Discuss about Multi Dimensional Array now

Creating Array of an Arrays in C is known as Multi-Dimensional Array .Multidimensional Array is of 2D array and 3D array . Two Dimensional (2D) array also known as Matrix . Matrix can be represented as table with rows and columns.

 

Let’s understand the syntax of 2D array .

Int x[3][4];

In Previous blog we already know about 1D array and it’s declaration here in 2D array we declare the array in same way with another [] assuming one for rows and another for column .

Here, x is a two-dimensional (2d) array. The array can hold 12 elements. An array as a table with 3 rows and each row has 4 columns so 3*4=13 so we ca say this array can hold 12 elements.

Here rows is 3 and 4 is column assume this in table.

Similarly we can declare three dimensional (3D) array as below .

int i[2][4][3];

 

Here array I can hold 24 elements 2*4 *3=24;

 

Let’s understand 2D array with one Program where we use 2 rows and 3 columns and takes the input from user and display the elements of array and the value which looks similar to tables with rows and columns.

 

Below is the 2D program with rows and columns.

#include <stdio.h>

#include <stdlib.h>

int main()

{

   int tab[2][3];

   int i, j;

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

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

         printf("Please enter value for tab[%d][%d]:", i, j);

         scanf("%d", &tab[i][j]);

      }

   }

  

   printf("2D array elements:\n");

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

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

         printf("%d ", tab[i][j]);

         if(j==2){

            printf("\n");

         }

      }

   }

       return 0;

}

 

Here Output will be with 2 rows and 3 column value and user needs to give input 6 times as in array we declare 2 for rows and 3 for columns.

In above Program .

1.     We declare 2D array as int tab[2][3];  where 2 is rows and 3 is columns.

2.        Counter variables for the loop to take input from the users.

3.      And in last we just display the array elements to see the matrix.

 

Below is the output of above program in compiler code- block for better understanding.

Here in output we can see user input 6 values with rows and columns and table shows 2 rows and 3 columns. You can change row and column with different value as per your requirement.

Note we can declare above array as below also

Tab[2][3]={10,12,13} , {12,13,14} as hard coded value if you want to use this value in program as here also two value passed that is row and in  row we give 3 values as column.

Let’s understand with one more programs where we can display marks in array. So suppose we declare array, mark[3][3]; so total elements hold in array mark is 3*3=9 .  Program is similar to above but here we use nested for loop to display the elements and take the value from user.

Below is the Program we will explain the logic after this.

int main()

{

    int mark[3][3];

   int i, j;

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

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

         printf(" \n Please enter value for mark[%d][%d]:", i, j);

         scanf("%d", &mark[i][j]);

      }

    }

   printf("\n  Marks values in Matrix are: \n\n");

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

   {

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

       {

           printf("%d\n" , mark[i][j]);

       }

       printf("\n");

   }

   printf("\n\n");

    return 0;

}

 

Here we declare  2D array for mark[3][3] ; that will hold 9 elements

After that we used Counter variables for the loop and we use for nested loop where inner loop display the value of marks passed by user and outer loop used to print space between the marks .

 

Below is the output in compiler Code Block for your understanding.

In above program for 2D Array we simply use nested for loop where inner loop displays the element value of array as outer loop used to display the space or next lineHope you are able to understand how this program take input of 2D array and display the elements by using Nested for loop. You can write several programs using 2D to store the students marks and display it by using loops and other program as well like sum of two matrixes etc.

Let’s understand one 3D matrix where we simply use 3 variable to store and display the elements of array.

 

Below is the Program for 3D.

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

int threedi[2][3][2];

 

  printf(" Please enter the 12 values: \n");

 

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

  {

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

    {

      for (int k = 0; k < 2; ++k)

      {

        scanf("%d", &threedi[i][j][k]);

      }

    }

  }

 

 

  printf("\n Printing the values:\n");

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

  {

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

    {

      for (int k = 0; k < 2; ++k)

      {

        printf("test[%d][%d][%d] = %d\n", i, j, k, threedi[i][j][k]);

      }

    }

  }

 

    return 0;

}

 

Let’s understand this 3D program here we initialize array threedi[2][3][2]; it means threedi can hold 12 elements as 2*3*2 =12 so we have taken 3 variable I , j , k and by using loop we store 12 value in array passed by user and simply displaying all the array elements by using loop and printf command to display the 3D matrix.

 

See the below output of above program in code compiler for your understanding.

 

 

 

Please see this blog carefully and write as much 2D and 3D program as you can as per your requirement and let me know if you have any doubt on Array programs. Hope you are able to understand the benefits of using Array in the programs . Later we use array in pointer and functions program so please practice array program . In next blog I will come up with String uses in C and practice the program by using loops and Array.

 

Thanks


Comments

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li