C Program for Performing Matrix Multiplication

C Program for performing matrix multiplication using pointers and without pointers

// This code is written by Souvik Ghosh
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
 int row, column;
 int sgg[10][10];
} MATRIX;
// Function for input the matrix
void input(MATRIX *p)
{
 printf("Please enter number of rows");
 scanf("%d", &p->row);
 printf("Please enter number of column");
 scanf("%d", &p->column);
 for (int i = 0; i < p->row; i++)
    {
 for (int j = 0; j < p->column; j++)
        {
 printf("Enter element [%d][%d]", i + 1, j + 1);
 scanf("%d", &p->sgg[i][j]);
        }
    }
}
// Function for output the matrix
void output(MATRIX m)
{
 for (int i = 0; i < m.row; i++)
    {
 printf("\n");
 for (int j = 0; j < m.column; j++)
        {
 printf("%d ", m.sgg[i][j]);
        }
    }
}
// Function for matrix multiplication using pointers
void multiplication(MATRIX m1, MATRIX m2, MATRIX *p)
{
 if (m1.column != m2.row)
    {
 printf("Matrix multiplication is impossible");
 return;
    }
 else
    {
 p->row = m1.row;
 p->column = m2.column;
 for (int i = 0; i < p->row; i++)
        {
 for (int j = 0; j < p->column; j++)
            {
 p->sgg[i][j] = 0;
 for (int k = 0; k < m1.column; k++)
                {
 p->sgg[i][j] += m1.sgg[i][k] * m2.sgg[k][i];
                }
            }
        }
    }
}
// Funcion for matrix multiplication without using pointers
MATRIX multiply(MATRIX m1, MATRIX m2)
{
 MATRIX t;
 t.row = m1.row;
 t.column = m2.column;
 for (int i = 0; i < t.row; i++)
    {
 for (int j = 0; j < t.column; j++)
        {
 t.sgg[i][j] = 0;
 for (int k = 0; k < m1.column; k++)
            {
 t.sgg[i][j] += m1.sgg[i][k] * m2.sgg[k][i];
            }
        }
    }
 return t;
}

void main()
{
 MATRIX m1, m2, s, t;
 input(&m1);
 input(&m2);
 multiplication(m1, m2, &s);
 printf("\nOutput Using First Function\n");
 output(s);
 printf("\nOutput Using Second Function\n");
 t = multiply(m1, m2);
 output(t);
}

Related Posts

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

HTML Cheatsheet

Unleash your web development potential with my HTML cheatsheet! Elevate your coding skills, master essential tags and attributes, and build stunning websites with ease. Whether you’re a…

Git&GitHub Cheatsheet

Elevate your Git and GitHub game with my ultimate cheatsheet! Unlock the power of version control, streamline collaboration, and boost productivity. Navigate the complexities of Git commands…

Command Line Cheatsheet

Master the Command Line effortlessly with my comprehensive cheatsheet! Streamline your workflow, boost productivity, and become a command line guru. Unlock essential commands, shortcuts, and tips to…

OpenAI’s APIs Notes

Unlock the potential of OpenAI’s APIs with my comprehensive notes! Dive into the world of cutting-edge artificial intelligence, explore the capabilities of GPT-4 and beyond, and learn…

SQL Notes

Discover the essence of SQL with my comprehensive notes! Dive into the world of Structured Query Language, unraveling the complexities, and mastering database management. Elevate your SQL…

Leave a Reply

Your email address will not be published. Required fields are marked *

Buy Me A Coffee