# I need help with variables, please

Printable View

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 08-16-2007
anon
For one thing, you have a lot of places where you probably mean to test for equality (==) but use the assignment operator (=).

Other than that, attempting something like that without arrays doesn't get you far. I have no idea what this code is attempting to do, but it seems that most of it is just redundant.
• 08-16-2007
Salem
Why do you still have that mass of specific variables?

Here's a short example of what I meant. Try to think about the problem in array terms (using some loops) rather than endlessly copy/pasting increasingly complicated (and buggy) expressions.

Code:

```#include <stdio.h> int sumRow ( int arr[4][4], int row ) {     int sum = 0, col;     for ( col = 0 ; col < 4 ; col++ ) {         sum += arr[row][col];     }     return sum; } int sumCol ( int arr[4][4], int col ) {     int sum = 0, row;     for ( row = 0 ; row < 4 ; row++ ) {         sum += arr[row][col];     }     return sum; } int main ( ) {     int puzzle[4][4] = {         {  1,  2,  3,  4 },         { 11, 12, 13, 14 },         { 21, 22, 23, 24 },         { 31, 32, 33, 34 },     };     printf("&#37;d\n", sumRow(puzzle,2) );     printf("%d\n", sumCol(puzzle,2) );     return 0; } \$ ./a.exe 90 72```
With a few well chosen functions, you should be able to do so much better.
• 08-20-2007
JOCAAN
Hi,
Quote:

Code:

```#include <stdio.h> int sumRow ( int arr[4][4], int row ) {     int sum = 0, col;     for ( col = 0 ; col < 4 ; col++ ) {         sum += arr[row][col];     }     return sum; } int sumCol ( int arr[4][4], int col ) {     int sum = 0, row;     for ( row = 0 ; row < 4 ; row++ ) {         sum += arr[row][col];     }     return sum; } int main ( ) {     int puzzle[4][4] = {         {  1,  2,  3,  4 },         { 11, 12, 13, 14 },         { 21, 22, 23, 24 },         { 31, 32, 33, 34 },     };     printf("%d\n", sumRow(puzzle,2) );     printf("%d\n", sumCol(puzzle,2) );     return 0; }```

I've seen that example and I've some doubts;
sumRow and sumCol should work randomly is'n it? so why fixed numbers in
Code:

```int main ( ) {     int puzzle[4][4] = {         {  1,  2,  3,  4 },         { 11, 12, 13, 14 },         { 21, 22, 23, 24 },         { 31, 32, 33, 34 },     };```
Thank you
• 08-20-2007
Salem
They're fixed, simply to demonstrate the functionality.

You can fill the array with your own random numbers.

Understand that you'll get a lot of "half" answers which you'll be expected to look at, then think about how to apply what you see to your own particular problem.

We can show you the way, but we're not here to do all the work for you.
• 08-23-2007
JOCAAN
That's ok but in array like the follow:
Code:

```int main ( ) {     int puzzle[4][4] = {         {  a,  b,  c , d },         { d,  e,  f,  g },         { h,  i,  j,  k },         { l,  m,  n, o },     };```
(letters can be random numbers between 1 and 4)
I want to know how to to get files, rows and squares contain only once every numbe so I dunno how to do it inside arrays.
Thank you again
• 08-23-2007
Salem
So write a for loop, like I've shown previously, to initialise the array with suitable values.
• 08-23-2007
JOCAAN
Ok. I've thought in exploit that if all rows, columns and squares sum 10 means that in all ones there are once 1,2,3,4.
In order to do that 've thought in the first row, for example.
I follow Salem's advice and I use a loop:
Code:

```int sumRow ( int arr[row][col], int row ) {     int sum = 0, col;     for ( col = 0 ; col < 4 ; col++ ) {  // but it is a column and is not a row isn't it?         sum == 10;  // I don't know what to put in this line     }     return sum; }```
Finally how to represent the array is correct with letters like here?
Code:

```int main ( ) {     int puzzle[4][4] = {         {  a,  b,  c , d },         { d,  e,  f,  g },         { h,  i,  j,  k },         { l,  m,  n, o },     };```
Thank you
• 08-24-2007
dwks
Quote:

Originally Posted by JOCAAN
Ok. I've thought in exploit that if all rows, columns and squares sum 10 means that in all ones there are once 1,2,3,4.
In order to do that 've thought in the first row, for example.
I follow Salem's advice and I use a loop:
Code:

