I'm attempting to wrap my head around a somewhat... logically-difficult programming assignment for a Java course. I'm not looking for help with the actual code, per-se, but figuring out the program structure is wringing my brain out, so I figured I'd see if you folks might be able to point me right.
The assignment describes a series of eight variables, A-H, which can each have any one of a series of eight values. There are five equations given, along the lines of "A+C=H" and "B+A=E". The object of the assignment is to create a program that can sort through all the various combinations of variables & values until it comes across the correct combination for all the equations.
Thus far, I've three classes, a "Variable" class that stores the variable and value of that variable, an "Expression" class that stores three Variables, and has a method to determine if the three Variables fit any of the equations. Finally, I've got a "Launcher" class that contains the main method and makes the magic happen. It has several arrays, one containing eight Variables, another containing five Expressions, and one to contain all the possible values alongside an array that contains the eight characters.
So far, I know that I'm thinking to use a series of nested for loops in order to cycle through all the character letters, assigning them to the Variable object array... but I'm at a loss as to how to *also* cycle through the values at the same time. Does anyone have a general idea of what an algorithm to do this would look like? I've included my for loop below... thanks!
for (int i = 0; i < varAry.length; i++){
//The whole point of this thing is to assign each variable every single value and test them,
// changing one variable at a time until you wind up with a solution that fulfills all the test conditions.
for(int charA = 0;;charA++){
varAry[i].setLetter(charAry[charA]);
for(int charB = 0;;charB++){
varAry[i].setLetter(charAry[charB]);
for(int charC = 0;;charC++){
varAry[i].setLetter(charAry[charC]);
for(int charD = 0;;charD++){
varAry[i].setLetter(charAry[charD]);
for(int charE = 0;;charE++){
varAry[i].setLetter(charAry[charE]);
for(int charF = 0;;charF++){
varAry[i].setLetter(charAry[charF]);
for(int charG = 0;;charG++){
varAry[i].setLetter(charAry[charG]);
for(int charH = 0;;charH++){
varAry[i].setLetter(charAry[charH]);
}
}
}
}
}
}
}
}
}

New Topic/Question
Reply




MultiQuote




|