A magic square is a 2D matrix of integers with the following

A magic square is a 2D matrix of integers with the following properties: The number of rows (n) = the number of columns The sum of the elements in each row, each column and each primary diagonal is equal to n(n2 + 1)/2. You are provided with an input file called input.txt in which each line represents the values in a 4 x 4 square to be tested. Write a C program that reads in a 4 x 4 square matrix from a line of the input file and outputs true if it is or false if the matrix is not a magic square.

Hint: You could write functions to test each condition – example checkRows, checkColumns, checkprimaryDiagonal1 and checkprimaryDiagonal2 2

Restrictions: 1. Your program must read the values form the testdata file and process the data one lines at a time

2. You program should handle square matrices of size 4 x 4 3. Your input file can have any number of lines of data; but the testdata file will always terminate with a line with the number 0.

Sample testdata.txt file

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

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

1 3 4 5 6 7 12 8 9 15 7 9 4 3 12 13

0

Sample Run Test Result ---------------------------------------

1 True

2 False

3 False

Solution

ANSWER:

#include <stdio.h>
#include <stdlib.h>

int main()
{

FILE *myFile;
myFile = fopen(\"file.txt\", \"r\");
   int line;
//read file into array
int numberArray[4][4];
int i, j;

if (myFile == NULL)
{
printf(\"Error Reading File\ \");
exit (0);
}
printf(\"Run Test Result \ \");
for(line = 0; line < 3 ; line++)
{
   for (i = 0; i < 4; i++)
   {  
       for (j = 0; j < 4; j++)
       {
           fscanf(myFile, \"%d,\", &numberArray[i][j] );
       }
   }
  
   if(checkRows(numberArray,4,34)) {
       if(checkColumns(numberArray,4,34)) {
           if(checkprimaryDiagonal1(numberArray,4,34)) {
               if(checkprimaryDiagonal2(numberArray,4,34)) {
                   printf(\" %d True\ \", line+1); }
               else {
                   printf(\" %d False\ \", line+1);
                   }
           } else {
                   printf(\" %d False\ \", line+1);
                   }
       } else {
               printf(\" %d False\ \", line+1);
               }
   } else {
           printf(\" %d False\ \", line+1);
           }
}

fclose(myFile);
   getchar();
return 0;
}

int checkRows(int arr[4][4], int size, int val)
{
   int row, col;
   int count=0, sum=0;
   for(row=0;row<size;row++)
   {
       sum=0;
       for(col=0;col<size;col++)
       {
           sum = sum + arr[row][col];
       }
       if(val == sum)
       count++;
   }
   if(count == size)
       return 1;
   else
       return 0;
}
int checkColumns(int arr[4][4], int size, int val)
{
   int row, col;
   int count=0, sum=0;
   for(row=0;row<size;row++)
   {
       sum=0;
       for(col=0;col<size;col++)
       {
           sum = sum + arr[col][row];
       }
       if(val == sum)
       count++;
   }
   if(count == size)
       return 1;
   else
       return 0;
}
int checkprimaryDiagonal1(int arr[4][4], int size, int val)
{
   int diagonal;
   int sum = 0;
   for(diagonal=0;diagonal<size;diagonal++)
       sum = sum + arr[diagonal][diagonal];
   if(sum == val)
       return 1;
   else
       return 0;
}
int checkprimaryDiagonal2(int arr[4][4], int size, int val)
{
   int row, col, skip, replace;
   int count=0, sum=0;
   for(row=0, skip=size-1 ;row<size;row++, skip--)
   {
       sum=0;
       for(col=0;col<size;col++)
       {
           replace = skip;
           if(replace == 0)
           {
               sum = sum + arr[row][col];
           }
           replace--;  
       }
       if(val == sum)
       count++;
   }
   if(sum == val)
       return 1;
   else
       return 0;
}

Explanation:

1.

A magic square is a 2D matrix of integers with the following properties: The number of rows (n) = the number of columns The sum of the elements in each row, eac
A magic square is a 2D matrix of integers with the following properties: The number of rows (n) = the number of columns The sum of the elements in each row, eac
A magic square is a 2D matrix of integers with the following properties: The number of rows (n) = the number of columns The sum of the elements in each row, eac

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site