Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-28-2005, 04:54 AM   #1 (permalink)
sugarless
Registered User
 
Join Date: Oct 2005
Posts: 3
sugarless is on a distinguished road
Thumbs up Sudoku checker (Array Help)

lately, care of my dad, i've been addicted to those sudoku puzzle's especially the super size ones.. i completed a C and a java course last year, so i figured hey i'm sure i can code something to help me with these puzzles! wrong...

i know there are simple to advanced little progs for this everywhere, but its an ego thing now.. basically i tackled the problem one way and got completely lost and wanted to jump out the window.. i want to start simple because i've forgotten a lot, especially file i/o..

aim:

basically i want to write a little console prog that will read a file eg. something called sudoku.in, where i can punch in the numbers (9rows, 9columns), either partially finished with 0's for spaces, or completed and either check if its valid, or return co-ordinates where its incorrect..

eg.

060104050
008305600
200000001
800406006
006000300
700901004
500000002
007206900
040508070

i figure i read the input of this file, into a 2dimensional array, and then check for duplicated numbers in rows or colums (ignoring 0's for spaces).. however, i completely forget 2d arrays and its driving me bonkers, because it seems embarassingly easy..

can anybody point me in the right direction.. or some useful code for file io, because the text book i have is completely useless, and the given example with the code gave me a compile error.....
sugarless is offline   Reply With Quote
Old 10-28-2005, 06:19 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Is this instruction of any help ? or perhaps the theory of 2d arrays or theory of matrixes
Else theres the old thread on 2D arrays.

For a quick reference, theres a well documented 2d array program written in Java.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-28-2005, 07:14 AM   #3 (permalink)
sugarless
Registered User
 
Join Date: Oct 2005
Posts: 3
sugarless is on a distinguished road
kind of.. now im a bit confused with the array/matrix differences, and how to check rows/columns.. and also start left corner as 1,1 not 0,0... i think my main problem is reading from a file into the array.. this is what i have so far;

Code:
#include "windows.h"
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"

int main(int argc, char* argv[])

{
	int nSodukuArray[9][9];

	ZeroMemory(nSodukuArray,sizeof(nSodukuArray));

	char sLineBuf[256];

	FILE *f = fopen("soduku.in","rt");

	int row = 0, col = 0;

	while (fgets(sLineBuf,255,f))
	{
		for (col=0; col<9; col++)
		{
			nSodukuArray[row][col] = sLineBuf[col] - '0';
		}
		row++;
	}
	fclose(f);

	for (row = 0; row < 9; row++)
	{
		//...
	}

	for (col = 0; col < 9; col++)
	{
		//...
	}
	return 0;

}
sugarless is offline   Reply With Quote
Old 10-28-2005, 07:47 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
If the file is presented as:
Code:
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
Then you should be able read it in with something like:
Code:
#include <stdio.h>

int main(){
    int nSodukuArray[9][9];
    FILE* fin;
    int i, j, ch;
    if(!(fin=fopen("soduku.in","r"))){
        printf("Error opening file\n");
        return -1;
    }
    for(i=0; i < 9 && !feof(fin); ++i){
        for(j=0; j < 9 && !feof(fin); ++j){
            nSodukuArray[i][j]=fgetc(fin);
            if(nSodukuArray[i][j] == '\n'){
                printf("Error reading from file\n");
                return -1;
            }
        }
        ch=fgetc(fin); /* discard '\n' on the line */
    }
    fclose(fin);
    /* do what ever with the 2d array */
    return 0;
}
Only problem here might be that you get your array mirrored since your columns now appears to be located as rows...
But when doing calculations and displaying that cen be rearranged.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-28-2005, 08:01 PM   #5 (permalink)
sugarless
Registered User
 
Join Date: Oct 2005
Posts: 3
sugarless is on a distinguished road
Code:
#include <fstream>
#include <stdio.h>

int main()
{
    /* sudoku array */
    int a[9][9];
    FILE* fin;
    int i, j, ch;
    if(!(fin=fopen("sudoku.in","r")))
    {
        printf("Error opening file\n");
        return -1;
    }
    /* input loop to read in sudoku array from sudoku.in */
    for(i=0; i < 9 && !feof(fin); ++i)
    {
        for(j=0; j < 9 && !feof(fin); ++j)
        {
            a[i][j] = fgetc( fin ) - '0';
            
            if(a[i][j] == '\n'){
                printf("Error reading from file\n");
                return -1;
        }
    }
        ch=fgetc(fin); /* discard '\n' on the line */
}    
    /* output loop to output the new 9x9 array */
    for (i=0; i < 9; ++i)
    {
        for (j=0; j < 9; ++j)
            printf("a[%d][%d]=%d ", i, j, a[i][j]);
    }
    fclose(fin);
}
return 0;
}
ok thanks! i finally tweaked it so the file io is right, it reads from the file into the array..

how would one go about scanning each row and each column in the array to see if numbers are repeated per row/column..?
sugarless is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array to pointer to pointer frier Standard C, C++ 8 03-25-2005 06:57 PM
Sorting an array of any Comparable objects plague Java 1 03-14-2005 06:24 PM
Simple reverse programm silex Standard C, C++ 3 01-22-2005 08:03 AM
breaking down an array from a form metazai PHP 12 07-09-2004 06:18 AM
adding to an array in php sde PHP 1 06-12-2002 11:04 AM


All times are GMT -8. The time now is 08:41 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting