Professional Documents
Culture Documents
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
4633775
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
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
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
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
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
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
4633775
11/13
4633775
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
4633775
13/13