码迷,mamicode.com
首页 > 编程语言 > 详细

c语言:矩阵相乘-矩阵相加 新手练习1

时间:2018-09-08 22:31:34      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:++   string   rand   矩阵相乘   div   code   print   pac   include   

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
void main()
{
    int a[4][4];
    int b[4][4];
    int c[4][4];
    int i,j,k,s,t,m,n;
    srand(time(0));
    for(i=0;i<=3;i++)
        for(j=0;j<=3;j++)
            a[i][j]=rand()%20+1;
                printf("a is \n");
                for(i=0;i<=3;i++)
                {
                 
                    for(j=0;j<=3;j++)
                         
                         
                            printf("%5d",a[i][j]);
                                printf("\n");
                                    printf("\n");
            }
                         
                            for(i=0;i<=3;i++)
                            for(j=0;j<=3;j++)
                                b[i][j]=rand()%20+1;
                                printf("b is \n");
                                            for(i=0;i<=3;i++)
                                            {
                                             
                                            for(j=0;j<=3;j++)
                                             
                                                 
                                                printf("%5d",b[i][j]);
                                                    printf("\n");
                                                        printf("\n");
                                            }
                                                for(i=0;i<=3;i++)
                                                    for(j=0;j<=3;j++)
                                                        c[i][j]=a[i][j]+b[i][j];
                                                            printf("c is \n");
                                                             
                                                                for(i=0;i<=3;i++)
                                            {
                                             
                                            for(j=0;j<=3;j++)
                                             
                                                 
                                                printf("%5d",c[i][j]);
                                                    printf("\n");
                                                        printf("\n");
                                            }
                                            int z;
                                            z=0;
                                    printf("a is \n");
                                        for(i=0;i<=3;i++)
                {
                 
                    for(j=0;j<=3;j++)
                         
                         
                            printf("%5d",a[i][j]);
                                printf("\n");
                                    printf("\n");
            }
 
                            int my=0;
                            int sb=0;
                            k=0;
                            int jsq=0;
                                                            printf("my is : \n");
                                    for(k=0;k<=3;k++)
                                    {
                                        for(i=0;i<=3;i++)
                                    {
                                     
                                    for(j=0;j<=3;j++)
                                    {
                                 
                                    sb=a[k][j]*b[j][i];
                                    my=my+sb;
                                     
                                    }
                                    c[k][i]=my;
         
                                    printf("%5d",c[k][i]);
                                    jsq++;
                                    if(jsq%4==0)
                                    printf("\n");
                                    my=0;
                                }
                            }                                                    
                             }

c语言:矩阵相乘-矩阵相加 新手练习1

标签:++   string   rand   矩阵相乘   div   code   print   pac   include   

原文地址:https://www.cnblogs.com/jx3ajmd/p/9610398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!