You are on page 1of 13

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 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.
8

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:

1/13

Jason Chiang Wann Chun


4633775
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:
3
8 1 6
3 5 7
4 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 <iostream>
#include <fstream>
#include "magicsquare.h"
using namespace std;
int main()
{
int option;
2/13

Jason Chiang Wann Chun


do
{
cout <<"*************************************" << endl;
cout <<"\tMagic Square " << endl;
cout << "*************************************" << endl;
cout << " 1 - Generate ( using odd number )" << endl;
cout << " 2 - Check condition " << endl;
cout << " 3 - Print Magic Square " << endl;
cout << " 4 - Quit " << endl;
cout << "************************************" << endl;
cout << "Enter an option : ";
cin >> option;
while ( !option || ( option < 1 || option > 4 ))
{
cin.clear();
cin.ignore ( 1000, '\n');
cout << "Invalid input, Only choose the option that available " << endl;
cout << "Re-enter the option " << endl;
cout << "Option : ";
cin >> option;
}
if ( option == 1 )
{
generate_ms(); // generate magic square
}
else if ( option == 2 )
{
check_ms(); // check magic square
}
else if ( option == 3 )
{
print_ms(); // print magic square
}
else if ( option == 4 )
{
cout << "\nProgram Close" << endl;
cout << "Thx for using ^^ " << endl;
cout << "Have a nice day " << endl;
}
}while ( option != 4);
return 0;
}
Magicsquare.h
//Jason Chiang Wann Chun 4633775
// Assignment 3
3/13

4633775

Jason Chiang Wann Chun


#include <iostream>
using namespace std;

4633775

