In: Computer Science
This assignment uses a combination of classes and arrays.
Instructions:
1) download the 3 program files into a new C++ project. NOTE: if your IDE does not allow you to create projects - or you are not sure how to do it - then you may combine the two cpp files into a single file.
2) Complete the program by adding code the the class methods. You may want to study the existing code first to get a feel for how to access the class attributes. There is no need to modify any of the methods that are already implemented.
UPDATE: You are working with magic_square.cpp. View comments as ref. Thank you.
Attachments ~ (3):
magic.h ~
#ifndef MAGIC_H
#define MAGIC_H
#define SQUARE_SIZE 3
class MagicSquare {
private:
    
    // matrix is a two-dimensional array that holds the 
    // magic square.  
    int matrix[SQUARE_SIZE][SQUARE_SIZE] = {{ 4, 9, 2 }, 
                                            { 3, 5, 7 },
                                            { 8, 1, 6 }};
    
public:
    
    // is_magic - returns true if matrix forms a magic square.  
    // a magic square is one that all the rows and all the cols and 
    // the two diagonals add up to the same number. 
    bool is_magic();
    
    // GIVEN : scramble(int loops) - switch two random elements of the matrix for form
    // a new square. 
    void scramble(int loops=1);
    
    // row_sum(int r) - return the sum of all the elements of row r. 
    int row_sum(int);
    
    // col_sum(int c) - return the sum of all the elements of column c.
    int col_sum(int);
    
    // diagnoal_left() - return the sum of all the elements from the upper-right
    // to lower-left. 
    int diagonal_left();
    
    // diagnoal_right() - return the sum of all the elements from the upper-left
    // to the lower-right. 
    int diagonal_right();
    
    // showMatrix() - Print the values of the matrix in a square format.  
    // For example: 
    //
    //    1 2 3
    //    4 5 6
    //    7 8 9
    //
    void showMatrix();
    
    // GIVEN:  set_matrix(int * array) - create a new matrix using the values
    // of the array.  
    void set_matrix(int *);
    
};
#endif
________________________________________________________________________________________________
magic_square.cpp ~
#include  
#include  
#include 
#include 
#include "magic.h"
using namespace std;
// Replace # after return as row_sum and col_sum 
int MagicSquare::row_sum(int r) {
    return matrix [0][0] + matrix [0][1] + matrix [0][2];
}
int MagicSquare::col_sum(int c) {
    return matrix [0][0] + matrix [1][0] + matrix [2][0];
}
int MagicSquare::diagonal_left() {
   return 0;
}
int MagicSquare::diagonal_right() {
    return 2;
}
bool MagicSquare::is_magic() {
 return false;
}
void MagicSquare::showMatrix() {
}
// Swap two random values of the matrix. 
void MagicSquare::scramble(int loops=1) {
    
    static bool _initialized = false;
   
    if (! _initialized) {
        srand((unsigned)time(0)); 
        _initialized = true;
    }
    while (loops--) { 
        int r1 = (rand()%SQUARE_SIZE);
        int c1 = (rand()%SQUARE_SIZE); 
        int r2 = (rand()%SQUARE_SIZE); 
        int c2 = (rand()%SQUARE_SIZE); 
        int temp = matrix[r1][c1];
        matrix[r1][c1] = matrix[r2][c2];
        matrix[r2][c2] = temp;
    }
}
// Create a new set of values for a magic square. The user should pass in an 
// array of 3 integers. 
void MagicSquare::set_matrix(int *values) {
   
    int a = values[0];
    int b = values[1]; 
    int c = values[2];
    
 matrix[0][0] = a;
 matrix[0][1] = 3 * c - a - b;
 matrix[0][2] = b;
 
 matrix[1][0] = c + b - a;
 matrix[1][1] = c;
 matrix[1][2] = c - b + a;
 
 matrix[2][0] = 2 * c - b;
 matrix[2][1] = a + b - c;
 matrix[2][2] = 2 * c - a;
   
}
_________________________________________________________________________________________________________
main.cpp ~
#include 
#include "magic.h"
using namespace std;
/*
 * 
 */
