Alexander Chen's Blog

Yes, we can!

“Your time is limited, so don’t waste it living someone else’s life. Don’t be trapped by dogma – which is living with the results of other people’s thinking. Don’t let the noise of other’s opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition. They somehow already know what you truly want to become. Everything else is secondary.”
By Steve Jobs

一个矩阵相乘的程序

Posted on:  March 5, 2009  |   Category: Computer
Comments:  No Comments
Print Friendly

前些天应一个朋友的要求,帮忙弄一个矩阵相乘的程序,在这里发出来,以便有需要的人拿去做参考。
其实这个程序很菜,用的方法也很笨拙,主要的目的只是为了让它从字面上能更好的被理解,大家见笑。

#include “stdio.h”

int main(void)
{
     int m,s,n; //matrix’s columns and rows
     float a[50][50],b[50][50],c[50][50]; //arrays to store your matrixes.
    
     int i,j,k; //loop control.
    
     for (i=0;i<50;i++)
         for (j=0;j<50;j++)
            a[i][j]=b[i][j]=c[i][j]=0.0; //initialize

    
     printf(“Input m:\n”);
     scanf(“%d”,&m);
     printf(“Input s:\n”);
     scanf(“%d”,&s);
     printf(“Input n:\n”);
     scanf(“%d”,&n);
    
     printf(“Input matrix 1:\n”);
    
     for (i=0;i<m;i++)
         for (j=0;j<s;j++)
             scanf(“%f”,&a[i][j]);
    
     printf(“Input matrix 2:\n”);
    
     for (i=0;i<s;i++)
         for (j=0;j<n;j++)
             scanf(“%f”,&b[i][j]);
            
     for (i=0;i<m;i++)
         for (j=0;j<n;j++)
             for (k=0;k<s;k++)
                 c[i][j]+=a[i][k]*b[k][j]; //cacluate the matrix C.

     for (i=0;i<m;i++)
     {   
         for (j=0;j<n;j++)
             printf(“%6.2f”,c[i][j]);
         printf(“\n”);
     }

} 

关键字: 矩阵相乘c语言,矩阵相乘c语言程序,用c语言实现矩阵相乘,矩阵运算c程序

Comments

Leave a Comment




Google Custom Search


About

Welcome to Alexander Chen's Blog. This blog is about a little bit everything, but mainly focuses on my thoughts, and some computer and smart device tips.

Any content here is welcomed to be used or referenced, but before you use it freely, please follow THIS agreement.

This blog is optimized for MOBILE DEVICES.

Contact Me


Function