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

Write a program that asks the user to enter the numbers 1 to 16 (in any order) a

ID: 3665355 • Letter: W

Question

Write a program that asks the user to enter the numbers 1 to 16 (in any order) and then displays the numbers in a 4 x 4 aligned arrangement, followed by the sums of the rows, columns, and diagonals. A session of the program will look like

Enter the numbers from 1 to 16 in any order:

16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1

The 4 x 4 arrangement is

16 3 2 13

5 10 11 8

9 6 7 12

4 15 14 1

Row sums: 34 34 34 34

Column sums 34 34 34 34

Diagonal sums: 34 34

Note: If the row, column, and diagonal sums are all the same, the numbers are said to form a magic square. Hint: use setw function to specify field width. Look online to find the library that is required for this function.

Explanation / Answer

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

void main()
{
   int a[4][4], i, j, r[4],c[4],d1,d2,r1=0,k=0;

   cout<<"Enter the numbers from 1 to 16 in any order:";

   for(i=0;i<4;i++)
   {
       for(j=0;j<4;j++)
       {
           cin>>a[i][j];
           r1=r1+a[i][j];
       }
       r[k]=r1;
       r1=0;
       k++;
   }

   for(i=0;i<4;i++)
   {
       for(j=0;j<4;j++)
       {
           cout<<a[i][j]<<setw(4);
       }
       cout<<endl;
   }

   r1=0;
   k=0;
   for(i=0;i<4;i++)
   {
       for(j=0;j<4;j++)
       {

           r1=r1+a[j][i];
       }
       c[k]=r1;
       r1=0;
       k++;
   }

   cout<<"Row sums ";

   for(i=0;i<4;i++)
   {
       cout<<r[i]<<" ";
   }

   cout<<" Column sums ";

   for(i=0;i<4;i++)
   {
       cout<<c[i]<<" ";
   }

   getch();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote