I guess my post #2 from 4 MONTHS ago fell on deaf ears then.
Printable View
I guess my post #2 from 4 MONTHS ago fell on deaf ears then.
Do you never listen?
Don't use void main, and indent that horrible code! I'm going to repeat this to you, what's in my signature:
"Lack of time is not an excuse for poor indenting."
Also
"Indentation is a means to make your code readable and help you find bugs, NOT making the code unreadable and create bugs!"
So indent your code! You should always indent your code when writing it! And you should do it WHEN you are writing it and NOT after.
And get rid of those horrible statements. Make functions instead.
Do this first. Otherwise it looks like we're going to have to keep repeating ourselves to you.
The point of using arrays is that you (virtually) never index them using literals (like 0, 1, 2) and use variables and loops instead (i, j).
What you have is that you have simply replaced the named variables with fixed array elements, gaining nothing.
Sudoku is not quite trivial. May-be you try something simpler first to make sure you understand the ideas behind arrays and loops?
hi,
I've indeed my code and replaced the void for an int.
Do you mean I have to change the four boxes statements for functions? How to put them inside while break loop?Quote:
And get rid of those horrible statements. Make functions instead.
It's correct to put an k inicialized 0 instead "0" here?:Quote:
What you have is that you have simply replaced the named variables with fixed array elements, gaining nothing.
Here is my code indeed:Code:while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){
I continue getting a runtime error (dwin.exe error)Code:// Sudoku
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream.h>
#define MAX_ROWS 10
#define MAX_COLUMNS 10
//Factorial of dimensions
int suma(int dimensions){
if (dimensions<2) return dimensions;
return dimensions+suma(dimensions-1);
}
int main(){
double a[MAX_FILES][MAX_COLUMNES];
int dimensions=0;
int i,j;
int q;
int suma(int);
int k;
cout <<"How may columns or rows there are in the sudoku?\n";
cin >> dimensions;
q=dimensions/2;
k=0;
while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){
//first box
do{
for (i=0;i<q;i++){
for(j=0;j<q;j++){
a[i][j]=(rand()%dimensions)+1;
}
}
}while ((a[i][j])=!suma(dimensions));
//second box
do{
for (i=(q-1);i<dimensions;i++){
for(j=0;j<q;j++){
a[i][j]=(rand()%dimensions)+1;
}
}
}while ((a[i][j])=!suma(dimensions));
//third box
do{
for (i=0;i<q;i++){
for(j=(q-1);j<dimensions;j++){
a[i][j]=(rand()%dimensions)+1;
}
}
}while ((a[i][j])=!suma(dimensions));
//fourth box
do{
for (i=(q-1);i<dimensions;i++){
for(j=(q-1);j<dimensions;j++){
a[i][j]=(rand()%dimensions)+1;
}
}
}while ((a[i][j])=!suma(dimensions));
if ((a[i][k]==suma(dimensions))&&(a[k][j]==suma(dimensions))) {
break;
}
}
// All sudoku is showed
for (i=(q-1);i<dimensions;i++){
for(j=(q-1);j<dimensions;j++){
cout <<""<< a[i,j] <<endl;
}
}
}
Can you give me some help, please?
This shouldn't even compile...
Do you consider this readable?
I don't.Code:while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){
Place function declarations inside header files or at the top of your .cpp files! Don't put them inside functions!Code:int suma(int);
It's just that... look at the code! I'm horrified at how it looks.
You can probably put together many loops into one, as well.
Does your compiler even work? Like Elysia said, this shouldn't compile at all.
>>double a[MAX_FILES][MAX_COLUMNES];
First of all, you most likely meant to say MAX_COLUMNS, as that's what you have defined. But what the heck is MAX_FILES, and even if it did exist, why would you use that as an array size for a bool?
This has a lot of problems. This function is going to keep going on forever, because you call it to return the value. Well, maybe it may stop eventually, but it doesn't look good.Code:int suma(int dimensions){
if (dimensions<2) return dimensions;
return dimensions+suma(dimensions-1);
}
I don't know how you consider this indenting your code. I don't really think this belongs in the C++ forum either, as it's all C.
That is not a factorial function. Factorials involve multiplications, not additions.Quote:
Code://Factorial of dimensions
int suma(int dimensions){
if (dimensions<2) return dimensions;
return dimensions+suma(dimensions-1);
}
I'm with hk_mp5kpdw.
It's at about this point that I'd strongly suggest a career change to the OP.
Is this supposed to be a Sudoku generator, or a Sudoku solver?
The purely random algorithms you seem to be using are a terrible choice anyway. It's as bad as using bogo sort.
You should probably be using backtracking algorithms, which would definitely seem to be far above your current ability levels.
I think you need to pick much easier problems to solve first instead. Can you write a function to reverse an array in-place?