Like Tree2Likes
  • 1 Post By iMalc
  • 1 Post By grumpy

got stuck in simple coding process. how to initialize "any value"

This is a discussion on got stuck in simple coding process. how to initialize "any value" within the C Programming forums, part of the General Programming Boards category; hi guys in order to calculate some logarithm approximation I need to make a variable, say s, search through the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    25

    got stuck in simple coding process. how to initialize "any value"

    hi guys

    in order to calculate some logarithm approximation I need to make a variable, say s, search through the whole integer numbers.

    I believe something like this isn't much of help?

    Code:
    int s;
    for {;;s++){
    
    code...
    
    }
    since the variable needs to be initialized right?

    any ideas how to go through all the numbers?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    All the integers would be
    for ( int i = INT_MIN ; i <= INT_MAX ; i++ )

    But most people trying to evaluate an infinite progression tend to stop after a few iterations anyway.
    Calculating even say the 100th decimal place, when a double has only 15 digits of precision is pointless.
    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. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Salem View Post
    All the integers would be
    for ( int i = INT_MIN ; i <= INT_MAX ; i++ )
    Except that i <= INT_MAX is never false.

    If you don't need negative values then you can declare your variable as unsigned, and then go up to about 4.2 billion rather than only 2.1 billion.
    But really, how high do you need to go?
    Salem likes this.
    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"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,657
    Quote Originally Posted by Salem View Post
    All the integers would be
    for ( int i = INT_MIN ; i <= INT_MAX ; i++ )
    That would cause an infinite loop at best, or undefined behaviour (when an int with value INT_MAX is incremented).

    If you want to do something over all integers that can be represented on your target system, one technique would be

    Code:
    for (i = INT_MIN; ; ++i)    /*  note no termination condition here */
    {
            /* do something with i */
    
        if (i == INT_MAX) break;    /*  But we break out after finishing with INT_MAX */
    }
    This is one of those obscure cases that is difficult to achieve without a goto (a break is a goto by another name).
    Last edited by grumpy; 03-02-2013 at 09:23 PM.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with coding "Random Number Guessing Game" (Beginner)
    By DecoratorFawn82 in forum C++ Programming
    Replies: 13
    Last Post: 03-02-2013, 01:38 AM
  2. "Expressions must have (pointer-to-)" I'm stuck. :(
    By Shmamy in forum C++ Programming
    Replies: 13
    Last Post: 12-07-2011, 12:58 PM
  3. "Organizing" Your Desktop for Coding
    By skim in forum Windows Programming
    Replies: 2
    Last Post: 03-13-2006, 06:52 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

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