Skip to main content

Program to multiply two matrices

Hello, I hope you are able to add two matrices as per the previous post.
Now, we will go a step further and multiply the matrices. To multiply matrices, it should satisfy the basic condition i.e. number of columns in the first matrix should be equal to the number of rows in the second matrix.

Let's try it out.
Don't forget to post the screenshot of the output in the comment section.

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

void main()
{

    int mat1[10][10], mat2[10][10], mul[10][10], r1, c1, r2, c2, i, j, k;
    clrscr();
    printf("Enter rows and column for first matrix: \n");
    scanf("%d %d", &r1, &c1);

    printf("Enter rows and column for second matrix: \n");
    scanf("%d %d",&r2, &c2);

    // Column of first matrix should be equal to column of second matrix and
    while (c1 != r2)
    {
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix:\n ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: \n");
scanf("%d %d",&r2, &c2);
    }

    // Storing elements of first matrix.
    printf("\nEnter elements of matrix 1:\n");
    for(i=0; i<r1; i++)
for(j=0; j<c1; j++)
{
    printf("Enter elements mat1[%d][%d]: ",i+1, j+1);
    scanf("%d", &mat1[i][j]);
}

    // Storing elements of second matrix.
    printf("\nEnter elements of matrix 2:\n");
    for(i=0; i<r2; i++)
        for(j=0; j<c2; j++)
        {
            printf("Enter elements mat2[%d][%d]: ",i+1, j+1);
            scanf("%d",&mat2[i][j]);
        }

    // Initializing all elements of mul matrix to 0
    for(i=0; i<r1; i++)
        for(j=0; j<c2; j++)
        {
            mul[i][j] = 0;
        }

    // Multiplying matrices mat1 and mat2 and
    // storing result in mul matrix
    for(i=0; i<r1; i++)
        for(j=0; j<c2; j++)
            for(k=0; k<c1; k++)
            {
                mul[i][j]+=mat1[i][k]*mat2[k][j];
            }

    // Displaying the mul
    printf("\nOutput \n Multiplication Matrix:\n");
    for(i=0; i<r1; i++)
        for(j=0; j<c2; j++)
        {
            printf("| % d | \t", mul[i][j]);
            if(j == c2-1)
                printf("\n\n");
        }
    getch();
}

Comments

Popular posts from this blog

CloudSimSDN Compiling Issues

Hello, this is regarding issues such as Main class not found or Can not find symbol while executing or compiling. You just need to set proper path of your cloudsimsdn directory. I stored CloudSimSDN folder downloaded from github  at location E:\ with the name cloud. It's very much important to provide proper jar file's address as well as providing complete path of file that has to be inputed such as energy-physical.json.  Set Java path variable value:  C:\Program Files\Java\jdk1.8.0_121\bin CLASSPATH value : E:\cloud\jars\*; E:\cloud\src; For Compiling javac E:\cloud\src\org\cloudbus\cloudsim\sdn\example\ SDNExample.java For Executing java org.cloudbus.cloudsim.sdn.example.SDNExample MFF E:\cloud\dataset-energy\energy-physical.json E:\cloud\dataset-energy\energy-virtual.json E:\cloud\dataset-energy\energy-workload.csv > results.out These commmands are tested with Windows 10 and Latest java version. Please share your valuable comments and queries....

Parallel Computing

Parallel Computing- Its all about parallel thinking. Recent technologies, Various discussions, interesting facts about Parallel Computing & high-performance computing will be posted here very soon. ⇋  Stay Tuned Parallely ⇋ PDS Quiz 1 PDS_Quiz 2 PDS_Quiz3 PDS_Quiz 4 PDS_Quiz 5 Course Exit Survey 2019

MACRO DATABASE

Great to know that you are curious about the databases used in the MACRO Pass1 and Pass2. Here we Go.!!!!! Pass 1: Databases Source Program Output of Source Program to be fed to Pass 2 Macro Definition Table (MDT)- Stores Macro Definitions Macro Name Table(MNT) - Stores the Macro Name that are defined in the Program Macro Definition Table Counter (MDTC) - Indicates next available entry in MDT Macro Name Table Counter (MNTC) - Indicates next available entry in MNT Argument List Array (ALA)-  Stores the index markers for dummy arguments Pass 2: Databases Input Source Program Expanded Source Code i.e  Output for Assembler  Macro Definition Table (MDT)- Created and Maintained by Pass 1 Macro Name Table(MNT) -  Created and Maintained by Pass 1  Macro Definition Table Pointer (MDTP) - Indicate next line used in Expansion. Arguments List Arrays (ALA)- Substitutes arguments for index markers. Example       ...