[Solved] Programs verify_hetero.c to verify a heterosquare

$25

File Name: Programs_verify_hetero.c_to_verify_a_heterosquare.zip
File Size: 461.58 KB

SKU: [Solved] Programs verify_hetero.c to verify a heterosquare Category: Tag:
5/5 - (1 vote)

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 nfor 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 nsuch 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:

39,8,72,1,63,4,5

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, its 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_heteroUsage: ./verify_hetero <filename>$ cat hetero-4.txt41,2,3,45,6,7,89,10,11,1213,14,16,15$ ./verify_hetero hetero-4.txttrue$ cat not-hetero-3.txt34,3,89,1,52,7,6$ ./verify_hetero not-hetero-3.txtfalseThe 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

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] Programs verify_hetero.c to verify a heterosquare
$25