Wednesday 29 January 2014

Explain two dimensional array with example.Write a program to print a matrix and its transpose.

Two dimensional array-It is a collection of a number of one-dimensional arrays.Each row can be thought of as a one-dimensional array.
It is thought as a rectangular display of elements with rows and columns.
        column1        column2     column3
row1        x[0][0]        x[0][1]        x[0][2]
row2        x[1][0]        x[1][1]        x[1][2]
row3        x[2][0]        x[2][1]        x[2][2]

Write a program to display elements of two dimensional array.
#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
printf("Elements of an array");
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            printf(" \t %d ",a[i][j]);
            }
        printf(" \n ")
           
    }
   Output
   1 2 3
   4 5 6
   7 8 9

Write a program to enter elements of a matix with order 3x3 and print the matrix.


#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3];
printf("Enter elements of an array");
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            scanf(" \t %d ",&a[i][j]);
            }
        printf("\n")
    }
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            printf(" \t %d",a[i][j]);
            }
        printf("\n")
    }
       Output
    Enter elements of an array
       1 2 3
       4 5 6
       7 8 9
Write a program to enter elements of a matix with order 3x3 and display its transpose.

#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3];
printf("Enter elements of an array");
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            scanf(" \t %d ",&a[i][j]);
            }
        printf("\n")
    }
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            printf(" \t %d",a[i][j]);
            }
        printf("\n")
    }
printf("Transpose of matrix");
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            printf(" \t %d",a[j][i]);
            }
        printf("\n")
    }
}
    Output
Enter elements of an array
    1 2 3
    4 5 6
    7 8 9
Transpose of matrix
    1 4 7
    2 5 8
    3 6 9

No comments:

Post a Comment