Jump to content

Archive:Matrix multiplication using C: Difference between revisions

From IdeaWazaWiki
wikademia>Sebmol
m Reverted edits by Stinky sock (Talk) to last version by JWSchmidt using rollback
wikademia>Wikademia
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<pre>/* Program to implement matrix multiplication. */
<source lang="c">/* Program to implement matrix multiplication. */
/* Performed by Milan. */
/* Performed by Milan. */
/* Edited by Gergo: resolved some portability issues */


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


void main()
int main(void)
{
{
int a[10][10], b[10][10], c[10][10];
        int a[10][10], b[10][10], c[10][10];
int i, j, m, n, x, y, k;
        int i, j, m, n, x, y, k;
clrscr();


/* Entering values for matrix a...*/
        /* Entering values for matrix a...*/
printf ("\n Enter the rows and columns of a: ");
        printf ("\n Enter the rows and columns of a: ");
scanf ("%d%d", &m, &n);
        scanf ("%d%d", &m, &n);


for (i = 0; i < m; i++)
        for (i = 0; i < m; i++)
{
        {
for (j = 0; j < n; j++)
                for (j = 0; j < n; j++)
{
                {
printf (" a[%d][%d] = ", (i+1), (j+1));
                        printf (" a[%d][%d] = ", (i+1), (j+1));
scanf ("%d", &a[i][j]);
                        scanf ("%d", &a[i][j]);
}
                }
printf ("\n");
                printf ("\n");
}
        }


/* Entering values for matrix b... */
        /* Entering values for matrix b... */
printf ("\n Enter the rows and columns of b: ");
        printf ("\n Enter the rows and columns of b: ");
scanf ("%d%d", &x, &y);
        scanf ("%d%d", &x, &y);


for (i = 0; i < x; i++)
        for (i = 0; i < x; i++)
{
        {
for (j = 0; j < y; j++)
                for (j = 0; j < y; j++)
{
                {
printf (" b[%d][%d] = ", (i+1), (j+1));
                        printf (" b[%d][%d] = ", (i+1), (j+1));
scanf ("%d", &b[i][j]);
                        scanf ("%d", &b[i][j]);
}
                }
printf ("\n");
                printf ("\n");
}
        }


/* Calculations */
        /* Calculations */
if ( n != x)
        if ( n != x)
{
        {
printf ("\n Error!! Rows of a don't match with columns of b.");
                printf ("\n Error!! Rows of a don't match with columns of b.");
printf ("\n Multiplication Impossible.");
                printf ("\n Multiplication Impossible.");
getch();
                exit(EXIT_FAILURE);
exit(0);
        }
}
        else
else
        {
{
                for (i = 0; i < m; i++)
for (i = 0; i < m; i++)
                {
{
                        for (j = 0; j < y; j++)
for (j = 0; j < y; j++)
                        {
{
                                c[i][j] = 0;
c[i][j] = 0;
                                for (k = 0; k < n; k++)
for (k = 0; k < n; k++)
                                {
{
                                        c[i][j]+=a[i][k]*b[k][j];
c[i][j]+=a[i][k]*b[k][j];
                                }
}
                        }
}
                }
}
        }
}


/* Output */
        /* Output */
printf ("\n The resultant c is:\n");
        printf ("\n The resultant c is:\n");
for (i = 0; i < m; i++)
        for (i = 0; i < m; i++)
{
        {
for (j = 0; j < y; j++)
                for (j = 0; j < y; j++)
{
                {
printf (" c[%d][%d] = %d", (i+1), (j+1), c[i][j]);
                        printf (" c[%d][%d] = %d", (i+1), (j+1), c[i][j]);
}
                }
printf ("\n");
                printf ("\n");
}
        }


getch();
        return 0;
}
}
/* Output:
/* Output:
Line 97: Line 96:
  c[2][1] = 14 c[2][2] = 11
  c[2][1] = 14 c[2][2] = 11
*/
*/
</pre>
</source>


[[Category:C source code examples]]
[[Category:C source code examples]]
[[Category:Programming]]

Latest revision as of 06:24, 13 June 2009

<source lang="c">/* Program to implement matrix multiplication. */ /* Performed by Milan. */ /* Edited by Gergo: resolved some portability issues */

  1. include <stdio.h>
  2. include <stdlib.h>

int main(void) {

       int a[10][10], b[10][10], c[10][10];
       int i, j, m, n, x, y, k;
       /* Entering values for matrix a...*/
       printf ("\n Enter the rows and columns of a: ");
       scanf ("%d%d", &m, &n);
       for (i = 0; i < m; i++)
       {
               for (j = 0; j < n; j++)
               {
                       printf (" a[%d][%d] = ", (i+1), (j+1));
                       scanf ("%d", &a[i][j]);
               }
               printf ("\n");
       }
       /* Entering values for matrix b... */
       printf ("\n Enter the rows and columns of b: ");
       scanf ("%d%d", &x, &y);
       for (i = 0; i < x; i++)
       {
               for (j = 0; j < y; j++)
               {
                       printf (" b[%d][%d] = ", (i+1), (j+1));
                       scanf ("%d", &b[i][j]);
               }
               printf ("\n");
       }
       /* Calculations */
       if ( n != x)
       {
               printf ("\n Error!! Rows of a don't match with columns of b.");
               printf ("\n Multiplication Impossible.");
               exit(EXIT_FAILURE);
       }
       else
       {
               for (i = 0; i < m; i++)
               {
                       for (j = 0; j < y; j++)
                       {
                               c[i][j] = 0;
                               for (k = 0; k < n; k++)
                               {
                                       c[i][j]+=a[i][k]*b[k][j];
                               }
                       }
               }
       }
       /* Output */
       printf ("\n The resultant c is:\n");
       for (i = 0; i < m; i++)
       {
               for (j = 0; j < y; j++)
               {
                       printf (" c[%d][%d] = %d", (i+1), (j+1), c[i][j]);
               }
               printf ("\n");
       }
       return 0;

} /* Output:

Enter the rows and columns of a: 2 2
a[1][1] = 1
a[1][2] = 2
a[2][1] = 3
a[2][2] = 4


Enter the rows and columns of b: 2 2
b[1][1] = 2
b[1][2] = 1
b[2][1] = 2
b[2][2] = 2


The resultant c is:
c[1][1] = 6 c[1][2] = 5
c[2][1] = 14 c[2][2] = 11
  • /

</source>