Program to print the elements of a 2-D array in Row major and Column major orders

 
 #include <stdio.h>
 
 int main()
 {
    int Temp[3][2];
    int i, j;
 
    /*Read in the values for the array Temp*/
    printf("nEnter the elements of array Temp(3×2) row-wise...n");
    for(i=0;i<3;i++)
        for(j=0;j<2;j++)
            scanf("%d",&Temp[i][j]);
 
    /*Print the array Temp*/
    printf("nThe array Temp as entered is...n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<2;j++)
            printf("t%d",Temp[i][j]);
        printf("n");
    }
 
    /*Print the array Temp in row-major order*/
    printf("nThe array as entered in row major order is...n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<2;j++)
            printf("t%d",Temp[i][j]);
    }
    /*Print the array Temp in column-major order*/
    printf("nnThe array as entered in column major order is...n");
    for(j=0;j<2;j++)
    {
        for(i=0;i<3;i++)
            printf("t%d",Temp[i][j]);
    }
    return 0;
 }

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.