Home

A program to declare two-dimensional array and read the elements into array

C Programming Language 057

The following code shows how to write a program to declare two-dimensional array and read the elements into array. Print the elements in matrix format.

Program

#include <stdio.h>

void main()
{
    int a[10][10], i, j, m, n;
    printf("Enter order of matrix m,n [m x n] : ");
    scanf("%d %d", &m, &n);

    printf("Enter values: ");
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }

    printf("Matrix is \n");
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%d\t", a[i][j]);
        }
        printf("\n");
    }

    printf("\n");
}

Result

Enter order of matrix m,n [m x n] : 2 2
Enter values: 1 2 3 4
Matrix is
1       2
3       4


Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments