[Legacy Content] This page contains very old information, which might not be valid anymore, they are only kept here for legacy purposes.
if you have any inquiries, feel free to contact me! Click here

X * Y Matrix with 2D Arrays

Created: Thursday, 13 November 2014 Written by Ehab Eldeeb

Write a program that takes X and Y as 2D arrays from the user with size 4x4 each
Multiply the two matrices in a third matrix Z [4x4 also] and output the result on the screen

 

#include <stdio.h>
#include <conio.h>

int main(){
    // Define the three arrays
    int x[4][4],y[4][4],z[4][4];

    // Get inputs for the X matrix
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            printf("Enter element [%d][%d] for X array: ",i+1,j+1);
            scanf("%d",&x[i][j]);
        }
    }

    // Get inputs for the Y matrix
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            printf("Enter element [%d][%d] for Y array: ",i+1,j+1);
            scanf("%d",&y[i][j]);
        }
    }

    // Multiply X and Y and put them in Z
    for (int i=0;i<4; i++){
        for (int j=0;j<4;j++){
            z[i][j]=x[i][j] * y[i][j];
        }
    }

    // Print out the result
    printf("\n============\nARRAY OUTPUT\n X * Y \n============\n\n");
    for (int i=0;i<4; i++){
        for (int j=0;j<4;j++){
            printf("%d\t", z[i][j]);
        }
        printf("\n\n");
    }
    getch();
    return 0;
}