Homework 3
User-defined functions: Creating functions with more than one return value
Due: Tuesday, November 14, 2006 at 11:59pm
Outcomes
After successfully completing this assignment, you will
be able to...
- Design and implement functions that return multiple values
- Use 2-dimensional arrays in a program
Before Starting
Read sections 6.9 and 7.1 - 7.4.
The Assignment
An n x n matrix of integers is called a magic square if all the rows,
columns, and diagonals sum to the same value, and each integer value from
1 to n-squared appears exactly once in the matrix. The 5 x 5 matrix shown below
is a magic square because each row, column, and diagonal sum to 65 and each
of the integers from 1 to 25 appears exactly once:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Write a C program that will ask the user to enter the values for a
square array of size 10 x 10 or smaller and will verify whether the values do,
in
fact, form a magic square.
The program should also print the
sum of one of
the rows if the square is a magic square.
Include files
- stdio.h provides printf, scanf
Assumptions and Restrictions
The program must include the following functions:
- InputSquare This function should ask the user for the size of the
square (the number of rows). The number of rows must be in the range 2 - 10.
The function should read in the values for the square,
row by row. It
returns the initialized square and the size of the square.
- IsMagic This function takes as input a square array and its size.
It returns 1 ( true) if the square forms a magic square, and
0 (false) otherwise.
It also returns the sum of a row of the square if the square is magic.
- OutputSquare Given a square array and its size, this function will
display the contents of the square row by row, formatted so that the integers
are right-justified.
Other functions may be included if you wish. All functions must be
commented with pre- and post-conditions.
Sample Execution
You will be asked to enter the values in a square
matrix. This program will determine if the square
is a magic square or not. In a magic square, the
sum of the integers is the same for every row, column,
and diagonal, and each integer appears only once.
How many rows in your square?
3
Enter all the values in row 1 separated by spaces
8 3 4
Enter all the values in row 2 separated by spaces
1 5 9
Enter all the values in row 3 separated by spaces
6 7 2
Here is your square
8 3 4
1 5 9
6 7 2
The square you entered IS a magic square
The sum of each row is 15
Deliverables
Submit your implementation file using the following
turnin command:
/cs/bin/turnin submit cs2301 hw3 hw3.c
Programs submitted after 11:59pm on November 14 will be tagged as late,
and will be subject to the late homework
policy.
Grading
This assignment will be graded on the following areas:
documentation (including pre- and post-conditions for all functions),
proper choice of parameters and local variables, proper choice of function
return types, adherence to specifications
for each function, correctness, and robustness (ability to recover from
invalid input).
Programs must compile successfully in order to receive points for correctness.