I need help with variables, please

This is a discussion on I need help with variables, please within the C++ Programming forums, part of the General Programming Boards category; For one thing, you have a lot of places where you probably mean to test for equality (==) but use ...

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    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.

  3. #18
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    Hi,
    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

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    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.

  5. #20
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    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

  6. #21
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    So write a for loop, like I've shown previously, to initialise the array with suitable values.
    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.

  7. #22
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    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

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by JOCAAN View Post
    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.

    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'},
        /* ... */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #24
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    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

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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. #26
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Agggghhh!
    Attached Images Attached Images    
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #27
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    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.

  13. #28
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    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.

  14. #29
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #30
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    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

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Replies: 6
    Last Post: 01-02-2004, 12:01 PM

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