Loop for 1/3+1/5+1/7...

This is a discussion on Loop for 1/3+1/5+1/7... within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by johnggold tsk, tsk No test for invalid n??. What if n is 0? - loop forever!!! And ...

  1. #16
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Quote Originally Posted by johnggold View Post
    tsk, tsk

    No test for invalid n??. What if n is 0? - loop forever!!!

    And you loop once too many!!!! - requirement is for n - not n+1

    Also ++i - I know you can do this - not usual method - especially when recommending code for a beginner.
    your post is the one that should be 'passed around the office'

  2. #17
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    I suggest you try googling "for loop C++ tutorial"

    1. In every case the syntax i++ not ++i is used. It's not me who is out of touch. Teaching students the difference comes a lot later, and the for loop really isn't the best place to show it for the first time.

    2. The questioner clearly wants to stop a n - as shown in the worked example.

    3. Programming for errors is an essential coding habit. Wikibooks have an excellent tutorial on this - emphasing checks for division by zero, for example.

    See C Programming/Error handling - Wikibooks, collection of open-content textbooks

    4. Apart from silverlight I've yet to see anyone else put up code - it seems that criticising others needs a lot less effort.
    Never re-write code unless the user benefits

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by johnggold View Post
    I suggest you try googling "for loop C++ tutorial"

    1. In every case the syntax i++ not ++i is used. It's not me who is out of touch. Teaching students the difference comes a lot later, and the for loop really isn't the best place to show it for the first time.
    I suggest you read Programming Principles and Practice by Bjarne Stroustrup (the creator of C++) and you will notice that he teaches to use ++i rather than i++. But hey, what does he know compared to you and your 6 staff?

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by johnggold View Post
    I passed your email around the office - it got a good laugh.
    Whether or not that's true, you're really trying to enrage someone there by sying that arent you! How would you feel if someone posted that in reference to you?

    If you had been on this forum very long you would know that the frequent posting of code from beginners that has virtually no indentation, or completely screwed up indentation is a major point of difficulty in both the posters ability to spot their problem, and for us to do the same.

    About ++i, it's not simply that you "can do this", it is a semantically less complex operation which is most preferable when writing code that uses iterators, or anywhere else operator overloading is involved. Some would even agree that it is a better habbit to use preincrement even where it does not matter, and that really a beginner should by taught preincrement rather than postincrement right from the beginning. You see it far more now than you used to because people are starting to wake up to the fact that there are good reasons to prefer it. The times, they are a changin'.
    Last edited by iMalc; 08-13-2010 at 03:39 PM.
    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"

  5. #20
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    I suggest you re-read what I said.

    I said try googling - an independent source - not yours or mine. I did not hold up my team as the arbiters.
    Never re-write code unless the user benefits

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by johnggold
    I suggest you try googling "for loop C++ tutorial"

    1. In every case the syntax i++ not ++i is used. It's not me who is out of touch. Teaching students the difference comes a lot later, and the for loop really isn't the best place to show it for the first time.
    Perhaps you missed darren78's source? Professor Bjarne Stroustrup is certainly involved in teaching students at beginner level, and among his examples is the for loop code in this FAQ: How do I write this very simple program?

    Furthermore, although iMalc has already mentioned it and you may know the reasoning, I suggest that you read this FAQ on Which is more efficient: i++ or ++i?

    As to teaching students the difference: I believe that the difference is often taught when these operators are introduced. If not, it would make sense simply to recommend prefix operator++ over postfix. There is simply no reason to recommend the postfix form over the prefix form for standalone expressions, other than dubious appeals to tradition (and the name "C++", heh).

    Quote Originally Posted by johnggold
    2. The questioner clearly wants to stop a n - as shown in the worked example.
    The questioner clearly stated:
    Quote Originally Posted by fyoung91
    Want it to count 1/3+1/5+1/7...... for a set number of terms
    Your example, after the bug fix, returns 1.0 if the argument is 1, and 1.0/3.0 if the argument is 2. This would seem contrary to the "worked example". Personally, I chose not to bring this up because I feel that the core of the example is the loop, not the specifics of the result, which fyoung91 could be expected to fix himself/herself. But if you want to keep talking about "once too many" and stopping at n, then I must say that my code stops at n (since i == n on its last iteration), but yours gives the wrong result for n=1, and stops at n-1 when n>1 (since i == 2 at the start of your loop).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    Actually, it contains a serious bug which I failed to remove when modifying johnggold's example. pianorain apparently spotted it, and johnggold followed shortly thereafter. The corrected code:
    Yes, I fixed that one in a previous post of mine. I missed that one the first time I glanced at it, then I read johnggold's complaints and went back to verify that the bugs he stated weren't there. I didn't actually look through the code too thoroughly as I didn't expect johnggold to miss on a good opportunity to insult someone's code to feel superior.

    Quote Originally Posted by johnggold View Post
    I work in a team of six, with six very different styles. We all have tuned beautifiers, which we may use occasionally on very complex code - mostly we read each others style with no problem.
    Quote Originally Posted by johnggold View Post
    I suggest you try googling "for loop C++ tutorial"

    1. In every case the syntax i++ not ++i is used. It's not me who is out of touch. Teaching students the difference comes a lot later, and the for loop really isn't the best place to show it for the first time.
    I still don't get it. First of all, how can you use six different styles in the same team? Isn't one of the jobs of the lead coder or whoever is in charge to make sure everyone uses the same style. I'd hate to work on a project where every call to a function you have to look up whether the name is in camel case or with underscores. Or even when you read some code - it looks ludicrous to mix styles, and generally way too hard to see where functions begin or end (ie you can't on first glance even though it should).
    But that's not my point here. You are apparently an expert in reading many different styles. But yet you complain about the ++i syntax? Is there no one in your office that uses the syntax (quite surprisingly, as - as I and several others have stated before - it can be faster than the post-incrementing). But it doesn't matter if nobody in your office uses it: if everyone uses a different style and you have no problem with that, how can you have a problem with such a simplistic thing as this?
    Okay, let's just say you personally don't have a problem with it but it is really for the OP's regard, as it is too hard according to a newbie as you said before as well. Well, right "silverlight"'s [sic] url to Stroustrup's FAQ, which shows that Mr. Stroustrup disagrees. Well, let's see, whose opinion do I regard higher - that of a random Internet troll or the one that made up the programming language used..?
    Tough one.


    Quote Originally Posted by johnggold View Post
    4. Apart from silverlight I've yet to see anyone else put up code - it seems that criticising others needs a lot less effort.
    If you read my post before you should've wondered why I wrote "[sic]" after "silverlight" [sic]. That was because "silverlight" is wrong. And then you probably should have checked out his real name and you would've noticed his name is "laserlight". You didn't, which means you either didn't read my post, didn't understand the "[sic]" after the name, or was so stubborn that you really don't care if you misspell a name.
    Again you criticize criticizing. Yet the first one to criticize code was you about "silverlight" [sic] his code. I think that's fine - if the critics made any sense. Had you said: "your code is wrong[ly copied of mine], because you use n in your loop rather than i", then I completely agreed with your post. However you criticize stuff that, as I've shown before, is even wrong. Except for maybe your opinion on "++i": it's fine if you disagree with that style even though I have no idea what you have to do with this.


    Quote Originally Posted by johnggold View Post
    I suggest you re-read what I said.

    I said try googling - an independent source - not yours or mine. I did not hold up my team as the arbiters.
    You keep doing this. You complain about a dozen things, everybody shows you how wrong you are and then you simply say something like "you're wrong" or "re-read what I said" or "I posted your bull........ post around the office" or something along those lines. You don't even say which of the dozens of points made you disagree with. Next time you post, please tell us what you disagree with. Why don't you quote something you disagree with?


    Quote Originally Posted by iMalc View Post
    Whether or not that's true, you're really trying to enrage someone there by sying that arent you! How would you feel if someone posted that in reference to you?
    I really didn't care. If the post was remotely a WTF I would have. And if he sounded remotely like he knew much about coding. If it was you, laserlight or anyone else on this forum I probably would've cared and I've re-read my message dozens of times until I'd understood what made it bad. But in this case I didn't even take another glance at my post, as he had already spoken too much bull for me to take him seriously. Let him post it around the office - hopefully with a link to this thread, so his coworkers can see how "smart" he really is.
    (Actually, someone should link to this post on the thedailywtf.com sidebar - it's actually quite sidebar worthy imho)

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by EVOEx
    First of all, how can you use six different styles in the same team?
    With respect to indent styles: if the same readable style is consistently used throughout each file, it is generally not a problem. As johnggold mentioned, there is also the option of using code formatters to format the code according to one's preference, though I am not in favour of this as it can pose problems for things like file differencing tools if the file is not saved in a standard format.

    Of course, no code formatter (except maybe a buggy one) is going to format i++ to ++i when the difference might actually matter. Furthermore, it is rude to expect that poorly formatted code posted on a forum must go through such a tool before being inspected, but perhaps we can forgive johnggold for being new to this. (But not for posts made after he reads this post.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by johnggold View Post
    I passed your email around the office - it got a good laugh.
    This thread has left me in tears. Could you post a link to your company website, to cheer me up with a good laugh?
    Last edited by MWAAAHAAA; 08-14-2010 at 11:31 AM.

  10. #25
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I work in a team of six, with six very different styles.
    I feel very sorry for the other 5.

    We do not take kindly to 'leet coderz' around here who think they are better than everyone else. Quite honestly I would rather work with a complete noob that had a great attitude than a seasoned professional with an ego problem. IMO inflated ego's can kill a team, can force bad design decisions, and can ultimately produce lower quality code.
    Last edited by VirtualAce; 08-14-2010 at 12:00 PM.

  11. #26
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    ^^^ this!

  12. #27
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    I think you have it the wrong way round - it's us who feel sorry for you. As a team it's not possible to have an ego - it would soon get knocked out of you. I can only assume that a lot of contributors work on their own, with no-one around to keep them in check.

    With the exception of just a couple of contributors - notably silverlight, who I've seen positively helping on several threads, the rest have the biggest egos we've come across, and between us we subscribe to quite a few forums covering the different languages we use.

    Considering the apparent wealth of knowledge available, I saw scant knowledge on our fflush problem.

    I have an IT graduate coming for final interview on Monday. I set him the problem as a pre-interview test, and got a better response than the so-called professionals on this forum, complete with suggested code workarounds - one of which seems to be succesful.

    silverlight I have to say stands out from the crowd, as the exception to the rule, and I have seen a couple of others providing genuine help to beginners.
    Never re-write code unless the user benefits

  13. #28
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by johnggold View Post
    I can only assume that a lot of contributors work on their own, with no-one around to keep them in check.
    I'm a Senior Software Engineer working for a large company with a couple dozen developers alone. I oversee and mentor probably more of the rest than any of the others senior devs on our team. The skills for helping others learnt here translate to work and vice versa.

    One thing you see on this forum is that whenever anyone joins, they learn a lot in their first few weeks. It doesn't matter who they are, everyone from a total newbie to a veteran of the language all learn a lot of stuff that they thought they knew but didn't. I know a lot of people on here who have come a hell of a long way. Have you ever heard the saying "The more you learn, the more you realise you don't know"? If not, you have much more to learn.

    Quote Originally Posted by johnggold View Post
    As a team it's not possible to have an ego
    You're proven that wrong for us already, stating that you work in a team of six.

    Of all the times you've hinted at your age through your many years of experience, you're only really showing one thing. That with age comes stubbornness!
    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"

  14. #29
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I have an IT graduate coming for final interview on Monday. I set him the problem as a pre-interview test, and got a better response than the so-called professionals on this forum, complete with suggested code workarounds - one of which seems to be succesful.
    Sorry but this so-called professional would never work with you nor have you on a team with your current attitude. You criticize one of our own, who by the way, has been nice enough to ignore your immature bantering and criticizing, and then tell us we are the ones in the wrong? I feel you owe this thread and those in it an apology for your rude behavior. None of us are getting paid to help you out and we don't mind helping. However your attitude stinks and if you keep it you won't be well liked around here.

    Take it down a few notches and chill out. And, oh, think twice before you go insulting someone else's code. Finding problems with it is perfectly ok and that is what this forum is for - but stepping down to the point of insulting the code especially when it is a regular member who helps thousands of people a year is not going to win you any rewards.

    Quite frankly most of us here are professionals and we have a wide range of experiences, talents, and specialties but all of that really doesn't have anything to do with the problem at hand which is: your attitude. We do not tolerate poor attitudes for long and they certainly won't gain you any more help. Being a professional, student, or hobbyist matters little when it comes to netiquette and forum manners. And if it feels like we are ganging up on you...well...we are. We don't take kindly to people who unfairly and unjustly criticize others here.

    My advice would be to forget about who is what and where and just learn some manners. Think before you respond to the next person who willingly posts code to help you.
    Last edited by VirtualAce; 08-14-2010 at 03:48 PM.

  15. #30
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Not to mention can't even bother to get Laserlight's name correct.

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

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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