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

耗尽温柔 提交于 2020-04-30 06:24:07

问题


Assume the matrices to be of the form,

The required output on stdout using C language should be

My code:

    #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++;
    }
}

But this code only gives the desired output when size=2.

But I need a solution which works for all values of size entered by the user.


回答1:


you use a tab (8 columns) to go to the next column, so replace

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

by

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

example :

of course that supposes all numbers need up to 7 columns

If you want to always have 1 line between each line with numbers modify the while to have :

  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++;
  }

producing :

Out of that I encourage you to check scanf always return 1 else you do not detect an invalid input




回答2:


This code will solve my question,

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++;
   }

This code gives the outputs for size=4 and size=5 respectively as,


If this code can be optimised then please do tell.



来源:https://stackoverflow.com/questions/61086171/given-three-nxn-arrays-produce-the-output-ab-c-where-a-b-and-c-are-represente

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!