void generate_ms();
void check_ms();
void print_ms();
main_magicsquare.cpp
//Jason Chiang Wann Chun 4633775
// Assignment 3
#include <iostream>
#include <fstream>
#include <cctype>
#include "magicsquare.h"
using namespace std;
void generate_ms()
{
int num, total_row_col;
int row, newrol, col, newcol;
char filename[100];
char condition;
ofstream outfile;
cout << "\nEnter a odd number value to generate magic square ( * only odd number )" << endl;
cout << "Num : ";
cin >> num;
cout <<"\n" << endl;
while ( !num || num%2 == 0 )
{
cin.clear();
cin.ignore ( 1000, '\n');
cout << " Invalid input " << endl;
cout << " Only can enter an odd number value " << endl;
cout << " Num : ";
cin >> num;
}
int **ms_array = new int*[num]; //Magic square in 2 dimensional array
for ( int i = 0; i < num; i++ )
{
ms_array[i] = new int[num];
}
for ( int i = 0; i < num; i++ )
{
for ( int j = 0; j < num; j++ )
{
ms_array[i][j] = 0;
4/13

Jason Chiang Wann Chun


}
}

4633775

row = 0;
col = num / 2;
total_row_col = num * num;
ms_array[row][col] = 1;
int rows; // a temporary location for row
for ( rows = 2; rows < total_row_col + 1; rows++ )
{
if ( row - 1 < 0)
{
row = num - 1;
}
else
{
row --;
}
if ( col + 1 == num )
{
col = 0;
}
else
{
col ++;
}
// Check for the condition of the magic square
if ( ms_array[row][col] != 0 )
{
row = newrol;
col = newcol;
if ( row + 1 == num )
{
row = 0;
}
else
{
row ++;
ms_array[row][col] = rows;
}
}
else
5/13

Jason Chiang Wann Chun


{
ms_array[row][col] = rows;
}

4633775

newrol = row;
newcol = col;
}
for ( int i = 0; i <num; i++ )
{
for ( int j = 0; j < num; j++ )
{
cout << ms_array[i][j] << "\t";
}
cout << "\n" << endl;
}
cout << "Condition save ? ( Y / N ) " << endl;
cout << "Condition : ";
cin >>condition;
condition = toupper(condition);
while ( !condition || ( condition != 'Y' && condition != 'N'))
{
cin.clear();
cin.ignore ( 1000, '\n');
cout << "Invalid input " << endl;
cout << "Only enter ( Y ) for yes and ( N ) for no " << endl;
cout << "Condition : ";
cin >> condition;
condition = toupper(condition);
}
if ( condition == 'Y')
{
cout << "\nEnter a file name to save the generated magic square"<< endl;
cout << "File name : ";
cin >> filename;
outfile.open ( filename, ios::out);
if ( outfile.good())
{
outfile << num << endl;
for ( int i = 0; i < num; i++ )
{
for ( int j = 0; j < num; j++ )
{
outfile << ms_array[i][j] << "\t" << endl;
}
6/13

Jason Chiang Wann Chun


outfile << "\n";
}

4633775

outfile.close();
cout << "\n***File saved***\n" << endl;
}
}
else
{
cout <<"File not saved" << endl;
cout <<"Jumping back to menu selection " << endl;
cout << "\n" << endl;
}
for ( int i = 0; i < num; i++ )
{
delete[]ms_array[i]; // function execute when the magic square is not save
}
delete[]ms_array;
}
void check_ms()
{
int num;
int total_row;
int sum;
char filename[100];
bool condition = true;
ifstream infile;
cout << "\nEnter a file name " << endl;
cout << "File name : ";
cin >> filename;
infile.open (filename, ios::in);
if ( infile )
{
infile >> num;
int **ms_array = new int*[num];
for ( int i = 0; i < num; i++ )
{
ms_array[i] = new int[num];
}
for ( int i = 0; i < num; i++ )
7/13

Jason Chiang Wann Chun


{
for ( int j = 0; j < num; j++ )
{
infile >> ms_array[i][j];
}
}

4633775

total_row = 0;
for ( int row = 0; row < num; row++ )
{
total_row += ms_array[0][row];
}
int sum_cols;
for ( sum_cols = 1; sum_cols < num; sum_cols++ )
{
sum = 0;
for ( int row = 0; row < num; row++ )
{
sum += ms_array[sum_cols][row];
}
if ( sum != total_row )
{
condition = false;
}
}
int sum_rols;
for ( sum_rols = 0; sum_rols < num; sum_rols++ )
{
sum = 0;
for ( int row = 0; row < num; row ++)
{
sum += ms_array[row][sum_rols];
}
if ( sum != total_row)
{
condition = false;
}
}
sum = 0;
for ( int row = 0; row < num; row ++ )
{
sum += ms_array[row][row];
}
8/13

Jason Chiang Wann Chun


if ( sum != total_row )
{
condition = false;
}

4633775

sum = 0;
for ( int row = 0; row < num; row ++ )
{
sum += ms_array[row][num - row - 1];
}
if ( sum != total_row )
{
condition = false;
}
if ( condition == true)
{
cout << "\n***Magic square completed***" << endl;
}
else
{
cout << "\n***Magic square uncompleted***" << endl;
}
cout << "\n" << endl;
for ( int i = 0; i < num; i++ )
{
delete[]ms_array;
}
delete[]ms_array;
}
else
{
cout <<"File is not exist"<< endl;
cout <<"Jumping back to menu " << endl;
cout <<"\n"<< endl;
}
infile.close();
}
void print_ms()
{
int num;
char filename[100];
ifstream infile;
9/13

Jason Chiang Wann Chun

4633775

cout << "Enter a file name" << endl;


cout << " File name : ";
cin>>filename;
infile.open ( filename, ios::in);
if ( infile )
{
infile >> num;
int **ms_array = new int*[num];
for ( int i = 0; i < num; i++ )
{
ms_array[i] = new int[num];
}
for ( int i = 0; i < num; i++ )
{
for ( int j = 0; j < num; j++ )
{
infile >> ms_array[i][j];
}
}
cout <<"\n" << endl;
cout <<"
^Magic Square^ " << endl;
cout <<"\n"<<endl;
for ( int i = 0; i < num; i++ )
{
for ( int j = 0; j < num; j++ )
{
cout << ms_array[i][j] << "\t";
}
cout << "\n";
}
cout << "\n";
for ( int i = 0; i < num; i++ )
{
delete[]ms_array[i];
}
delete[]ms_array;
}
else
{
cout << "File is not exist " << endl;
10/13

Jason Chiang Wann Chun


cout << "Jumping back to menu selection " << endl;
}
infile.close();
}

11/13

4633775

Jason Chiang Wann Chun

4633775

Conclusion
During this assignment, Im having a problem to generate 2 Dimensional array , along with the pointer as
well. This is a good training and Ive learned some new way to execute program.

Assessment Criteria
Assessment Criteria
Correctness
Coding
Input Validation
Readability and Documentation
Output (clear and well formatted)
Conclusion
Total

Marks Allocated
5
10
4
3
1
2
25

Submission:
12/13

Jason Chiang Wann Chun

4633775

You are to submit:


1. A documentation containing the following:
a. The standard cover page
b. The source code listing together with sample input/ouput
c. A conclusion stated what you have learned from this assignment
2. The softcopy of the project containing all files (.cpp and .h) submit to Moodle.
The completed Assignment must be submitted latest by Thursday, 22nd May 2014, 5:00 pm.
Late submissions will be marked with a 25% deduction for each day.
Submissions more than three days late will not be marked, unless an extension has been granted.

13/13

You might also like