documentuu

16
Jason Chiang Wann Chun 4633775 IICS FACULTY OF INFORMATICS University of Wollongong CSCI124 Feb Session 2014 Assignment 3 (Individual Work – 6% of subject marks) Background: The assignment examines a student’s knowledge of dynamics, manipulating arrays, file input/ output, and program design. Remember that: 1. All programs should be able to run on the lab’s computers. 2. You must put the following information on the header of each text and source file you will be submitting in this assignment: Student’s full name: Student’s ID: Modification Date: Purpose of this file (or program): 3. Assignments that are not able to be compiled will result in zero mark given to the assignment. 4. You must only use the C++ features that have already been covered in the lectures Problem Specification: A magic square is a square of numbers with N rows and N columns in which each of the integer values from 1 to (N * N) appears exactly once and the sum of each column, each row, and each diagonal is the same value. The following shows an example of a magic square. 1/16 8 1 6 3 5 7 4 9 2

Upload: jason-chiang

Post on 06-Nov-2015

214 views

Category:

Documents


1 download

DESCRIPTION

y

TRANSCRIPT

Jason Chiang Wann Chun4633775IICSFACULTY OF INFORMATICSUniversity of WollongongCSCI124 Feb Session 2014Assignment 3 (Individual Work 6% of subject marks)

Background:The assignment examines a students knowledge of dynamics, manipulating arrays, file input/ output, and program design.

Remember that:1.All programs should be able to run on the labs computers.2. You must put the following information on the header of each text and source file you will be submitting in this assignment: Students full name: Students ID: Modification Date: Purpose of this file (or program): 3. Assignments that are not able to be compiled will result in zero mark given to the assignment.4.You must only use the C++ features that have already been covered in the lectures

Problem Specification:

A magic square is a square of numbers with N rows and N columns in which each of the integer values from 1 to (N * N) appears exactly once and the sum of each column, each row, and each diagonal is the same value. The following shows an example of a magic square.

618

753

294

A magic square can be implemented in a program using a two dimensional array. Your task is to write a program that provides the following options to the user:

1. Construct and display a magic square for any given odd number N. For this function, the program should prompt the user for the number N using standard in. Then generate a two-dimensional array using dynamic memory allocation and fill the elements of the array with the appropriate values so that it fulfills the criteria of a magic square as stated above.

The algorithm to construct a magic square is as follows:

Insert the value 1 in the middle of the first row (think about how to go about in getting the middle value from a row). After a value, x, has been placed, move up one row and to the right one column. Place the next number, x + 1, there, unless:

(i) You move off the top (row = -1) in any column, then move to the bottom row and place the next number, x + 1, in the bottom row of that column.(ii) You move off the right end (column = N) of a row, then place the next number, x + 1, in the first column of that row.(iii) You move to a position that is already filled or out of the upper right corner, then place the next number, x + 1, immediately below x.Stop when you have placed as many elements as there are in the array.

The program should then display the magic square. Before the user is allowed to choose another option, ask the user whether to save the newly generated magic square or not. If the user chooses to save it, prompt for a filename to write. The first line in the file should be the number N followed by the values for each rows and columns. A sample output file may look like follows:

38 1 63 5 74 9 2

2. Check whether a given input (read from a text file) is a magic square or not. In this case, the program should prompt for a filename (.txt) and read the values from the file to generate a square filled with the values read. Then perform a checking on the square and display a message indicating whether the square is a magic square or not. The input file should have the same format as the output file shown above. The square should be generated dynamically using the input from the file.

3. The last option is to print a given magic square.All options must be implemented by individual function. Identify and use appropriate parameters for each function. Remember to clean up all dynamically created memory before the program terminates. Make sure also you are careful in dealing with the array indices so that you will not go outside the bounds of the array.

Main.cpp

//Jason Chiang Wann Chun 4633775// Assignment 3#include #include #include "magicsquare.h"using namespace std;

int main(){ int option;

do { cout