4 Replies - 3482 Views - Last Post: 05 December 2012 - 11:00 AM Rate Topic: -----

#1 tmarr80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-November 12

Magic Square Program

Posted 04 December 2012 - 04:22 PM

Hello!

I have an assignment to code a program that allows you to enter in the size of magic square (ex: if you enter 4 for size then it will be a 4x4 grid) and then populate it row by row. After you have completed populating the grid using a multidimensional array, the program is *supposed* to determine whether or not the values entered create a magic square (all columns, rows, and diagonals add up to same sum). If it does, then the program outputs "This is a magic square" and if it does not, then it should output "This is not a magic square." The issue I'm having is that no matter if it is or isn't, I'm always getting a consistent output of "This is a magic square."

I thought this might have something to do with the if statement in the main program that evaluates whether or not the is_magic function is true. So alternatively I tried this syntax (where my if statement conditional evaluates if is_magic is not equal to 0 which is the same as false) and then received the alternative output consistently as well, the opposite of the problem I was having with the initial syntax I use in the full program below.

void main() 
{
  cout << "Welcome to Magic Square Program" << endl << endl;  
  read();
  if (is_magic()!=0)
	  cout << "That square is magic!" << endl;
  else          
	  cout << "That square is not magic." << endl;

  cin.get();
}




So I'm pretty confused as to what is causing my program to always output the same thing regardless of whether or not the grid creates a magic square. Can anyone give me some insight, or at least hint at where the problem lies in my program, for example: "Take another look at function x,y,z."

Here is my program in it's entirety:

 
// p7.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
using namespace std;
 
int size;
int grid[9][9];

void read()  /* Post: the square data is read  */
{
cout << "Enter size of magic square up to 9:";
	cin >> size;
	cout << "Now type in the data row by row:" << endl;

if(1<=size<=9)
{
	for(int i=0; i<size; i++)
	{	
		for(int j=0; j<size;j++)
		{
			cin >> grid[i][j];
		}
	}
}	
}


int sum_other()
/*   Post: Returns the non-main diagonal sum  */
{
  int sum = 0;
  for(int j=0; j<size; j++)
	{
		sum = sum+grid[j][j];
	}
	return sum;
}

int sum_maindiag()
/*   Post: Returns the main diagonal sum  */
{
   int sum = 0;
      for(int j=0; j<size; j++)
  {
		sum = sum+grid[j][j];
  }
   return sum;
}

int sum_row(int i)
/*   Pre: i is a valid row of the square
    Post: Returns the row sum of the ith row  */
{
   int sum = 0;
      for(int j=0; j<size; j++)
  {
		sum = sum+grid[j][j];
  }
   return sum;
}

int sum_col(int i)
/*   Pre: i is a valid col of the square
    Post: Returns the col sum of the ith col  */
{
   int sum = 0;
     for(int j=0; j<size; j++)
  {
		sum = sum+grid[j][j];
  }
   return sum;
}

bool is_magic()
{
   int number = sum_maindiag(); 
   if (sum_other() != number) return false;
   for (int i = 0; i < size; i++)
      if (sum_row(i) != number) return false;
   for (int i = 0; i < size; i++)
      if (sum_col(i) != number) return false;
   return true;
}

void main() 
{
  cout << "Welcome to Magic Square Program" << endl << endl;  
  read();
  if (is_magic())
	  cout << "That square is magic!" << endl;
  else          
	  cout << "That square is not magic." << endl;

  cin.get();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Magic Square Program

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 337
  • View blog
  • Posts: 730
  • Joined: 27-June 09

Re: Magic Square Program

Posted 04 December 2012 - 04:43 PM

All of your sum functions are the same code. They all just get the sum of the same diagnol, so they will always return the same result.
Was This Post Helpful? 0
  • +
  • -

#3 tmarr80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-November 12

Re: Magic Square Program

Posted 04 December 2012 - 05:22 PM

So I guess my question would be then; how do I sum up the different columns/rows/diagonals? I know it has to be the one for loop with sum=sum+grid[][], that much is correct, but I don't understand how to create variability in the summation.

Do I need to do something like this for each function...

grid[i][j] for one of the loops
grid[j][i] for another
grid[i][i] for another
grid[j][j] for another?

am I on the right track?
Was This Post Helpful? 0
  • +
  • -

#4 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: Magic Square Program

Posted 04 December 2012 - 07:16 PM

View Posttmarr80, on 04 December 2012 - 03:22 PM, said:

int sum_row(int i)
/*   Pre: i is a valid row of the square
    Post: Returns the row sum of the ith row  */
{
	int sum = 0;
	for(int j=0; j<size; j++)
	{
	    sum = sum+grid[j][j];
	}
	return sum;
}


The problem with this code for this method is that it isn't returning the sum for that specific row, it's returning the diagonal going from top left to bottom right of the square. For this one, you would need to have your sum change like this: sum += grid[i][j]; where i is the row that the has been requested and j is the current column.

View Posttmarr80, on 04 December 2012 - 03:22 PM, said:

int sum_col(int i)
/*   Pre: i is a valid col of the square
    Post: Returns the col sum of the ith col  */
{
	int sum = 0;
	for(int j=0; j<size; j++)
	{
	    sum = sum+grid[j][j];
	}
	return sum;
}


Here, it is the same as above, but instead it should be reversed (i.e. sum += grid[j][i];).

View Posttmarr80, on 04 December 2012 - 03:22 PM, said:

int sum_other()
/*   Post: Returns the non-main diagonal sum  */
{
	int sum = 0;
	for(int j=0; j<size; j++)
	{
	    sum = sum+grid[j][j];
	}
	return sum;
}


Here, you don't need to change how the grid works, just how the for loop is initially started. Instead of assigning 0 to j, assign size-1, and instead of checking if j is less than the size, check if it is greater than or equal to 0, and instead of incrementing at the end of each pass, decrement at the end of each pass j--


That should hopefully help you fix your problems.
Was This Post Helpful? 1
  • +
  • -

#5 tmarr80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-November 12

Re: Magic Square Program

Posted 05 December 2012 - 11:00 AM

That worked! Thanks psyking!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1