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; I guess my post #2 from 4 MONTHS ago fell on deaf ears then....

  1. #31
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    I guess my post #2 from 4 MONTHS ago fell on deaf ears then.
    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.

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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?
    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).

  4. #34
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    hi,
    I've indeed my code and replaced the void for an int.
    And get rid of those horrible statements. Make functions instead.
    Do you mean I have to change the four boxes statements for functions? How to put them inside while break loop?
    What you have is that you have simply replaced the named variables with fixed array elements, gaining nothing.
    It's correct to put an k inicialized 0 instead "0" here?:
    Code:
    while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){
    Here is my code indeed:
    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;
    	}
      }
    
    
    }
    I continue getting a runtime error (dwin.exe error)
    Can you give me some help, please?

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    This shouldn't even compile...
    Do you consider this readable?
    Code:
    while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){
    I don't.

    Code:
        int suma(int);
    Place function declarations inside header files or at the top of your .cpp files! Don't put them inside functions!

    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #36
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    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?
    Code:
    int suma(int dimensions){
    
        if (dimensions<2) return dimensions;
        return dimensions+suma(dimensions-1);
    
    }
    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.

    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.
    Last edited by mikeman118; 12-08-2007 at 02:36 PM.

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Quote Originally Posted by mikeman118 View Post
    Code:
    int suma(int dimensions){
    
        if (dimensions<2) return dimensions;
        return dimensions+suma(dimensions-1);
    
    }
    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.
    No, it will stop eventually since it keeps calling itself with dimensions - 1 and returns if dimensions < 2.
    But it doesn't look pretty, I agree.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Code:
    //Factorial of dimensions
    int suma(int dimensions){
    
        if (dimensions<2) return dimensions;
        return dimensions+suma(dimensions-1);
    
    }
    That is not a factorial function. Factorials involve multiplications, not additions.

    I'm with hk_mp5kpdw.
    Quote Originally Posted by hk_mp5kpdw View Post
    Agggghhh!
    It's at about this point that I'd strongly suggest a career change to the OP.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Quote Originally Posted by iMalc View Post
    It's at about this point that I'd strongly suggest a career change to the OP.
    I'm bound to agree.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 3 of 3 FirstFirst 123
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, 01:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  5. Replies: 6
    Last Post: 01-02-2004, 01:01 PM

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