This is the class square and the main function.

const int max_size = 9;
class Square {
   public:
      void read();     //the square data is read
      bool is_magic();     // determin if the given square is a magic square
   private:
      int sum_row(int i);     // returns the row sum of the ith row
      int sum_col(int i);      // returns the col sum of the ith row
      int sum_maindiag();   // returns the main the main diagonal sum
      int sum_other();     // returns the non-main diagonal sum
      int size;
      int grid[max_size][max_size];
};
void main()
{
      cout << "Magic Square Program" << endl << endl;
      cout << "Enter a square of integers:" << endl;
      Square s;
      s.read();
      if (s.is_magic()) cout << "That square is magic!" << endl;
      else cout << "That square is not magic." << endl;
}
2

There are 2 answers

1
Grammin On

So basically you have to write and implement the Square class. The one that you've detailed has two public methods which means that those methods can be called anywhere. Therefore in your main you're calling the s.read() method and s.is_magic() to access the class. So you declare an instance of Square and call it s and then you use s.read() to call the read() method within s which is a instance of the class square.

You have a bunch of private functions in the square class to help write it. Private functions are functions that can only be called within that class. So start by making the read method within the square class. You should use the helper functions like sum_row() and sum_col() to help write your read function. Also the private class vars like size are able to be used across functions within the class.

If you have any questions leave a comment. But if you're trying to get out of writing the code yourself no one here is going to write it for you. By the way I've used methods/functions interchangeably here, you can look up what the difference is if you want.

0
Tim On

A good way to go about software is in 4 phases: Requirements, Design, Coding, Testing.

  1. Requirements. What is it that you actually want to do? In your case, check for Magic Squares.
  2. Design. How do you want to do this? Plan your software before you write code. Write it out in plain English (or whatever language).
  3. Coding. Now that you have a plan, write your code.
  4. Testing. Test your software to make sure it does what you set out to do.

You can do this in small iterations, all at once, there's a lot of variations on how to go about this, but it's a good way to approach the task of writing a program.

In your case, you're up to phase 2. So take the time to think about what a Magic Square is, and think of how to go about checking for it. Then try to take your algorithm and write it into code.