The author has 1 post.
Font size: Small - 100% (Default)  Content converter: No conversion
 
Clicks Replies
907 0
【试题】矩阵乘法
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 1 Posted at: 4/16/16 20:45
#include <stdio.h>
#include <stdlib.h>

int **create2d(int row, int col)
{
    int i;
    int **pp = (int **)malloc(row * sizeof(int *));
    *pp = (int *)malloc(row * col * sizeof(int));
    for (i = 1; i < row; i++)
        pp[i] = pp[i - 1] + col;
    return pp;
}

void free2d(int **pp)
{
    free(*pp);
    free(pp);
}

int main()
{
    int m, s, n;
    int **a, **b;
    int i, j, v, p;
    
    scanf("%d%d%d", &m, &s, &n);
    a = create2d(m, s);
    b = create2d(s, n);
    
    for (i = 0; i < m; i++)
        for (j = 0; j < s; j++)
            scanf("%d", &a[i][j]);
    for (i = 0; i < s; i++)
        for (j = 0; j < n; j++)
            scanf("%d", &b[i][j]);
    
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            v = 0;
            for (p = 0; p < s; p++)
                v += a[i][p] * b[p][j];
            printf("%d ", v);
        }
        putchar('\n');
    }
    
    free2d(a);
    free2d(b);
    return 0;
}
Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
(Shortcut key: Ctrl+Enter)
Post Information
Clicks: 907 Replies: 0
Author: 巨大八爪鱼
Last reply: 巨大八爪鱼
Last reply time: 4/16/16 20:45
Announcements