```int sumRow ( int arr[row][col], int row ) {     int sum = 0, col;     for ( col = 0 ; col < 4 ; col++ ) {  // but it is a column and is not a row isn't it?         sum == 10;  // I don't know what to put in this line     }     return sum; }```

Do you want to add each element in arr[][] to sum? In that case, you'll need two loops and a statement like "sum = sum + arr[x][y]" nested inside them.

Quote:

Finally how to represent the array is correct with letters like here?
Code:

```int main ( ) {     int puzzle[4][4] = {         {  a,  b,  c , d },         { d,  e,  f,  g },         { h,  i,  j,  k },         { l,  m,  n, o },     };```
Thank you
Do you want a char array, not an int one?

In any case, you'd probably use
Code:

```int puzzle[4][4] = {     { 'a', 'b', 'c', 'd'},     /* ... */```
• 11-01-2007
JOCAAN
Hi, finally I've decided to learn about arrays and I've wrote the following code only with arrays.
However I continue getting the same problem. That's more the sudoku stpos running even more often.
I wrote the code here:
Code:

```//sudoku.cpp: #include<stdlib.h> #include<math.h> #include<stdio.h> #include<time.h> #include<algorithm> #include<iostream>   using std::cout;   using std::endl;   using std::cin; void main(){     int ninc=0;         int xval;     int puzzle[4][4];                          srand(time(0)); puzzle[3][3]=0; while(puzzle[3][3]==0) {  // 1st row puzzle[1][1]=(rand()%4)+1; do {         puzzle[1][2]=(rand()%4)+1;  }while(puzzle[1][2]==puzzle[1][1]); { } do {         puzzle[1][3]=(rand()%4)+1;  }while((puzzle[1][3]==puzzle[1][1])||(puzzle[1][3]==puzzle[1][2])); { } do {         puzzle[1][4]=(rand()%4)+1;  }while((puzzle[1][4]==puzzle[1][1])||(puzzle[1][4]==puzzle[1][2])||(puzzle[1][4]==puzzle[1][3])); { } // Column A do {         puzzle[2][1]=(rand()%4)+1;  }while((puzzle[2][1]==puzzle[1][1])||(puzzle[2][1]==puzzle[1][2])); { } do {         puzzle[3][1]=(rand()%4)+1;  }while((puzzle[3][1]==puzzle[1][1])||(puzzle[3][1]==puzzle[2][1])); { } do {         puzzle[4][1]=(rand()%4)+1;  }while((puzzle[4][1]==puzzle[1][1])||(puzzle[4][1]==puzzle[1][2])||(puzzle[4][1]==puzzle[3][1])); { } // Column D do {         puzzle[2][4]=(rand()%4)+1;  }while((puzzle[2][4]==puzzle[1][3])||(puzzle[2][4]==puzzle[1][4])||(puzzle[2][4]==puzzle[2][1])); { } do {         puzzle[3][4]=(rand()%4)+1;  }while((puzzle[3][4]==puzzle[1][4])||(puzzle[3][4]==puzzle[2][4])||(puzzle[3][4]==puzzle[3][1])); { } do {         puzzle[4][4]=(rand()%4)+1;  }while((puzzle[4][4]==puzzle[1][4])||(puzzle[4][4]==puzzle[2][4])||(puzzle[4][4]==puzzle[3][4])||(puzzle[4][4]==puzzle[4][1])); { } // 4th row do {         puzzle[4][2]=(rand()%4)+1;  }while((puzzle[4][2]==puzzle[3][1])||(puzzle[4][2]==puzzle[4][1])||(puzzle[4][2]==puzzle[1][2])||(puzzle[4][2]==puzzle[4][4])); { } do {         puzzle[4][3]=(rand()%4)+1;  }while((puzzle[4][3]==puzzle[3][4])||(puzzle[4][3]==puzzle[4][4])||(puzzle[4][3]==puzzle[1][3])||(puzzle[4][3]==puzzle[4][1])||(puzzle[4][3]==puzzle[4][2])); { } // Central square do {         puzzle[2][2]=(rand()%4)+1;  }while((puzzle[2][2]==puzzle[1][1])||(puzzle[2][2]==puzzle[1][2])||(puzzle[2][2]==puzzle[2][1])||(puzzle[2][2]==puzzle[2][4])||(puzzle[2][2]==puzzle[4][2])); { } do {         puzzle[2][3]=(rand()%4)+1;  }while((puzzle[2][3]==puzzle[1][3])||(puzzle[2][3]==puzzle[1][4])||(puzzle[2][3]==puzzle[2][1])||(puzzle[2][3]==puzzle[2][2])||(puzzle[2][3]==puzzle[2][4])||(puzzle[2][3]==puzzle[4][3])); { } do {         puzzle[3][2]=(rand()%4)+1;  }while((puzzle[3][2]==puzzle[3][1])||(puzzle[3][2]==puzzle[4][1])||(puzzle[3][2]==puzzle[4][2])||(puzzle[3][2]==puzzle[1][2])||(puzzle[3][2]==puzzle[2][2])||(puzzle[3][2]==puzzle[3][4])); { } do {         puzzle[3][3]=(rand()%4)+1;  }while((puzzle[3][3]==puzzle[4][3])||(puzzle[3][3]==puzzle[4][4])||(puzzle[3][3]==puzzle[3][4])||(puzzle[3][3]==puzzle[1][3])||(puzzle[3][3]==puzzle[2][3])||(puzzle[3][3]==puzzle[3][1])||(puzzle[3][3]==puzzle[3][2])); { } if (puzzle[3][3]!=0)   {     break;   } } // It shows all sudoku cout <<""<<endl; cout <<"Sudoku"<<endl; cout <<puzzle[1][1]<<" "<<puzzle[1][2]<<" "<<puzzle[1][3]<<" "<<puzzle[1][4]<<endl; cout <<puzzle[2][1]<<" "<<puzzle[2][2]<<" "<<puzzle[2][3]<<" "<<puzzle[2][4]<<endl; cout <<puzzle[3][1]<<" "<<puzzle[3][2]<<" "<<puzzle[3][3]<<" "<<puzzle[3][4]<<endl; cout <<puzzle[4][1]<<" "<<puzzle[4][2]<<" "<<puzzle[4][3]<<" "<<puzzle[4][4]<<endl; cout <<""<<endl; cout <<puzzle[1][1]<<" "<<puzzle[1][2]<<" "<<puzzle[1][3]<<" "<<puzzle[1][4]<<endl; cout <<puzzle[2][1]<<" "<<"x"<<" "<<puzzle[2][3]<<" "<<puzzle[2][4]<<endl; cout <<puzzle[3][1]<<" "<<puzzle[3][2]<<" "<<"y"<<" "<<puzzle[3][4]<<endl; cout <<"z"<<" "<<puzzle[4][2]<<" "<<puzzle[4][3]<<" "<<puzzle[4][4]<<endl; cout <<""<<endl; while (xval!=puzzle[2][2]){cout << "x?\n";     cin >> xval;   if (xval==puzzle[2][2]) {                 cout << "OK\n";       break;   } } cout <<""<<endl; while (xval!=puzzle[3][3]){cout << "y?\n";     cin >> xval;   if (xval==puzzle[3][3]) {                 cout << "OK\n";       break;   } } cout <<""<<endl; while (xval!=puzzle[4][1]){cout << "z?\n";     cin >> xval;   if (xval==puzzle[4][1]) {                 cout << "OK\n";       break;   } } cout <<""<<endl; }```
Thank you 4 help
• 11-01-2007
Daved
>> int puzzle[4][4];
Here you are creating a 4x4 two dimensional array. The valid indexes are 0, 1, 2 and 3. Your code uses indexes 1, 2, 3 and 4. Switch your indexes to 0, 1, 2 and 3 and then see if it helps.
• 11-01-2007
hk_mp5kpdw
Agggghhh!
• 11-01-2007
Salem
Indeed hk_mp5kpdw!

