summaryrefslogtreecommitdiff
path: root/matmult.c
blob: d1b2f2c86b23fc504dc22092c7660a7a5c25a352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*  File: matmult.c
    Multiplication of two matricies*/
#include <stdio.h>

#define M 10
#define N 20
#define P 30

int main()
{
    double a[M][N];
    double b[N][P];
    double c[M][P];
    int i;
    int j;
    int k;

    int pop1,pop2;
    for(pop1=0;pop1<M;pop1++)
    {
        for(pop2=0;pop2<N;pop2++)
        {
            a[pop1][pop2] = pop1+pop2;
        }   
    }
    
    for(pop1=0;pop1<M;pop1++)
    {
        for(pop2=0;pop2<P;pop2++)
        {
            b[pop1][pop2] = pop1*pop2;
        }
    }

    for(i=0;i<M;i++)
    {
        for(j=0;j<P;j++)
        {
            c[i][j] = 0;
            for(k=0;k<N;k++)
            {
                c[i][j] += a[i][k]*b[k][j];
            }
        }
    }
    
    printf("Matrix a is\n");
    int l,m;
    
    for(l = 0;l<M;l++)
    {
        for(m = 0; m<N;m++)
        {
            printf("%3i ",(int)a[l][m]);
        }
        printf("\n");
    }
    
    printf("\nMatrix b is\n");
    for(l = 0;l<M;l++)
    {
        for(m = 0; m<N;m++)
        {
            printf("%3i ",(int)b[l][m]);
        }
        printf("\n");
    }
    
    printf("\nMatrix c is\n");
    for(l = 0;l<M;l++)
    {
        for(m = 0; m<N;m++)
        {
            printf("%5i ",(int)c[l][m]);
        }
        printf("\n");
    }
    
    return 0;
}