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. 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. 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. 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. 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.

5. Originally Posted by green_ghoul
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. 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.

7. 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. 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. 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. 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. 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. Your compiler wants the i,j,x int's declared BEFORE they're used. Make a new line 7 and declare them there.

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. > 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.