Program verify_hetero.c  to verify a heterosquare. Solution.ZIP

Program verify_hetero.c to verify a heterosquare. Solution

A heterosquare is a square matrix of size n x n with positive numbers from 1… n2 arranged such that the numbers in any line (horizontal, vertical, and both main diagonals) sum to differentvalues. You can read more about it in this reference (Links to an external site.)Links to an external site..

For this assignment you will be writing a programs verify_hetero.c  to verify a heterosquare.

The program verify_hetero.c will be run as follows:

./verify_hetero <filename
Where <filename is the name of the file that contains the square that needs to be verified. The format of the file will be as follows:

·         The first line will be a positive integer n for the number of rows/columns of the square. So the dimensions of the square are n x n.

·         Every line after that represents a row in the magic square, starting with the first row. There will be n such lines where each line has n numbers(columns) separated by commas.

So for instance, a file containing a heterosquare of size 3 will have this format:


The program should read and parse the input file to construct a square using the struct provided in the skeleton code. You will need to dynamically allocate memory for the 2D array.

To verify the square is indeed a heterosquare you need to total up the lines for each row, each column, and the two main diagonals (top-left to bottom-right, and top-right to bottom-left) and store those line totals in an array. Next, pass the array to the insertion sort function discussed in the next paragraph. Finally, check if the sorted array contains any duplicate values, and if so, it's not a heterosquare. The program should print true if the input file is a heterosquare, and false otherwise (keywords like right/wrong, valid/not valid, etc. should not be used).

The program implements insertion sort to put the line totals (integers) in ascending order, which will be used for verification of the heterosquare. Insertion sort performs in-place sorting on an array of integers. Using the array of unsorted integers, it iterates through that array growing the sorted list as each unsorted item is inserted into the sorted part of the array. Initially, the first integer in the array (at index 0) is in the sorted part and the rest of the integers are in the unsorted part. The integer at index 1 is then inserted into the sorted part by finding its where it belongs in the sorted list, shifting any larger values in the sorted list each one to the right to make room, and then assigning the integer being sorted to its element. This approach is repeated for all remaining integers in the unsorted part.

The sample run below shows the expected behavior of the program:

$ ./verify_hetero
Usage: ./verify_hetero <filename
$ cat hetero-4.txt
$ ./verify_hetero hetero-4.txt
$ cat not-hetero-3.txt
$ ./verify_hetero not-hetero-3.txt

 The following assumptions can be made regarding the input file:

·          The input file contains distinct integers from 1 to n2

·         The maximum number of characters possible in each line of input file is 100

Powered by