Array values incorrect positions

Hey everyone,

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <sstream>
#include <string>
#include <istream>
using namespace 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:

http://img687.imageshack.us/img687/1240/sudokubugimg.jpg

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.
1
2
3
4
5
6
7
        for(int iCounter = 0; iCounter < iAmount; iCounter++){
		cout << "\nInput " << iCounter + 1<< "(Coordinates): ";
		cin >> iCoords[0] >> iCoords[1];
		cout << "Input " << iCounter + 1<< "(Value): ";
		cin >> iValue;
		field[(iCoords[1]&0x0F)-1][(iCoords[0]&0x0F)-1] = iValue;
	}


EDIT:
just a minor retouch of your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int main(){
	int field[9][9] = {0};
	char inputX = 0;
	char inputY = 0;
	int iAmount; //given values count
	int iValue;
	cout << "How many boxes have been filled out already?: ";
	cin >> iAmount;
	cout << "Enter the coordinates of the boxes, and then their value\n";

    for(int iCounter = 0; iCounter < iAmount; iCounter++) {
        cout << "\nInput " << iCounter + 1<< "(Coordinates): ";
        cin >> inputX >> inputY;
        cout << "Input " << iCounter + 1<< "(Value): ";
        cin >> iValue;
        field[(inputY&0x0F)-1][(inputX&0x0F)-1] = iValue;
    }

    cout << '\n';

    for(int r = 0; r < 9; r++) {
        for(int c = 0; c < 9; c++) {
            cout << field[r][c];
            if(c % 3 == 2) printf("  ");
        }
        cout << '\n';
        if(r % 3 == 2) cout << "\n";
    }
}
Last edited on
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.
Last edited on
I do understand both those systems =)
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
Last edited on
Thanks again :) I'm not yet completely with you on that... but I'm getting there. Some more gazing over that and I'll probably get it ;)
Topic archived. No new replies allowed.