欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-26 09:15:25
#include #include #include #include void main() { int a[4][4]; int b[4][4]; int c[4][4]; int i,j,k,s,t,m,n; srand ......
#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;
                                }
                            }                                                    
                             }