Homework help: Sudoku and "explosive recursion"

This is a discussion on Homework help: Sudoku and "explosive recursion" within the C Programming forums, part of the General Programming Boards category; So for my homework, one of my problems asks me to write a program that reads a text file and ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    16

    Unhappy Homework help: Sudoku and "explosive recursion"

    So for my homework, one of my problems asks me to write a program that reads a text file and determines if it is a correct solution to a sudoku puzzle. I'm not totally sure what to do.

    I think I'm supposed to write it to an array then work from there using if statements, but how do I take from the text file and put it to a 2 dimensional 9x9 integer array?

    The second problem asks to do an Ackerman function, I don't really know what that is, my professor gave us an example of one using n and m, and asks to print the number k which doesn't appear in the function.

    This doesn't make much sense. All help would be appreciated.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This sounds a whole lot like a case where you would be smartest to go to your professor or ta and seek clarification.

    Our thing here is helping with problems in C source code... not sorting out homework assignments.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    I can work out the second one with help, all it takes is asking him to explain the function to me.

    The sudoku one I understand already for the most part, all I need to know is how to take numbers from a text file and put it into a 9x9 array. I'm thinking I should use fopen, fscanf, and an embedded loop....does that seem about right?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    We can give hints about the first one. Lots of ways to read from a file, but for this case using fgetc() makes sense since it reads a single character at a time - which coincidentally is what each entry of your array will hold. Since you have a 2-d array, using two nested loops seems like a natural fit.

    One potential gotcha - the character '0' isn't the same as an integer value 0, so you'll have to figure out some conversion there. Typically people map '0' to 0, '1' to 1, '2' to 2 and so on. Using this idea, and the fact that '0' through '9' are sequential values and the concept that you can do math on character values such as '0' (and remembering that x-x == 0) should get you there.

    Yes, this is intentionally vague but you'll never learn C if you don't figure these things out on your own.
    Last edited by KCfromNC; 11-22-2011 at 08:55 AM. Reason: fgetc, not fgets (dumb typo)

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by green_ghoul View Post
    I can work out the second one with help, all it takes is asking him to explain the function to me.

    The sudoku one I understand already for the most part, all I need to know is how to take numbers from a text file and put it into a 9x9 array. I'm thinking I should use fopen, fscanf, and an embedded loop....does that seem about right?
    Yep... it's pretty close. Depending on the layout of the file you may need 2 nested loops.

    Thing is to spend some time analysing the task before you begin coding... you'll be amazed how much easier it is when you're working to a plan.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, you are on the right track. A Sudoku puzzle should be given in the form of 81 consecutive digits.

    Less fanatic fans sometimes give them in puzzle format:
    123456789
    456789123
    000000000
    000000000
    000000000
    000000000

    etc.

    Where an empty space may be either a . (period) or 0, either one.
    Last edited by Adak; 11-22-2011 at 09:18 AM.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Assuming the OP is correct, he's not looking to solve them, just see if the input board is solved correctly. Two different problems ... or at least one is a subset of the other.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, that's a kill joy, if ever I met one.

    In a small distribution counter array of int's dis[9], you can:
    Code:
    for each row of the puzzle:
    for(each column in the row) {
       dis[puzzle[row][col]]++;
    }
    check dis[] after every row. Any dis[] element != 1 means the puzzle is not solved correctly. You can also sum up every row and column. If it's not equal to 45, then the puzzle is not solved correctly, but this is not absolutely definitive since it can be "spoofed".

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    KC is right, I take an already solved, or attempt at a solution, and determine whether it's correct or not. What is dis[]? Is it just the name of the array I'm going to make?

    Oh, the ackerman and sudoku are two separate problems, sorry if my post led to confusion D;

    Also, this is kind of off-topic, but what is C99? Sometimes when I compile with a for loop it won't work because "i'm not in c99 mode" I'm using code-blocks to code and compile if that says anything.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    dis[] is just short for distribution array. It counts up the number (distribution), of each digit.

    That's what i called it.

    C99 is the newest standard for the C language. Not all compilers have it in their products yet. Yours appears to use a special mode to enable some features, of C99.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    I tried to make my code to write the initial array, but I get an error that says "error: r undeclared, first time use in this function"

    my code is
    Code:
    #include <stdio.h>
    
    int main()
    {
        FILE *fp;
        int arr[9][9];
        fp=fopen("/sudoku1.txt",r);
        while(int x=(fgetc(fp))!=EOF)
        {
            for(int j=0;j<8;j++)
            {
                for(int i=0;i<8;i++)
                {
                    arr[j][i]=x;
                }
            }
        }
        return 0;
    }

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your compiler wants the i,j,x int's declared BEFORE they're used. Make a new line 7 and declare them there.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Your compiler wants the i,j,x int's declared BEFORE they're used. Make a new line 7 and declare them there.
    He might also try... fp = fopen("/sudoku1.txt","r"); ... since that's the variable it's complaining about.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > fp=fopen("/sudoku1.txt",r);
    Do you really store everything in the root of your filesystem?
    That's a good way to trash your system at some point.

    > while(int x=(fgetc(fp))!=EOF)
    Look up operator precedence.
    You're not getting characters, only the boolean result of comparing each char with EOF.

    Plus I'd be surprised if the inline int declaration actually works.
    I only thought it applied to for loops, not while loops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-17-2006, 08:31 AM
  2. A new answer to "Do my homework" threads...
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 04-20-2005, 12:36 PM
  3. incrementation of "Homework" threads
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-06-2004, 09:08 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

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