Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need answer in C language.. redit Problem Description of Problem - magic square

ID: 3715473 • Letter: N

Question

need answer in C language..

redit Problem Description of Problem - magic square is a 2-dimensional array of positive integers which has the following characteristics: . The numbers of rows equals the number of columns . All entries must be positive integers e The sum of all the entries in each row, each column, and the two diagonals are identical In a 2-dimensional array, one diagonal goes from the upper left corner to the lower right corner. The other diagonal goes from the lower left corner to the upper right corner. See the diagram on the following page. Task to perform 1. Populate the array displayed on the following page. 2. Do the Sums for all columns, rows, and diagonals. There will be 8 sums altogether 3. Display all the results. Make sure to identify where the sum was located. Example "Sum of 1st row" 4. Multiply the 2nd column of numbers by 4 5. Subtract 3 from the center number 6. Add 3 to the last number in the last row 7. Do the Sums again for all columns, rows, and diagonals 8. Display all the results again 9. Draw out the results of the Magic Square 10. Answer the following question. Is the sum of the 2nd column greater than, less than, or equal to the sum of the top left to bottom right diagonal?

Explanation / Answer


#include <conio.h>
#include <stdio.h>

void main()
{   int r1=0,r2=0,r3=0,c1=0,c2=0,c3=0,dia1=0,dia2=0,n=3,i,j;
    int a[3][3]={{8,3,4},{1,5,9},{6,7,2}}; //solution to first part a stores magicsquare
  
  
    for(i=0;i<n;i++) //loop to find the sums
    {
        r1=r1+a[i][0];    
        r2=r2+a[i][1];
        r3=r3+a[i][2];
        c1=c1+a[0][i];
        c2=c2+a[1][i];
        c3=c3+a[2][i];
        dia1=dia1+a[i][2-i];
        dia2=dia2+a[2-i][i];
      
    }

//results after sum
  
    printf("The sum of 1st ROW is: %d ",r1);
    printf("The sum of 2nd ROW is: %d ",r2);
    printf("The sum of 3rd ROW is: %d ",r3);
    printf("The sum of 1st COLUMN is: %d ",c1);
    printf("The sum of 2nd COLUMN is: %d ",c2);
    printf("The sum of 3rd COLUMN is: %d ",c3);
    printf("The sum of the first diagonal is : %d ",dia1);
    printf("The sum of the Second diagonal is : %d ",dia2);
  
    for(i=0;i<3;i++)
    {
        a[i][1]=a[i][1]*4;      //multiply 2nd column by 4 array starts with 0 index
    }
  
    a[n/2][n/2]-=3;     //substract 3 from center
    a[n][n]=a[n][n]+3;    //add 3 to last element of last row

    //repeat the sum process
for(i=0;i<n;i++) //loop to find the sums
    {
        r1=r1+a[i][0];    
        r2=r2+a[i][1];
        r3=r3+a[i][2];
        c1=c1+a[0][i];
        c2=c2+a[1][i];
        c3=c3+a[2][i];
        dia1=dia1+a[i][2-i];
        dia2=dia2+a[2-i][i];
      
    }
//results after modification
        printf("The sum of 1st ROW is: %d ",r1);
    printf("The sum of 2nd ROW is: %d ",r2);
    printf("The sum of 3rd ROW is: %d ",r3);
    printf("The sum of 1st COLUMN is: %d ",c1);
    printf("The sum of 2nd COLUMN is: %d ",c2);
    printf("The sum of 3rd COLUMN is: %d ",c3);
    printf("The sum of the first diagonal is : %d ",dia1);
    printf("The sum of the Second diagonal is : %d ",dia2);

    printf(" THE MAGIC CUBE ");
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf(" %d",a[i][j] );
        }
        printf(" ");
    }

printf("THE SUM OF 2nd column and diagonal are equal.");
}