for loop inquiry

This is a discussion on for loop inquiry within the C Programming forums, part of the General Programming Boards category; Get rid of the file I/O completely, and paste my code exactly, does it still exhibit the infinite loop?...

  1. #16
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Get rid of the file I/O completely, and paste my code exactly, does it still exhibit the infinite loop?

  2. #17
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Don't try subtle, supposedly innocuous changes.
    Code:
    for(i=0;s[i]!=0; i++)
     {
      if(s[i]%3 == 0)
      {
          printf("found %c to be divisible by 3, summing\n", s[i]);
    	  fprintf(f, "found %c to be divisible by 3, summing\n", s[i]);	  
       i = s[i] - '0';
       sum = sum + i;
      }
     }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #18
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    What the heck.

    I've attached my entire project.

    Since the forum doesn't allow .zip extensions, you'll have to rename the file to kin_03.zip after you download it.
    Last edited by cunnus88; 02-04-2008 at 12:12 AM.

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    But still, I think you want to check whether the actual digit is divisible by 3, not simply the character representing it.
    Code:
    #include <stdio.h>
    
    int i_summultofthree(int i)
    {
       char s[256];
       int sum = 0;
       sprintf(s, "%d", i);
       for ( i = 0; s[i] != 0; ++i )
       {
          int digit = s[i] - '0';
          if ( digit % 3 == 0 )
          {
             sum += digit;
          }
       }
       return sum;
    }
    
    int main()
    {
       int i = 123456789;
       printf("i_summultofthree(%d) = %d\n", i, i_summultofthree(i));
       return 0;
    }
    
    /* my output
    i_summultofthree(123456789) = 18
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    True, missed that, but it wouldn't be what results in the infinite loop

  6. #21
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Here is the output I got when compiling your main.cpp.

    Code:
    123456789found 3 to be divisible by 3, summing
    found 6 to be divisible by 3, summing
    found 9 to be divisible by 3, summing
    18
    Sent from my iPadŽ

  7. #22
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I have no idea what's going on, but Sinkula's code worked for me.

    And while your suspicions are understandable, I think I can manage a simple ctrl-c then ctrl-v.

    And I can assure you, I first tried a simple copy paste execution before I did anything to the code. But cwr's code went through and infinite loop and sinkula's didn't.

  8. #23
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by cwr
    True, missed that, but it wouldn't be what results in the infinite loop
    What's s[10] of a 9-digit number?
    Last edited by Dave_Sinkula; 10-20-2005 at 10:32 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #24
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Your point is correct, Dave, but why did the code compile fine for CRW and me, but not for cunnus?
    Sent from my iPadŽ

  10. #25
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I don't think I've ever gotten so much excitement out of a post before. The speed was almost like being in a chatroom.

    I hope there's a resolution in here somewhere (which I'm not understaning at the moment), but nonetheless, thank you very much, everyone.

  11. #26
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Oh, of course. The reason it is breaking is because you were changing the value of i, which is also your iterator, use a different variable, as Dave does. Sorry.

  12. #27
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Why was it working fine on our systems, though cwr?
    Sent from my iPadŽ

  13. #28
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Because it is relying on values that are unitialised (s[10] and beyond), our systems coincidentally hit a zero byte, but the values were undefined.

  14. #29
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Huh? I'm afraid I don't understand. How could using the iterator in some other portion of the embedded code affect the outcome?

  15. #30
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    UB is UB. Sometimes it gives false positives. I suppose it depends on what was in s[] on your systems, since it was unitialized. The (un)fortunate observer may have had null characters to break the loop.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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

Similar Threads

  1. GNU strip inquiry
    By Chris87 in forum Tech Board
    Replies: 2
    Last Post: 07-05-2009, 08:04 PM
  2. Bitflags inquiry
    By Chris87 in forum C Programming
    Replies: 4
    Last Post: 01-03-2009, 05:22 PM
  3. Teaching myself C, quick inquiry
    By Iconate in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 02:19 PM
  4. Loops: The 'any' inquiry
    By chubbs1900 in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2007, 09:35 AM
  5. inquiry from a hungry mac os x user
    By terabyter in forum C Programming
    Replies: 3
    Last Post: 06-23-2006, 09:04 AM

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