【问题标题】:Given three nxn arrays, produce the output A+B=C where A, B and C are represented as matrices in the output using C programming language给定三个 nxn 数组,生成输出 A+B=C,其中 A、B 和 C 在使用 C 编程语言的输出中表示为矩阵
【发布时间】:2020-07-20 00:11:20
【问题描述】:

假设矩阵的形式为,

使用 C 语言在标准输出上所需的输出应该是

我的代码:

    #include<stdio.h>

    int main(void)
    {
     int size, i=0, j=0;
     printf("Enter the size of the array:");
     scanf("%d",&size);
     int A[size][size], B[size][size], C[size][size];   

//Entering the values in A[][]

     printf("enter the elements of Array A:\n");
     for(int i=0; i<size; i++)
         for(int j=0; j<size; j++)
           scanf("%d",&A[i][j]);

//Entering the values in B[][]

     printf("enter the elements of Array B:\n");
     for(int i=0; i<size; i++)
         for(int j=0; j<size; j++)
           scanf("%d",&B[i][j]);

// Calculating C[][]=A[][]+B[][]

     for(int i=0; i<size; i++)
         for(int j=0; j<size; j++)
           C[i][j]=A[i][j]+B[i][j]; 

     i=0;
     j=0;

     while(i<size)
     {
        if( i==size/2)
          printf("%*c%*c\n\n",6*size+1,'+',13*(size-1),'=');

         while(j<size)
           printf("%d\t",A[i][j++]);
         j=0;

         while(j<size)
           printf("%d\t",B[i][j++]);
         j=0; 

         while(j<size)
           printf("%d\t",C[i][j++]);
         j=0;

         printf("\n\n");
         i++;
    }
}

但此代码仅在 size=2 时给出所需的输出。

但我需要一个适用于用户输入的所有 size 值的解决方案。

【问题讨论】:

  • 你应该举一个例子说明为什么它是错误的
  • @MadPhysicist .. 没有 size=2 的输出是正确的。但我想要一个更通用的代码,我从你们那里得到了帮助。在下面检查我的答案..我纠正了我的代码,它给出了预期的结果。

标签: c arrays algorithm matrix logic


【解决方案1】:

这段代码将解决我的问题,

int size_odd_or_even=size%2;
    int check=1;

    while(i<size)
   {

       if( size_odd_or_even==0 && i==size/2 && check==1)
        {
          printf("\n%*c%*c\n",8*size-4,'+',8*size,'=');
          check=0;
          continue;
        }


       if( size_odd_or_even==1 && i== (int)(size/2) )
         {
               while( j<size-1)
                printf("%d\t",A[i][j++]);
               printf("%d   +   ",A[i][j]);
         }
        else
         {
           while( j<size)
            printf("%d\t",A[i][j++]);
         }
        j=0;


        if( size_odd_or_even==1 && i== (int)(size/2))
         {
               while( j<size-1)
                printf("%d\t",B[i][j++]);
               printf("%d   =   ",B[i][j]);
         }
        else
         {
           while(j<size)
          printf("%d\t",B[i][j++]);
         }
        j=0;


        while(j<size)
            printf("%d\t",Sum[i][j++]);
        j=0;

        if( size_odd_or_even==0 && !(i==size/2-1) )
          printf("\n\n");
        else if(size_odd_or_even==1)
          printf("\n\n");

        i++;
   }

这段代码分别给出了size=4size=5 的输出,


如果此代码可以优化,请告诉。

【讨论】:

    【解决方案2】:

    您使用标签(8 列)转到下一列,所以替换

    printf("%*c%*c\n\n",6*size+1,'+',13*(size-1),'=');
    

     printf("%*c%*c\n\n",8*size-4,'+',8*size,'=');
    

    示例:

    当然假设所有数字最多需要 7 列

    如果您希望在每行之间始终有 1 行带数字,请将 while 修改为:

      while(i<size)
      {
        if( i==size/2)
          printf("%*c%*c\n",8*size-4,'+',8*size,'=');
        else
          putchar('\n');
    
        while(j<size)
          printf("%d\t",A[i][j++]);
        j=0;
    
        while(j<size)
          printf("%d\t",B[i][j++]);
        j=0; 
    
        while(j<size)
          printf("%d\t",C[i][j++]);
        j=0;
    
        putchar('\n');
        i++;
      }
    

    生产:

    除此之外,我鼓励您检查 scanf 始终返回 1 否则您没有检测到无效输入

    【讨论】:

    • 它仍然没有给出想要的结果。 '+' 和 '=' 符号没有完全落在正确的位置。另外我猜如果矩阵的大小是偶数,则符号落在两行之间,而如果大小是奇数,则 =n 符号“+”和“=”必须沿中间行打印。希望你明白我的意思。
    • @PrateekTewary 我删除了答案中的示例,因为 S.O.不尊重空间,但有效
    • 顺便说一句,什么是 S.O. ??您的代码现在可以正常工作,但仍然无法在矩阵大小为奇数时将符号放在行之间。
    • @PrateekTewary S.O.是 StackOverflow ;-) 我不明白你关于大小何时奇怪的评论。我和你一样添加了图片
    • @PrateekTewary 终于可能我明白了,我编辑了我的答案,看看它
    猜你喜欢
    • 1970-01-01
    • 2021-10-04
    • 2011-05-30
    • 2015-06-12
    • 2017-01-05
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    相关资源
    最近更新 更多