Thread: if only if

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    2

    Question if only if

    CODE:
    --------------------------------------------------------

    for(add1 = 0; add1 <=25; add1++){
    for(add2 = 0; add2 <=25; add2++){
    for(add3 = 0; add3 <=25; add3++){
    for(add4 = 0; add4 <=25; add4++){
    for(add5 = 0; add5 <=25; add5++){
    for(add6 = 0; add6 <=25; add6++){
    for(add7 = 0; add7 <=25; add7++){
    for(add8 = 0; add8 <=25; add8++){
    for(add9 = 0; add9 <=25; add9++){
    for(add10 = 0; add10 <=25; add10++){
    for(add11 = 0; add11 <=25; add11++){
    for(add12 = 0; add12 <=25; add12++){
    for(add13 = 0; add13 <=25; add13++){
    for(add14 = 0; add14 <=25; add14++){
    for(add15 = 0; add15 <=25; add15++){
    for(add16 = 0; add16 <=25; add16++){
    for(add17 = 0; add17 <=25; add17++){
    for(add18 = 0; add18 <=25; add18++){
    for(add19 = 0; add19 <=25; add19++){
    for(add20 = 0; add20 <=25; add20++){
    for(add21 = 0; add21 <=25; add21++){
    for(add22 = 0; add22 <=25; add22++){
    for(add23 = 0; add23 <=25; add23++){
    for(add24 = 0; add24 <=25; add24++){
    for(add25 = 0; add25 <=25; add25++){
    for(add26 = 0; add26 <=25; add26++){

    if((a + add1) !=
    (a + add2) !=
    (a + add3) !=
    (a + add4) !=
    (a + add5) !=
    (a + add6) !=
    (a + add7) !=
    (a + add8) !=
    (a + add9) !=
    (a + add10) !=
    (a + add11) !=
    (a + add12) !=
    (a + add13) !=
    (a + add14) !=
    (a + add15) !=
    (a + add16) !=
    (a + add17) !=
    (a + add18) !=
    (a + add19) !=
    (a + add20) !=
    (a + add21) !=
    (a + add22) !=
    (a + add23) !=
    (a + add24) !=
    (a + add25) !=
    (a + add26))
    {

    BLABLABLA

    }}}}.....}}}}


    ----------------------------------------------------------------------
    END OF INCOMPLETE NOT SO WORKING CODE

    Where a is the character 'A' (0x41) and add# increments from 0 to 25 (no sh!t)

    I only want the if statement to be true when there aren't any repeats in the letters of the alphabet.

    Ex. when true..

    ABCDEFGHIJKLMNOPQRSTUVWXYZ

    Ex. when false

    AACDEFGHIJKLMNOPQRSTUVWXYZ

    As it turns out the if statement doesn't do anything remotely close to what I want it too. As it turns out the if statement is always true. When it should be almost always false.

    An obvious solution is:

    if ((a + add1) != (a + add2) ||
    (a + add1) != (a + add3) ||
    (a + add1) != (a + add4) ||
    (a + add1) != (a + add5) ||
    (a + add1) != (a + add6) ||
    (a + add1) != (a + add7) || ...... and 20 million more comparisons)

    Does anyone know a shortcut?

    Thanks,
    Newman

    b.t.w. I normally program java and still am not fluent with C. I decided to convert 2 years ago when I realized how useless the language actually is. So if there are really silly mistakes, point those out too.

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    assuming that if () statement and the blah blah blah after it takes .000001 seconds to execute, which there's no way it would be able to do, it would take about 13 trillion times the age of the universe to execute this program.
    hello, internet!

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Why in the name of all that is right and good in C do you need so many nested loops?

    >I only want the if statement to be true when there aren't any repeats in the letters of the alphabet.
    Set up an array holding the alphabet, then loop through your array and check each element of the alphabet with each element of your array. Increment a count and if it goes above 1 then there are duplicates:
    Code:
    #include <stdio.h>
    
    #define INVALID 0
    #define VALID   1
    
    static int no_dup ( char *s )
    {
      int i, j, k;
      char *alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
      for ( i = 0; alph[i] != '\0'; i++ ) {
        for ( j = 0, k = 0; s[j] != '\0'; j++ )
          if ( s[j] == alph[i] )
            k++;
        if ( k > 1 )
          return INVALID;
      }
      return VALID;
    }
    
    int main ( void )
    {
      char s[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
      if ( no_dup ( s ) == VALID )
        (void)puts ( "Good" );
      else
        (void)puts ( "Bad" );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why in the name of all that is right and good in C do you need so many nested loops?
    When I saw that (all the loops), I almost spit tea out my nose. Thanks for the laugh, Edinburgh. Every now and then you just need something like this to cheer you up.

    You could always do:
    Code:
    int valid( char *s )
    {
        char a[256]={0};
        int x;
        for( x = 0; *s && s[x]; x++ )
            if( ++a[s[x]] > 1 )
                return 0;
        return 1;
    }
    Quzah.
    Last edited by quzah; 08-05-2002 at 08:43 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Sorry Edinburgh, but that's one of the funniest things I've seen in awhile
    Why in the name of all that is right and good in C do you need so many nested loops?
    Because nested loops are your friends.

  6. #6
    Sayeh
    Guest
    You do realize, of course, that some compilers have a limit to how far they can nest...?!?

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    Originally posted by Sayeh
    You do realize, of course, that some compilers have a limit to how far they can nest...?!?
    Reading the environmental limits from C99, a conforming compiler must allow at least 127 levels of nested blocks in order to be conforming. This may be larger than that required for C90.

    127 nested blocks, for all but the most insane purposes, is enough. I don't think anyone writing reasonable code would be affected by this limit - algorithms don't tend to require that many nested blocks to be implemented... and I'd hate to maintain such a program.

    Hmm... with my indenting style, that would be a pain in the arse... 126 tabs or so. :P

    Ian Woods

  8. #8
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by hairyian


    Reading the environmental limits from C99, a conforming compiler must allow at least 127 levels of nested blocks in order to be conforming. This may be larger than that required for C90.

    127 nested blocks, for all but the most insane purposes, is enough. I don't think anyone writing reasonable code would be affected by this limit - algorithms don't tend to require that many nested blocks to be implemented... and I'd hate to maintain such a program.

    Hmm... with my indenting style, that would be a pain in the arse... 126 tabs or so. :P

    Ian Woods
    out of curiosity how many spaces do you set yer tab stops to?
    hello, internet!

  9. #9
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I set mine to 3 spaces. Actually I replace the tab with 3 spaces so it appears the same on any editor.

Popular pages Recent additions subscribe to a feed