@JOCAAN
What the hell happened to the loops and functions?

You've hard-coded your 4x4 answer, but what about a 5x5?
If you've used loops properly, it would be a 1-minute change and recompile, but instead you're looking at rewriting the code.
• 11-01-2007
JOCAAN
Hi, I've tried to Switch my indexes to 0, 1, 2 and 3 but it haven't worked. Here's my code:
Code:

```//sudoku.cpp: #include<stdlib.h> #include<math.h> #include<stdio.h> #include<time.h> #include<algorithm> #include<iostream>   using std::cout;   using std::endl;   using std::cin; void main(){     int ninc=0;         int xval;     int puzzle[4][4];                          srand(time(0)); puzzle[2][2]=0; while(puzzle[2][2]==0) {  // 1st row puzzle[0][0]=(rand()%4)+1; do {         puzzle[0][1]=(rand()%4)+1;  }while(puzzle[0][1]==puzzle[0][0]); { } do {         puzzle[0][2]=(rand()%4)+1;  }while((puzzle[0][2]==puzzle[0][0])||(puzzle[0][2]==puzzle[0][1])); { } do {         puzzle[0][2]=(rand()%4)+1;  }while((puzzle[0][3]==puzzle[0][0])||(puzzle[0][3]==puzzle[0][1])||(puzzle[0][3]==puzzle[0][2])); { } // Column A do {         puzzle[1][0]=(rand()%4)+1;  }while((puzzle[1][0]==puzzle[0][0])||(puzzle[1][0]==puzzle[0][1])); { } do {         puzzle[2][0]=(rand()%4)+1;  }while((puzzle[2][0]==puzzle[0][0])||(puzzle[2][0]==puzzle[1][0])); { } do {         puzzle[3][0]=(rand()%4)+1;  }while((puzzle[3][0]==puzzle[0][0])||(puzzle[3][0]==puzzle[0][1])||(puzzle[3][0]==puzzle[2][0])); { } // Column D do {         puzzle[1][3]=(rand()%4)+1;  }while((puzzle[1][3]==puzzle[0][2])||(puzzle[1][3]==puzzle[0][3])||(puzzle[1][3]==puzzle[1][0])); { } do {         puzzle[2][3]=(rand()%4)+1;  }while((puzzle[2][3]==puzzle[0][3])||(puzzle[2][3]==puzzle[1][3])||(puzzle[2][3]==puzzle[2][0])); { } do {         puzzle[3][3]=(rand()%4)+1;  }while((puzzle[3][3]==puzzle[0][3])||(puzzle[3][3]==puzzle[1][3])||(puzzle[3][3]==puzzle[2][3])||(puzzle[3][3]==puzzle[3][0])); { } // 4th row do {         puzzle[3][1]=(rand()%4)+1;  }while((puzzle[3][1]==puzzle[2][0])||(puzzle[3][1]==puzzle[3][0])||(puzzle[3][1]==puzzle[0][1])||(puzzle[3][1]==puzzle[3][3])); { } do {         puzzle[3][2]=(rand()%4)+1;  }while((puzzle[3][2]==puzzle[2][3])||(puzzle[3][2]==puzzle[3][3])||(puzzle[3][2]==puzzle[0][2])||(puzzle[3][2]==puzzle[3][0])||(puzzle[3][2]==puzzle[3][1])); { } // Central square do {         puzzle[1][1]=(rand()%4)+1;  }while((puzzle[1][1]==puzzle[0][0])||(puzzle[1][1]==puzzle[0][1])||(puzzle[1][1]==puzzle[1][0])||(puzzle[1][1]==puzzle[1][3])||(puzzle[1][1]==puzzle[3][1])); { } do {         puzzle[1][2]=(rand()%4)+1;  }while((puzzle[1][2]==puzzle[0][2])||(puzzle[1][2]==puzzle[0][3])||(puzzle[1][2]==puzzle[1][0])||(puzzle[1][2]==puzzle[1][1])||(puzzle[1][2]==puzzle[1][3])||(puzzle[1][2]==puzzle[3][2])); { } do {         puzzle[2][1]=(rand()%4)+1;  }while((puzzle[2][1]==puzzle[2][0])||(puzzle[2][1]==puzzle[3][0])||(puzzle[2][1]==puzzle[3][0])||(puzzle[2][1]==puzzle[0][1])||(puzzle[2][1]==puzzle[1][1])||(puzzle[2][1]==puzzle[2][3])); { } do {         puzzle[2][2]=(rand()%4)+1;  }while((puzzle[2][2]==puzzle[3][2])||(puzzle[2][2]==puzzle[3][3])||(puzzle[2][2]==puzzle[2][3])||(puzzle[2][2]==puzzle[0][2])||(puzzle[2][2]==puzzle[1][2])||(puzzle[2][2]==puzzle[2][0])||(puzzle[2][2]==puzzle[2][1])); { } if (puzzle[2][2]!=0)   {     break;   } } // It shows all the sudoku cout <<""<<endl; cout <<"Sudoku"<<endl; cout <<puzzle[0][0]<<" "<<puzzle[0][1]<<" "<<puzzle[0][2]<<" "<<puzzle[0][3]<<endl; cout <<puzzle[1][0]<<" "<<puzzle[1][1]<<" "<<puzzle[1][2]<<" "<<puzzle[1][3]<<endl; cout <<puzzle[2][0]<<" "<<puzzle[2][1]<<" "<<puzzle[2][2]<<" "<<puzzle[2][3]<<endl; cout <<puzzle[3][0]<<" "<<puzzle[3][1]<<" "<<puzzle[3][2]<<" "<<puzzle[3][3]<<endl; cout <<""<<endl; cout <<puzzle[0][0]<<" "<<puzzle[0][1]<<" "<<puzzle[0][2]<<" "<<puzzle[0][3]<<endl; cout <<puzzle[1][0]<<" "<<"x"<<" "<<puzzle[1][2]<<" "<<puzzle[1][3]<<endl; cout <<puzzle[2][0]<<" "<<puzzle[2][1]<<" "<<"y"<<" "<<puzzle[2][3]<<endl; cout <<"z"<<" "<<puzzle[3][1]<<" "<<puzzle[3][2]<<" "<<puzzle[3][3]<<endl; cout <<""<<endl; while (xval!=puzzle[1][1]){cout << "x?\n";     cin >> xval;   if (xval==puzzle[1][1]) {                 cout << "OK\n";       break;   } } cout <<""<<endl; while (xval!=puzzle[2][2]){cout << "y?\n";     cin >> xval;   if (xval==puzzle[2][2]) {                 cout << "OK\n";       break;   } } cout <<""<<endl; while (xval!=puzzle[3][0]){cout << "z?\n";     cin >> xval;   if (xval==puzzle[3][0]) {                 cout << "OK\n";       break;   } } cout <<""<<endl;```
In the other hand, how to get all rows, columns and squares contain 1,2,3 and 4 once with loops in order to make the sudoku easier to recompile in 5x5, 9x9...
I 've thought about it but I can't work it out at all.
• 11-03-2007
dwks
You shouldn't have code like this.
Code:

