Wednesday, January 30, 2013

MATRIXX

#include <stdio.h>

int main()

{

    int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, x, y, z;

    printf("Enter rows and column for first matrix: ");

    scanf("%d%d", &r1, &c1);

    printf("Enter rows and column for second matrix: ");

    scanf("%d%d",&r2, &c2);


/* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */

    while (c1!=r2)

    {

        printf("Error! column of first matrix not equal to row of second.\n");

        printf("Enter rows and column for first matrix: ");

        scanf("%d%d", &r1, &c1);

        printf("Enter rows and column for second matrix: ");

        scanf("%d%d",&r2, &c2);

    }


/* Storing elements of first matrix. */

    printf("\nEnter elements of matrix 1:\n");

    for(x=0; x<r1; ++x)

    for(y=0; y<c1; ++y)

    {

        printf("Enter elements a%d%d: ",x+1,y+1);

        scanf("%d",&a[x][y]);

    }


/* Storing elements of second matrix. */

    printf("\nEnter elements of matrix 2:\n");

    for(x=0; x<r2; ++x)

    for(y=0; y<c2; ++y)

    {

        printf("Enter elements b%d%d: ",x+1,y+1);

        scanf("%d",&b[x][y]);

    }


/* Initializing elements of matrix mult to 0.*/

    for(x=0; x<r1; ++x)

    for(y=0; y<c2; ++y)

    {

       mult[x][y]=0;

    }


/* Multiplying matrix a and b and storing in array mult. */

    for(x=0; x<r1; ++x)

    for(y=0; y<c2; ++y)

    for(z=0; z<c1; ++z)

    {

        mult[x][y]+=a[x][z]*b[z][y];

    }


/* Displaying the multiplication of two matrix. */

    printf("\nOutput Matrix:\n");

    for(x=0; x<r1; ++x)

    for(y=0; y<c2; ++y)

    {

        printf("%d  ",mult[x][y]);

        if(y==c2-1)

            printf("\n\n");

    }

    return 0;

}


output
Enter rows and column for first matrix: 0 5
Enter rows and column for second matrix: 9 2
Error! column of first matrix not equal to row of second.
Enter rows and column for first matrix: 9 2
Enter rows and column for second matrix: 5 0
Error! column of first matrix not equal to row of second.
Enter rows and column for first matrix:



No comments:

Post a Comment