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(); }