CS 241 Data Organization using C Project 1: Magic Squares Fall 2013 1 Magic Squares A magic square is an arrangement of numbers in a square grid, where the numbers in each row, in each column, and each major diagonal is a constant. For project, we will be dealing with a Magic Square of Order 3, that is, a 3x3 matrix of unique positive integers such that the sum of each row, column, and major diagonal is a constant. We will restrict the values in the 3x3 matrix to two digits (adding a leading zero for single digit values). We’ll call the 18 digits from the rows of the matrix a magic number. 12 27 6 → 45 9 15 21 → 45 24 3 18 → 45 . ↓ ↓ ↓ & 45 45 45 45 45 Nonconsecutive Magic Square of Order 3 18 digit Magic Number: 122706091521240318 2 Requirements Write a C program that reads from the standard input stream a list of 18 digit numbers with ‘?’ characters replacing some of the digits. • If an 18-digit Magic Number exists that uses the given digits in the given order, then find it, and print it. Specifically, echo the input, print “->”, and print the number found. • If not, echo the input and print “No Solution”. • If the input does not follow the specified format, then echo the input record and print “Error”. 1 2.1 Input format • A record is a sequence of characters followed by the newline character: ‘\n’. • The input will consist of some number of records (lines). • Each valid record will consist of 18 characters. • The only valid characters in a record are the ten digits (0-9) and ‘?’. • Either a full two-digit pair will be missing “??” or fully given. For example, “?2270609??212403??” is an error because part of one pair is given. 2.1.1 Example Error Cases Example ?2270609??212403?? Error 1122334455667?8899 Error 0409020307?????? Error ??270609??212403??24 Error ??270609??212403??2 Error ??270609x3212403?? Error 12??????????????12 Error ??270609??210303?? Error 2.1.2 Comment Unmatched ? Unmatched ? < 18 characters > 18 characters > 18 characters bad char ‘x’ 12 not unique 03 not unique Example Non-Error Cases Example 060702010509??0304 -> 060702010509080304 06??02010509??0304 -> 060702010509080304 0409020305???????? -> 040902030507080106 ??270609??212403?? -> 122706091521240318 15??09??182427???? -> 153009121824270621 54??364563????2772 -> 549936456381902772 ??23????111720??14 No Solution ??331215??273009?? -> 183312152127300924 ??50??32??44??26?? -> 355029323844472641 3 Comment very easy easy easy No sum given No sum given with 4 unknowns No sum given 5 unknowns; can take up to 5 minutes Turning in your assignment Attach your program file to the Magic Square assignment in UNM Learn. 2 4 Grading Rubric 20 Points: Adheres to CS-241 coding standard. This includes indenting, comments, dead code elimination, not using goto, . . . 54 Points: Three points for each passed test record of: magicNumber.in using: ./yourprogram < magicNumber.in > youroutputfile /usr/bin/diff youroutputfile magicNumber.out 28 Points: +30 Points extra credit two points for each passed test record of an unknown set. No valid record will have more than 5 unknowns. if you can find magic numbers with 5 unknowns in less than 30 seconds (when run on moons.cs.unm.edu) 3