```while((puzzle[0][2]==puzzle[0][0])||(puzzle[0][2]==puzzle[0][1])); while((puzzle[3][2]==puzzle[2][3])||(puzzle[3][2]==puzzle[3][3])\     ||(puzzle[3][2]==puzzle[0][2])||(puzzle[3][2]==puzzle[3][0])\     ||(puzzle[3][2]==puzzle[3][1])); while((puzzle[2][2]==puzzle[3][2])||(puzzle[2][2]==puzzle[3][3])\     ||(puzzle[2][2]==puzzle[2][3])||(puzzle[2][2]==puzzle[0][2])\     ||(puzzle[2][2]==puzzle[1][2])||(puzzle[2][2]==puzzle[2][0])\     ||(puzzle[2][2]==puzzle[2][1])```
It's just too hard to read. How would you be able to tell if you made a mistake in that?

See if you can use some for loops or something instead.

Code:

`cout <<""<<endl;`
You can use just
Code:

`cout << endl;`
• 12-08-2007
JOCAAN
Hi,
Code:

```// Sudoku #include<stdio.h> #include<stdlib.h> #include<time.h> #include<iostream.h> #define MAX_ROW 10 #define MAX_COLUMN 10 void main(){     double a[MAX_ROW][MAX_COLUMN];         int dimensions=0;     int i,j;         int q;     int suma(int); cout <<"Enter how many rows or columnos there are in the sudoku\n";     cin >> dimensions; q=dimensions/2; while ((a[i][0]=!suma(dimensions))||(a[0][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][0]==suma(dimensions))&&(a[0][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;                 } } } //factorial of dimensions int suma(int dimensions){     if (dimensions<2) return dimensions;     return dimensions+suma(dimensions-1); }```
I've used sum of all members of a box, row or column should be sudoku's dimensions factorial.
However I get a runtime error when I run the code.
How to solve it?
Thank you
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last