C Tutorial

Define C

Define Programming

First C Program

Compiler vs Interpreter

C Variable

C Keywords

C Data Types

C Comments

TurboC Shortcut keys

C Operators

Hierarchy of Operators

Ex: Arithmetic Operator

C Formatting Output

C Escape Sequence

C if statement

Ex: If statement

C Switch statement

Ex: Switch

Increment / Decrement

C Loops

Ex: Loops

C Nesting Of Loops

Ex: Nested Loops

C Jumping Statements

C Exit(), Gotoxy()

C Arrays 1D

Ex: Arrays 1D

C Arrays 2D

Ex: Arrays 2D

C Sorting

ASCII Value

Character I/O Function

Ex: Character

C String Functions

Ex: Strings

Array of Strings

C Math Functions

User-defined Function

Exercise Function

Local, Reference variable

Function Calling types

Array Passing Function

Recursion Function

Ex: Recursion Function

Constant Variable

Storage Class

C Header Files

C Preprocessor

C Pointers

Pointer to Pointer

C Structures

Exercise Structure

C Typedef

C Enumeration

C File Handling

Ex: File Handling

Command Line Argument

MCQ

Question-Answer

Page Stats

Visitor: 1060

Double-Dimensional Arrays or 2d-Arrays

2D Array

Double-Dimensional Arrays or 2d-Arrays are the combination of rows and columns. Normally, a 2D-Array is use to represent data in the form of matrix.

Syntax to declare 2d-Arrays:

int a[3][3]; //3 rows and 3 columns

In the above syntax, 1st square bracket represents number of rows and 2nd square bracket represent number of columns. The above declaration tells the compiler that we are declaring 3 rows and 3 columns total 9 elements. We can specify any value in row and column.

Example 1: Input 3x3 matrix and print it.

