Thanks a lot Boreas! That was all I needed to get started… It makes more sense now.
I wrote just about the whole program now, but when I go to run it, nothing happens; I just wait for it to print a square. I was wondering if you could take a look at my code?
#include <iostream>
using namespace std;
int answersArray[3456][4][4], currentAnswer=0;
//Prototype
void fillArray(int smSquare[]);
void printIt(int num);
int main()
{
int smSquare[9];
for(int i=0; i < 3456; i++)
{
fillArray(smSquare);
printIt(i);
}
cout << "\nPress enter key to continue...";
cin.get();
return 0;
}
void printIt(int num)
{
for(int i=0; i < 4; i++)
{
for(int j=0; j < 4; j++)
cout << answersArray[num][j][i] << " ";
cout << "\n";
}
}
bool inSquare(int val, int smSquare[], int currentSpot)
{
for(int i=0; i < currentSpot; i++)
if(smSquare[i] == val) return true;
return false;
}
/****************************************************************\
checkAndConvert changes the array to 2d, fills in the rest of the
square, returns true and puts the new array into the answers array
if everything checks out (everything sums 34)
/****************************************************************\ */
bool checkAndConvert(int smSquare[])
{
int total;
//Converts small 3x3 square into big 4x4 square (multi dimensional)
int bgSquare[4][4];
bgSquare[0][0] = smSquare[0];
bgSquare[1][0] = smSquare[1];
bgSquare[2][0] = smSquare[2];
bgSquare[0][1] = smSquare[4];
bgSquare[1][1] = smSquare[5];
bgSquare[2][1] = smSquare[6];
bgSquare[0][2] = smSquare[7];
bgSquare[1][2] = smSquare[8];
bgSquare[2][2] = smSquare[9];
//End conversion
//Fill empty spots
/* complete rows */
bgSquare[3][0] = 34 - smSquare[0] - smSquare[1] - smSquare[2];
bgSquare[3][1] = 34 - smSquare[3] - smSquare[4] - smSquare[5];
bgSquare[3][2] = 34 - smSquare[6] - smSquare[7] - smSquare[8];
/*complete columns */
bgSquare[0][3] = 34 - smSquare[0] - smSquare[3] - smSquare[6];
bgSquare[1][3] = 34 - smSquare[1] - smSquare[4] - smSquare[7];
bgSquare[2][3] = 34 - smSquare[2] - smSquare[5] - smSquare[8];
/*fill last spot*/
bgSquare[3][3] = 34 - bgSquare[0][3] - bgSquare[1][3] - bgSquare[2][3];
//End Fill empty spots
//Check 34's
/* rows */
for(int i=0; i < 4; i++)
{
total = 0;
for(int j=0; j < 4; j++)
total += bgSquare[i][j];
if(total != 34) return false;
}
/* columns */
for(int i=0; i < 4; i++)
{
total = 0;
for(int j=0; j < 4; j++)
total += bgSquare[j][i];
if(total != 34) return false;
}
//End check 34's
//Save to answers Array
for(int i=0; i < 4; i++)
for(int j=0; j < 4; j++)
answersArray[currentAnswer][j][i] = bgSquare[j][i];
currentAnswer += 1;
//End save to answers Array
return true;
}
void fillArray(int smSquare[])
{
for(smSquare[0]=1; smSquare[0] < 17; smSquare[0]++)
{
//if(inSquare(smSquare[0], smSquare, 0)) continue;
for(smSquare[1]=1; smSquare[1] < 17; smSquare[1]++)
{
if(inSquare(smSquare[1], smSquare, 1)) continue;
for(smSquare[2]=1; smSquare[2] < 17; smSquare[2]++)
{
if(inSquare(smSquare[2], smSquare, 2)) continue;
for(smSquare[2]=1; smSquare[2] < 17; smSquare[2]++)
{
if(inSquare(smSquare[3], smSquare, 3)) continue;
for(smSquare[3]=1; smSquare[3] < 17; smSquare[3]++)
{
if(inSquare(smSquare[4], smSquare, 4)) continue;
for(smSquare[4]=1; smSquare[4] < 17; smSquare[4]++)
{
if(inSquare(smSquare[5], smSquare, 5)) continue;
for(smSquare[5]=1; smSquare[5] < 17; smSquare[5]++)
{
if(inSquare(smSquare[6], smSquare, 6)) continue;
for(smSquare[6]=1; smSquare[6] < 17; smSquare[6]++)
{
if(inSquare(smSquare[7], smSquare, 7)) continue;
for(smSquare[7]=1; smSquare[7] < 17; smSquare[7]++)
{
if(inSquare(smSquare[8], smSquare, 8)) continue;
for(smSquare[8]=1; smSquare[8] < 17; smSquare[2]++)
{
if(inSquare(smSquare[8],smSquare,0)) continue;
if(checkAndConvert(smSquare))
return;
}
}
}
}
}
}
}
}
}
}
}
If you don't have the time to look at this I totally understand. But if you could find the logic error then that would be great because I can't seem to.
Thanks again.
BTW, I know that I don't search for diagonals and quadrants yet. I just wanted to see if it would work just finding rows and columns before I wrote that, but it didn't.