int main()
{       
    int times = 200000;
    MagicSquare square;
    int my_values[SQUARE_SIZE];
  
    cout << "Here is array \n\n";
    square.showMatrix();
    if (square.is_magic())
        cout << "\nIt is a magic square\n\n";
    else {
        cout << "Program is not working!!" << endl;
        return 1;
    }
    while (times--) {
        square.scramble();
        if (square.is_magic()) {
            cout << "Found one !!" << endl;
            square.showMatrix();
            cout << endl;
        }
    }
    
    cout << "Now you try!  Enter " << SQUARE_SIZE << " values" << endl;
            
    // Collect 3 values from the user and store them in an array called my_values
   
    // Call the set_matrix method and pass in the my_values array.
 
    // Display the new matrix using the showMatrix() method. 
    // Copy the while loop above to search for more versions of this magic square. 
     
    return 0;
} // End main function
magic.h
#ifndef MAGIC_H
#define MAGIC_H
#define SQUARE_SIZE 3
class MagicSquare {
private:
// matrix is a two-dimensional array that holds the
// magic square.
int matrix[SQUARE_SIZE][SQUARE_SIZE] = {{ 4, 9, 2 },
{ 3, 5, 7 },
{ 8, 1, 6 }};
public:
// is_magic - returns true if matrix forms a magic square.
// a magic square is one that all the rows and all the cols
and
// the two diagonals add up to the same number.
bool is_magic();
// GIVEN : scramble(int loops) - switch two random elements of
the matrix for form
// a new square.
void scramble(int loops=1);
// row_sum(int r) - return the sum of all the elements of row
r.
int row_sum(int);
// col_sum(int c) - return the sum of all the elements of column
c.
int col_sum(int);
// diagnoal_left() - return the sum of all the elements from the
upper-right
// to lower-left.
int diagonal_left();
// diagnoal_right() - return the sum of all the elements from
the upper-left
// to the lower-right.
int diagonal_right();
// showMatrix() - Print the values of the matrix in a square
format.
// For example:
//
// 1 2 3
// 4 5 6
// 7 8 9
//
void showMatrix();
// GIVEN: set_matrix(int * array) - create a new matrix using
the values
// of the array.
void set_matrix(int *);
};
#endif
-------------------------------------------------------------------
magic_square.cpp
//magic_square.cpp ~
#include <iostream>
#include "magic.h"
#include <stdlib.h>
#include <ctime>
using namespace std;
// Replace # after return as row_sum and col_sum
int MagicSquare::row_sum(int r) {
// return matrix [0][0] + matrix [0][1] + matrix [0][2];
return matrix [r][0] + matrix [r][1] + matrix
[r][2];
}
int MagicSquare::col_sum(int c) {
//return matrix [0][0] + matrix [1][0] + matrix [2][0];
return matrix [0][c] + matrix [1][c] + matrix
[2][c];
}
int MagicSquare::diagonal_left() {
//sum of left diagonal elements
return matrix [0][0] + matrix [1][1] + matrix
[2][2];
}
int MagicSquare::diagonal_right() {
//sum of RIGHT diagonal elements
return matrix [0][2] + matrix [1][1] + matrix
[2][0];
}
bool MagicSquare::is_magic() {
///first check sum of row(0) == sum of row(1) == sum of
row(2)
if(row_sum(0) == row_sum(1) && row_sum((1) ==
row_sum(2))){
///means all rows have same sum
if(col_sum(0) == col_sum(1) && col_sum(1)==
col_sum(2)){
///here all row and cols sum is same
if(diagonal_left() == diagonal_right()){
///here Sum of all rows = Sum of all cols =Sum of all
diagonals
return true;
}
}
}
return false;
}
// showMatrix() - Print the values of the matrix in a square
format.
void MagicSquare::showMatrix() {
cout<<endl;
for(int i=0;i<SQUARE_SIZE;i++){
for(int j=0;j<SQUARE_SIZE;j++){
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
// Swap two random values of the matrix.
void MagicSquare::scramble(int loops) {
static bool _initialized = false;
if (! _initialized) {
srand((unsigned)time(0));
_initialized = true;
}
while (loops--) {
int r1 = (rand()%SQUARE_SIZE);
int c1 = (rand()%SQUARE_SIZE);
int r2 = (rand()%SQUARE_SIZE);
int c2 = (rand()%SQUARE_SIZE);
int temp = matrix[r1][c1];
matrix[r1][c1] = matrix[r2][c2];
matrix[r2][c2] = temp;
}
}
// Create a new set of values for a magic square. The user
should pass in an
// array of 3 integers.
void MagicSquare::set_matrix(int *values) {
int a = values[0];
int b = values[1];
int c = values[2];
matrix[0][0] = a;
matrix[0][1] = 3 * c - a - b;
matrix[0][2] = b;
matrix[1][0] = c + b - a;
matrix[1][1] = c;
matrix[1][2] = c - b + a;
matrix[2][0] = 2 * c - b;
matrix[2][1] = a + b - c;
matrix[2][2] = 2 * c - a;
}
-----------------------------------------------------------------------------------
main.cpp
#include <iostream>
#include "magic.h"
using namespace std;
int main()
{
//int times = 200000;
int times = 5;
MagicSquare square;
int my_values[SQUARE_SIZE];
cout << "Here is array \n\n";
square.showMatrix();
if (square.is_magic())
cout << "\nIt is a magic square\n\n";
else {
cout << "Program is not working!!" << endl;
return 1;
}
while (times--) {
square.scramble();
if (square.is_magic()) {
cout << "Found one !!" << endl;
square.showMatrix();
cout << endl;
}
else{
cout << "NOT Found !!" << endl;
square.showMatrix();
cout << endl;
}
}
cout << "Now you try! Enter " << SQUARE_SIZE << " values" << endl;
// Collect 3 values from the user and store them in an array
called my_values
for(int i=0;i<SQUARE_SIZE;i++)
cin>>my_values[i];
// Call the set_matrix method and pass in the my_values
array.
square.set_matrix(my_values);
// Display the new matrix using the showMatrix() method.
square.showMatrix();
// Copy the while loop above to search for more versions of this
magic square.
if (square.is_magic())
cout << "\nIt is a magic square\n\n";
else {
cout << "Program is not working!!" << endl;
return 1;
}
times=20000; ///set to new value as currently times =0 and it will
run in infinite loop
//times=15;
while (times--) {
square.scramble();
if (square.is_magic()) {
cout << "Found one !!" << endl;
square.showMatrix();
cout << endl;
}
else{
cout << "NOT Found !!" << endl;
square.showMatrix();
cout << endl;
}
}
return 0;
} // End main function
----------------------
