Help with a program

This is a discussion on Help with a program within the C Programming forums, part of the General Programming Boards category; Hi everyone. Im new to C programming and having a trouble with writing a program which will display a suduko ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    38

    Help with a program

    Hi everyone. Im new to C programming and having a trouble with writing a program which will display a suduko square (9 * 9 ints), with a 0 in each square. Then the user should be prompted to enter a number into a square identified by its row and column. Users should be continuously prompted until the square is full. Each time a new number is added, the square should be redrawn.

    I've been trying to do it using 2d arrays and loops - a loop for rows inside a loop for columns, but am not even close to the solution...

    Any ideas how to do this? Much appreciated.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Step 1 - show your current progress. Describe what you get and how it is different from your expectations
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    The one particular problem that I had was how to assign 0 to all the "spaces" in the 9x9 matrix, but with a bit of research i managed to find a solution...

    Now im working on the user prompt part of the program... Will try to do it myself first, if i fail to find the solution, i;ll be back... hehe

    P.S.

    Current code:

    Code:
    #include <stdio.h>
    #define COLUMNS 9
    #define ROWS 9
    
    int main()	{
    
        int sudoku[COLUMNS][ROWS];
        int i, j;
        
        for(i = 0; i < COLUMNS; i++)    {
              for(j = 0; j < ROWS; j++) {
                    sudoku[i][j] = 0;
              }
        }
        
        for(i = 0; i < COLUMNS; i++)    {
              for(j = 0; j < ROWS; j++) {
                    printf("&#37;d", sudoku[i][j]);
              }
              printf("\n");
        }
    }

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    Here's what I came up with so far... Although, it does what it supposed to, kind of, its very buggy and im afraid i dont have enough knowledge at my disposal to fix it... Here's the updated code:

    Code:
    #include <stdio.h>
    #define COLUMNS 9
    #define ROWS 9
    
    int main() {
    	int sudoku[COLUMNS][ROWS];
        int i, j, a, b;
    	int num;
        
        for(i = 0; i < COLUMNS; i++) {
    		for(j = 0; j < ROWS; j++) {
    			sudoku[i][j] = 0;
    		}
        }
    
    	for(a = 0; a < COLUMNS; a++) {
    		for(b = 0; b < ROWS; b++) {
    			printf("Enter column: ");
    			scanf("&#37;d", &i);
    			printf("Enter row: ");
    			scanf("%d", &j);
    			printf("What number? ");
    			scanf("%d", &num);
    			sudoku[i][j] = num;
    			for(i = 0; i < COLUMNS; i++) {
    				for(j = 0; j < ROWS; j++) {
    					printf("%d", sudoku[i][j]);
    				}
    			printf("\n");
    			}
    		}
    	}
    }
    This is by far the hardest program i decided to challenge thus far, so bear with me.
    First of all, for the first row and first column i need to enter two zero's. I understand this is because the array starts with 0 and goes all the way to 8, but when i try to modify the loop to something like this:
    Code:
    for(i = 1; i < (COLUMNS + 1); i++)
    ...it doesnt work...
    Another problem that i found is if i overwrite the same "space" more than once, then the program ends without covering the whole field (i.e. changing ALL zero's to a user desired number)...

    Any advice?
    Last edited by Karpaty; 10-11-2007 at 05:55 PM.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    This is by far the hardest program i decided to challenge thus far, so bear with me.
    First of all, for the first row and first column i need to enter two zero's. I understand this is because the array starts with 0 and goes all the way to 8, but when i try to modify the loop to something like this:

    Code:
    for(i = 1; i < (COLUMNS + 1); i++)
    ...it doesnt work...
    If you do so, consider adding -1 after i when you are accessing your array, i.e
    Code:
    sudoku[i-1][j]
    Another problem that i found is if i overwrite the same "space" more than once, then the program ends without covering the whole field (i.e. changing ALL zero's to a user desired number)...
    It does so because told it to do so... that's it, you'll be ask for ROW*COLUMN (or ROW˛) times to enter a value.. so if you "overwrite" a space, than there will be one space who's still at zero. One way to solve this is by having a better program logic . I'll give you two prototypes, you may want to implement the functions.

    Code:
    #include <stdio.h>
    #define COLUMNS 9
    #define ROWS 9
    
    typedef enum {FALSE = 0, TRUE} Bool;
    /*	Defines a boolean */
    
    void printSudoku(int sudoku[COLUMNS][ROWS]);
    /*	Print to the screen a ROWS by COLUMNS sudoku */
    
    Bool hasZeros(int sudoku[COLUMNS][ROWS]);
    /*	Return TRUE if sudoku has at least one "space" who's
    	zero, else return FALSE */
    
    
    int main() {
        int sudoku[COLUMNS][ROWS];
        int i, j, a, b;
        int num;
        
        for(i = 0; i < COLUMNS; i++) {
            for(j = 0; j < ROWS; j++) {
                sudoku[i][j] = 0;
            }
        }
    
        while (hasZeros(sudoku) == TRUE)
        {
            printf("Enter column: ");
            scanf("%d", &i);
            printf("Enter row: ");
            scanf("%d", &j);
            printf("What number? ");
            scanf("%d", &num);
            sudoku[i][j] = num;
    		
            printSudoku(sudoku);
        }
    }
    There's also better way of doing this, like defining a type "Sudoku", but i won't go into details... except if you ask...

    Last thing, to be more "C correct", you should define your 2x2 array as
    Code:
    int sudoku[ROWS][COLUMNS];    // not int sudoku[COLUMNS][ROWS];

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Getting a good interface for Sudoku, is a challenge. Here's how I did it:

    From the main menu, the user can choose to set up a puzzle board.

    The display now shows a board, with nothing in the squares. Beneath that, is a square indicator: Square: 11 Number:

    The "Square: " and "Number: ", designations, don't move. The number after the square changes with every time the user presses enter: 12, 13, ...19, 21, 22, 23...29 ...99

    If the value input is 0, the square is left blank. If the value is 1 - 9, the square on the drawn board above it, is changed to reflect the new value, and the square number is incremented.

    The board is displayed as:
    -------------------------------------------------
    Row 1: | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
    -------------------------------------------------
    with 8 more rows. (Where the first row has the values here, of 1 through 9.)

    So that's the display I use, and it's all text based but looks sharp due to some trickery

    Perhaps the more important question is however, do you have an algorithm to solve the puzzle, or is that not a part of your program?

    I found the solution to Sudoku to be quite tricky to program.

    You may want to google sudoku and see how some website display their solver.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21