[2016-04-04] Challenge #261 [Easy] verifying 3x3 magic squares

Kind of messy/ugly code - with both bonuses + small menu. Could essentially combine the functions - but I kept them separate for clarity. Hopefully this formats properly this time

#include <iostream>
using namespace std;
int CheckSquare1(int magic[9]) {
int a;
a = magic[0]+magic[1]+magic[2];
if (a == 15) {
    a = magic[3]+magic[4]+magic[5];
    if (a == 15) {
        a = magic[6]+magic[7]+magic[8];
        if (a == 15) {
            a = magic[0]+magic[4]+magic[8];
                if (a == 15) {
                    a = magic[2]+magic[4]+magic[6];
                    if (a == 15) { cout << "Square is Verified!" << endl; }
                    else { cout << "That square is invalid :(" << endl; }
                }
        }
        else { cout << "That square is invalid :(" << endl; }
    }
    else { cout << "That square is invalid :(" << endl; }
}
else { cout << "That square is invalid :(" << endl; }

} int IsMultipleOf(int a, int b) { //int a is the number to check, b is the multiple if (a % b == 0) { return 1; } else { return 0; } } int CheckSquare2() { int a; int b; int c; cout << "Enter Number of square: "; cin >> a; b = aa; //get amount of squares c = (a(b+1)/2); // calculate how much needs to be added //if you don't feel like entering in x amound of squares uncomment one of these and comment lines the line starting with int magic[b] and the while loop // int magic[b] = {8,1,6,3,5,7,4,9,2}; // int magic[b] = {7, 12, 1, 14, 2, 13, 8, 11, 16, 3, 10, 5, 9, 6, 15, 4}; //int magic[b] = {17,24,1,8,15,23,5,7,14,16,4,6,13,20,22,10,12,19,21,3,11,18,25,2,9};

int magic[b];
int d = 0;
while (d < b) { //todo: do a check to see if a number is repeated or out of a range (which would be checking against b) but I am too lazy because it should fail once it checks the magic square either
        cout << "Enter Square Value " << d+1 << ": ";
        cin >> magic[d];
        d++;
    }
//start loop #1 - checking columns
d = 0;
int e = 0; //this is gonna be used as a temp variable for the addition
int f = 0; //variable to check if it should progress in the tests
while (d < b) {
    e = e+magic[d];
    d++;
    if (IsMultipleOf(d,a)) {
        if (e == c) { e = 0; f++; }
        else { d = b+1; }
    }
}
if (f == a) { cout << "Passed the column test" << endl; }
else { cout << "Not a valid square - didn't pass the column test" << endl; return 0; }
d = 0;
e = 0; //this is gonna be used as a temp variable for the addition
f = 0; //variable to check if it should progress in the tests
int g = 0; //temp value for multiple
while (d < b) {
    e = e+magic[g*a+f];
    g++;
    if (e == c) { e = 0; f++; g=0; }
    d++;
}
if (f == a) { cout << "Passed the row test" << endl; }
else { cout << "Failed at the row test" << endl; }
d = 0; // resetting loop variable to 0
e = 0; //this is gonna be used as a temp variable for the addition
f = 0; //variable to check if it should progress in the tests
g = 0; //temp value for multiple
int h = 0;
while (d < a) {
    g = (a-1)+h;
    e = e+magic[g];
    h+=(a-1);
    if (e == c) { f++; }
    d++;
}
if (f == 1) { cout << "Passed diag test #1" << endl; }
else { cout << "Failed at the diag test #1" << endl; }
d = 0; // resetting loop variable to 0
e = 0; //this is gonna be used as a temp variable for the addition
f = 0; //variable to check if it should progress in the tests
g = 0; //temp value for multiple
while (d < a) {
    g = (a+1)*d;
    e = e+magic[g];
    if (e == c) { f++; }
    d++;
}
if (f == 1) { cout << "This is a valid square!" << endl; }
else { cout << "Sorry, invalid square" << endl; }
return 0;

} int CheckSquare3() { int magic[9]; int chk[9] = {0,0,0,0,0,0,0,0,0}; //0 = no, 1 = yes int b[3] = {0,0,0}; //placeholders int x = 0; int y = 0; int a; int z = 0; int chk1, chk2; while (x < 6) { cout << "Enter square " << x+1 << ": "; cin >> magic[x]; a = magic[x]; a = a-1; if (chk[a] == 1) { x = 6; cout << "Sorry can't enter another number twice" << endl; } else { chk[a] = 1; x++; } } // it should be filled up to square 5 now with no repeats x = 0; while (x < 9) { if (chk[x] == 1) { x++; } else { b[y] = x+1; y++; x++; } } chk1 = magic[0]+magic[1]+magic[2]; chk2 = magic[3]+magic[4]+magic[5]; if ((chk1 == 15) && (chk2 == 15)) { while (z < 2) { x = 0; //loop variable y = 0; //for b[y] to find unknown int c = 0; //for the while loop trying to find unknown; int d; while (x < 4) { if (x == 0) { while (c < 3) { d = 0; d = magic[0]+magic[3]; d = d+b[c]; if (d == 15) { magic[6] = b[c]; x++; c++; } else { c++; } } } if (x == 1) { c = 0; while (c < 3) { d = 0; d = magic[1]+magic[4]; d = d+b[c]; if (d == 15) { magic[7] = b[c]; x++; c++; } else { c++; } } } if (x == 2) { c = 0; while (c < 3) { d = 0; d = magic[2]+magic[5]; d = d+b[c]; if (d == 15) { magic[8] = b[c]; x++; c++; } else { c++; } } } if (x == 3) { CheckSquare1(magic); return 0; } } } } else { cout << "Invalid Square" << endl; return 0; } } int main() { int choice; cout << "Magic Square Verifier" << endl << "1 - Normal" << endl << "2- Any Size" << endl << "3 - Missing bottom row" << endl; cin >> choice; switch(choice) { case 1: { int magic[9]; int x = 0; while (x < 9) { cout << "Enter square " << x+1 << ": "; cin >> magic[x]; x++; } CheckSquare1(magic); break; } case 2: CheckSquare2(); break; case 3: CheckSquare3(); break; default: cout << "Invalid Selection, Bye" << endl; return 0; break; } }

/r/dailyprogrammer Thread