C Program to Performing Matrix Addition

C Program for performing matrix addition using structure

// This code is written by Souvik Ghosh
#include <stdio.h>
typedef struct
{
 int M[10][10];
 int row, column;
} matrix;

void input(matrix *p)
{
 int row, column;
 printf("Enter the number of rows\n");
 scanf("%d", &row);
 printf("Enter the number of column\n");
 scanf("%d", &column);
 printf("Enter Elements\n");
 p->row = row;
 p->column = column;
 for (int i = 0; i < p->row; i++)
    {
 for (int j = 0; j < p->column; j++)
        {
 scanf("%d", &p->M[i][j]);
        }
    }
}
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.M[i][j]);
        }
    }
}
void addition(matrix *m1, matrix *m2)
{
 for (int i = 0; i < m1->row; i++)
    {
 for (int j = 0; j < m2->column; j++)
        {
 m1->M[i][j] += m2->M[i][j];
        }
    }
}
void main()
{
 matrix m1, m2;
 printf("Enter First Matrix\n");
 input(&m1);
 printf("Enter Second Matrix\n");
 input(&m2);
 if (m1.row != m2.row || m1.column != m2.column)
    {
 printf("Matrix Adition Not Possible\n");
    }
 else
    {
 addition(&m1, &m2);
 output(m1);
    }
 
}

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 *