Hello again experts!
I have another problem with a program i'm learning. You guys are gonna get sick of me soon!
But since theres no one else who knows c.. here goes:
I'm teaching myself c by giving myself a problem and trying to solve it.
Basically i'm trying to code a sudoku solver.
My program so far takes a 9x9 matrix, goes through each element and if its non zero, removes it from a row array, a column array and a set array. Thus leaving a set of possible values for that square.
Anyhow, my program is working so far to a point. my problem is reading in the matrix. I'm trying to use scanf. But its not working.
I wanted to use scanf untill i figured out how to make my program read from a file. (Don't know how to do that yet!!)
So my program is as follows:
Code:
#include <stdio.h>
int main()
{
int originalarray[9][9]; // 9x9 matrix, note that the elements are numbered from 0 and that (9,9) isn't used.
int x, y, a, i, c;
int row[9][9], col[9][9];
for (x = 0; x <= 8; x++) // Goes through rows
{
for (y = 0; y <= 8; y++) //Goes through columns
//scanf( "%d", &c ); // reads character input
originalarray[x][y] = y; // makes the integer value of the matrix equal to the character input
}
for (x = 0; x <= 8; x++) // Goes through rows
{
for (y = 0; y <= 8; y++) //Goes through columns
row[x][y] = y + 1; // makes the integer value of the matrix equal to the column number + 1
}
for (x = 0; x <= 8; x++) // Goes through rows
{
for (y = 0; y <= 8; y++) //Goes through columns
col[x][y] = x + 1; // makes the integer value of the matrix equal to the row number + 1
}
for (x = 0; x <= 8; x = x++) //Goes through rows
{
printf("\n"); //print newline
for (y = 0; y <= 8; y = y++) //Goes through columns
printf("%d", originalarray[x][y]); // prints each value of the original matrix in turn
}
printf("\n");
for (x = 0; x <= 8; x++) //Goes through rows
{
for (y = 0; y <= 8; y++) //Goes through rows
{
if(originalarray[x][y] != 0) // if the instant value of the original matrix is not 0, continue
{
a = originalarray[x][y]; //a takes on the insantaneous value of the original matrix
row[x][a - 1] = 0; // removes that value from the corresponding row table
col[y][a - 1] = 0; // removes that value from corresponding column table
}
}
}
printf("\n");
for (x = 0; x <= 8; x = x++) //Goes through rows
{
printf("\n"); //print newline
for (y = 0; y <= 8; y = y++) //Goes through columns
printf("%d", row[x][y]); // prints each value of the row matrix in turn
}
printf("\n");
for (x = 0; x <= 8; x = x++) //Goes through rows
{
printf("\n"); //print newline
for (y = 0; y <= 8; y = y++) //Goes through columns
printf("%d", col[x][y]); // prints each value of the column matrix in turn
}
printf("\n");
}
Note that in the first for loop, scanf is commented out and the matrix is just set to take the value of the column.
i.e the matrix will look like this:
Code:
012345678
012345678
012345678
012345678
012345678
012345678
012345678
012345678
012345678
the output i'm getting is fine for that matrix:
Code:
012345678
012345678
012345678
012345678
012345678
012345678
012345678
012345678
012345678
000000009
000000009
000000009
000000009
000000009
000000009
000000009
000000009
000000009
111111111
022222222
303333333
440444444
555055555
666606666
777770777
888888088
999999909
but when i uncomment scanf and try to input my own matrix - tedious value by value it doesn't work and i get this:
Code:
0
2
0
5
0
0
6
4
0
7
0
0
0
0
0
5
0
0
0
3
0
2
0
8
0
0
7
4
0
0
0
0
7
2
9
0
2
8
0
0
0
0
0
6
3
0
7
3
9
0
0
0
0
4
6
0
0
8
0
1
0
7
0
0
0
5
0
0
0
0
0
6
0
9
7
0
0
2
0
8
0
000007750287368253722127749105091836272709
000000000
000587202560129729336117002087201967202404942743655976237088
71145380912808591444129248591187996134513180013451310932
0116710011315641073831960410738326681297333612974256134513147
31292064613451314724641422134513044-10737580761297417621073832720
4118687709151090-1073758076000
024641422-10737579521297384000116346810738319600
6134519116-107375804013451328900-10737580081345145901292227
Segmentation fault (core dumped)
the big long line of numbers is me trying to input the matrix value by value.
it should look like this:
Code:
020500640
700000500
030208007
400007290
280000063
073900004
600801070
005000006
097002080
I would really appreciate if someone could have a brief look at this. I know you must enjoy the challenge!!
I don't want to be told how to solve the problem, just whats wrong with my code.
Although, suggestions on how to read the matrix in from a file would be cool.
Thank you in advance.
Simon.