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....

Macro Pass 1 and Pass 2

Hello, I presumed that you are aware of various databases needed for the Pass 1 &2. Please provide a following with the input file macroinput.txt I request you to post the screenshot of the output. import java.util.*; import java.io.*; class MntTuple { String name; int index; MntTuple(String s, int i) { name = s; index = i; } public String toString() { return("[" + name + ", " + index + "]"); } } class MacroProcessor { static List<MntTuple> mnt; static List<String> mdt; static int mntc; static int mdtc; static int mdtp; static BufferedReader input; static List<List <String>> ala; static Map<String, Integer> ala_macro_binding; public static void main(String args[]) throws Exception { initializeTables(); System.out.println("===== PASS 1 =====\n"); pass1(); System.out.println("\n===== PASS 2 =====\n"); pass2(); } static void pas...

SC ANN Course Material

Hello Enthusiasts, Checkout the following course material. It will be regularly updated. Quiz 1 Quiz 2 Upload the Assignment 1 Upload the Assignment 2 MSc_IT_All_SC_ANN_Syllabus_2019_2020 MSc_IT_All_Sub_Syllabus_2019_2020 Github Notes PPTs  Topic Link Introduction to Fuzzy Logic 1 Intro_to_Fuzzy Fuzzy Rules 2 Fuzzy Rules Defuzzification 3 Defuzzification NN_Introduction 4 NN_Introduction NN_Architecture 5 NN_Architecture NN_Training 6 NN_Training References: [1]  https://cse.iitkgp.ac.in/~dsamanta/courses/sca/index.html [2] NPTEL YouTube Channel-       https://www.youtube.com/channel/UC0FyHh2HW62lSCYeRYfNqww As complexity rises, precise statements lose meaning and meaningful statements lose precision -  Lotfi A Zadeh