#include<stdio.h>
#include<conio.h>
void main()
{
    int i, j, a[3][3];
    clrscr();
    printf("Enter 3x3 matrix");
    for(i=0;i<=2;i++)
    {
        for(j=0;j<=2;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }

    printf("Your 3x3 matrix is:\n");
    for(i=0;i<=2;i++)
    {
        for(j=0;j<=2;j++)
        {
            printf("\t%d",a[i][j]);
        }
        printf("\n");
    }
    getch();
}

Initialization of 2D Array

int a[3][3] = {{10,20,30}, {40,50,60}, {70,80,90} };

Example 2: WAP to initialize the matrix.

void main()
{
   int a[3][3] = {{10,20,30}, {40,50,60}, {70,80,90} };
   int i, j;
   clrscr();
   printf("\n3x3 Matrix is:\n");
   for(i=0;i<2;i++)
   {
      for(j=0;j<2;j++)
      {
         printf("%d  ",a[i][j]);
      }
      printf("\n");
   }
   getch();
}

Exercise Question: 2D-Arrays

  1. Input 3x3 matrix and transpose it.
  2. void main()
    {
        int a[3][3], i, j;
        clrscr();
        printf("Enter matrix");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        printf("Input Matrix is:\n");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                printf("%d  ",a[i][j]);
            }
        printf("\n");
        }
        
        printf("\nTranspose of a Matrix is:\n");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                printf("%d  ",a[j][i]);
            }
            printf("\n");
        }
        getch();
    }
  3. Input 3x3 matrix and find Row-wise sum, column-wise sum and diagonals.
  4. void main()
    {
        int a[3][3], i, j, r1=0, r2=0, r3=0, c1=0, c2=0, c3=0, d1=0, d2=0;
        clrscr();
        printf("Enter 3x3 matrix");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        
        printf("\nInput matrix is:\n");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                printf("%d  ",a[i][j]);
            }
            printf("\n");
        }
        
        for(i=0;i<=2;i++) //row
        {
            for(j=0;j<=2;j++) //col
            {
                if(i==0)
                    r1=r1+a[i][j];
                if(i==1)
                    r2=r2+a[i][j];
                if(i==2)
                    r3=r3+a[i][j];
                if(j==0)
                    c1=c1+a[i][j];
                if(j==1)
                    c2=c2+a[i][j];
                if(j==2)
                    c3=c3+a[i][j];
                if(i==j)
                    d1=d1+a[i][j];
                if(i+j==2)
                    d2=d2+a[i][j];
            }
        }
        printf("\nSum of 1st row = %d",r1);
        printf("\nSum of 2nd row = %d",r2);
        printf("\nSum of 3rd row = %d",r3);
        
        printf("\nSum of 1st column = %d",c1);
        printf("\nSum of 2nd column = %d",c2);
        printf("\nSum of 3rd column = %d",c3);
        
        printf("\nSum of 1st diagonal = %d",d1);
        printf("\nSum of 2nd diagonal = %d",d2);
        
        getch();
    }
  5. Find sum of upper and lower triangular matrices.
  6. void main()
    {
        int a[3][3], i, j, upper=0, lower=0;
        clrscr();
        printf("Enter 3x3 matrix");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        
        printf("\nInput matrix is:\n");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                printf("%d  ",a[i][j]);
            }
            printf("\n");
        }
        
        for(i=0;i<=2;i++) //row
        {
            for(j=0;j<=2;j++) //col
            {
                if(i<=j)
                    upper = upper +a[i][j];
                if(i>=j)
                    lower = lower+a[i][j];
            }
        }
        printf("\nSum of Upper triangular matrix = %d",upper);
        printf("\nSum of Lower triangular matrix = %d",lower);
        
        getch();
    }
  7. Find whether the input matrix is symmetric or not.
  8. void main()
    {
       int a[3][3], b[3][3], i, j, isSymmetric=1;
       printf("Enter 3*3 matrix :");
       for(i=0;i<=2;i++)
       {
          for(j=0;j<=2;j++)
          {
             scanf("%d",&a[i][j]);
          }
       }
      
       clrscr();
       printf("A matrix is:\n");
       for(i=0;i<=2;i++)
       {
          for(j=0;j<=2;j++)
          {
             printf("%d ",a[i][j]);
          }
          printf("\n");
       }
       
       for(i=0;i<=2;i++)
       {
          for(j=0;j<=2;j++)
          {
             b[i][j] = a[j][i];
          }
       }
       
       printf("Transpose of a matrix is:\n");
       for(i=0;i<=2;i++)
       {
          for(j=0;j<=2;j++)
          {
             printf("%d ",b[i][j]);
          }
          printf("\n");
       }
       
       for(i=0; i<=2; i++)
       {
          for(j=0; j<=2; j++)
          {
             if(a[i][j] != b[i][j])
             {
                isSymmetric = 0;
                break;
             }
          }
       }
    
       if(isSymmetric)
       {
          printf("\nInput Matrix is Symmetric.");
       }
       else
       {
          printf("\nInput Matrix is NOT Symmetric.");
       }
       getch();
    }
  9. Input two 3x3 matrix and print them.
  10. #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
        int i, j, a[3][3], b[3][3];
        clrscr();
        printf("Enter 1st 3x3 matrix");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
    
        printf("Enter 2nd 3x3 matrix");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                scanf("%d",&b[i][j]);
            }
        }
    
        printf("Your 1st 3x3 matrix is:\n");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                printf("\t%d",a[i][j]);
            }
            printf("\n");
        }
    
        printf("Your 2nd 3x3 matrix is:\n");
        for(i=0;i<=2;i++)
        {
            for(j=0;j<=2;j++)
            {
                printf("\t%d",b[i][j]);
            }
            printf("\n");
        }
    
        getch();
    }
  11. Input two 3x3 matrix and check matrices are equal or not.
  12. void main()
    {
       int a[3][3], b[3][3], i, j, isSymmetric=1;
       printf("Enter 3*3 matrix :");
       for(i=0;i<=2;i++)
       {
          for(j=0;j<=2;j++)
          {
             scanf("%d",&a[i][j]);
          }
       }
       for(i=0;i<=2;i++)
       {
          for(j=0;j<=2;j++)
          {
             scanf("%d",&b[i][j]);
          }
       }
       
       clrscr();
       printf("A matrix is:\n");
       for(i=0;i<=2;i++)
       {
          for(j=0;j<=2;j++)
          {
             printf("%d ",a[i][j]);
          }
          printf("\n");
       }
       
       printf("B matrix is:\n");
       for(i=0;i<=2;i++)
       {
          for(j=0;j<=2;j++)
          {
             printf("%d ",b[i][j]);
          }
          printf("\n");
       }
       
       for(i=0; i<=2; i++)
       {
          for(j=0; j<=2; j++)
          {
             if(a[i][j] != b[i][j])
             {
                isSymmetric = 0;
                break;
             }
          }
       }
    
       if(isSymmetric)
       {
          printf("\nBoth matrix are equal.");
       }
       else
       {
          printf("\nMatrix are not equal.");
       }
       getch();
    }
  13. Input two 3x3 matrix and sum them.
  14. Input two 3x3 matrix and multiple them.
Updated: 10-Jul-21