I've been working on a school assignment, which is to create a Sudoku solving program (Sudoku: you know, those 9 by 9 japanese puzzles). Now I'm not here right now to ask how to do this ofcourse, but I'm having some trouble with the array I use as the sudoku field. Here's the code:
#include <iostream>
#include <sstream>
#include <string>
#include <istream>
usingnamespace std;
int main(){
int field[9][9] = {0};
int inputX = 0;
int inputY = 0;
int iAmount; //iAmount stands for input amount, the number of already filled in boxes.
int iValue;
char iCoords[2];
cout << "How many boxes have been filled out already?: ";
cin >> iAmount;
cout << "Enter the coordinates of the boxes, and then their value (Format X-coordinateY-coordinate for coordinates)\n";
for(int iCounter = 0; iCounter < iAmount; iCounter++){ //iCounter stands for input counter, this is used to count how many inputs have already been entered. It's also used to end the input sequence when the input counter value is equal to the number of already filled in boxes.
cout << "\nInput " << iCounter + 1<< "(Coordinates): ";
cin >> iCoords;
cout << "Input " << iCounter + 1<< "(Value): ";
cin >> iValue;
field[iCoords[2]][iCoords[1]] = iValue;
}
//This next shitload of cout is to print the sudoku field(array) in its current state
cout << "\n";
cout << "|-----|-----|-----|\n|" << field[0][0] << " " << field[0][1] << " " << field[0][2] << "|" << field[0][3] << " " << field[0][4] << " " << field[0][5] << "|" << field[0][6] << " " << field[0][7] << " " << field[0][8] << "|\n";
cout << "|" << field[1][0] << " " << field[1][1] << " " << field[1][2] << "|" << field[1][3] << " " << field[1][4] << " " << field[1][5] << "|" << field[1][6] << " " << field[1][7] << " " << field[1][8] << "|\n";
cout << "|" << field[2][0] << " " << field[2][1] << " " << field[2][2] << "|" << field[2][3] << " " << field[2][4] << " " << field[2][5] << "|" << field[2][6] << " " << field[2][7] << " " << field[2][8] << "|\n";
cout << "|-----|-----|-----|\n|" << field[3][0] << " " << field[3][1] << " " << field[3][2] << "|" << field[3][3] << " " << field[3][4] << " " << field[3][5] << "|" << field[3][6] << " " << field[3][7] << " " << field[3][8] << "|\n";
cout << "|" << field[4][0] << " " << field[4][1] << " " << field[4][2] << "|" << field[4][3] << " " << field[4][4] << " " << field[4][5] << "|" << field[4][6] << " " << field[4][7] << " " << field[4][8] << "|\n";
cout << "|" << field[5][0] << " " << field[5][1] << " " << field[5][2] << "|" << field[5][3] << " " << field[5][4] << " " << field[5][5] << "|" << field[5][6] << " " << field[5][7] << " " << field[5][8] << "|\n";
cout << "|-----|-----|-----|\n|" << field[6][0] << " " << field[6][1] << " " << field[6][2] << "|" << field[6][3] << " " << field[6][4] << " " << field[6][5] << "|" << field[6][6] << " " << field[6][7] << " " << field[6][8] << "|\n";
cout << "|" << field[7][0] << " " << field[7][1] << " " << field[7][2] << "|" << field[7][3] << " " << field[7][4] << " " << field[7][5] << "|" << field[7][6] << " " << field[7][7] << " " << field[7][8] << "|\n";
cout << "|" << field[8][0] << " " << field[8][1] << " " << field[8][2] << "|" << field[8][3] << " " << field[8][4] << " " << field[8][5] << "|" << field[8][6] << " " << field[8][7] << " " << field[8][8] << "|\n|-----|-----|-----|\n";
system("PAUSE"); //placeholder to keep the program from terminating
}
Now the problem I'm having is that when I run this code and I enter some coordinates and values, they seem to be in completely incorrect positions in the array. An image to illustrate what I mean:
So their positions are way off. I realize the error could be in the order in which the array is printed, but it seems to me that there's nothing wrong in that part of the code (The part with all the cout). And I also realize that a while and an if could have done the job of printing the array perhaps somewhat easier but that's not my concern now.
Thanks for replying, but I don't see how that link could help me. It's a different problem? Just to be clear again: Im not asking for a way to do the Sudoku solving part, im asking for a way to fix the fact that my array is putting values in completely wrong places without an apparent reason.
Thank you very much! It works fine now =) Are you willing to explain to me what this actually does, or how it works? If I just take over what you just gave him I don't learn a lot from it ;)
do you understand binary, and hexadecimal number system? if yes then i'll explain it. if not read first or just watch it on youtube if you lazy enough to read like me.
ok iCoords here is a char right. if you look at the ascii code, numbers (0-9) are acutally represented (48-57) in binary. lets say you input 25 as the coordinate so iCoords now has
1 2 3 4 5 6 7 8 9 10 11 12 13
//x coordinate
iCoords[0] = '2'; //in ascii 0011 0010
//y coordinate
iCoords[1] = '5'; //in ascii 0011 0101
//notice that the last 4 bit has the integer value of
//the character so need to get rid of the first 4
0011 0010 //this is iCoords[0]
& //AND gate
0000 1111 //this is 0x0f
0000 0010 //which is equal to 2
//so now 2 is use as the x coordinate
//and same goes with 5
i'm not good in english so i can't explain it that much clear.
BTW this should also work '2'-'0'//this give you an integer value of 2