News for hackers. Stuff that matters.
Re:For loops help - comp.lang.c by tlrobinson on Wednesday May 12, 2010 @03:55AM (#1340456)
Mostly deobfuscated:

    int putchar ( int ) ;

    int main ( void )
    {
        int z ;
        for (z = 0; z < 90; z++)
        {
            putchar (
                z % 9 + z / 9 > 3 &&
                z % 9 + z / 9 < 14 &&
                z / 9 < z % 9 + 6 &&
                z / 9 > z % 9 - 5 ? '*' : ' ' );

            if (!(z % 9 - 9 + 1))
                putchar ( '\n' );
        };
        putchar ( '\n' ) ;
        return 0;
    }
It's too late at night for me to try to simplify that big expression in the middle...
Re:For loops help - comp.lang.c by mmastrac on Wednesday May 12, 2010 @04:09AM (#1340476)
The big expression in the middle is effectively unpacking a 2D point encoded in z. z % 9 is the x coordinate and z / 9 is the y coordinate:

  x = z % 9;
  y = z / 9;

  putchar (
          x + y > 3 &&
          x + y < 14 &&
          y < x + 6 &&
          y > x - 5 ? '*' : ' ' );

  if (x == 8)
    putchar ( '\n' );

Wolfram Alpha plots this inequality nicely:

http://www.wolframalpha.com/input/?i=x+%2B+y+%3E+3+%26%26+x+...

Re:For loops help - comp.lang.c by Oatmeat on Wednesday May 12, 2010 @04:19AM (#1340487)
The program is drawing on a grid 9 wide by 10 high. The loop is going over each space in this grid. It puts a * in each cell that falls within the equations described by the four inequalities. Here's a slightly more simplified version.

    int putchar ( int ) ;
    
    int main ( void )
    {
        int z;
        int width = 9;
        int height = 10;
    
        for (z = 0; z < width * height; z++)
        {
            int x = z % width;
            int y = z / width;
    
            putchar (
                x + y > 3 &&   /* top left */
                x + y < 14 &&  /* bottom right */
                y < x + 6 &&   /* bottom left */
                y > x - 5      /* top right */
                 ? '*' : ' ' );
    
            if (x == width - 1)
                putchar ( '\n' );
        };
        putchar ( '\n' ) ;
    
        return 0;
    }
It would be clearer to separate the main loop into nested loops for x and y, but I've left it as is to show a closer resemblance to the previous version.

The fun part is that you can now play with the various settings. Try increasing the height and width and scaling the equations appropriately. Replace x by 2 * x or x * x for more interesting shapes. You can bound the picture by any equations you like. Example: http://ideone.com/53Qh3

Re:For loops help - comp.lang.c by johnohara on Wednesday May 12, 2010 @10:30AM (#1341012)
This problem appears in the Deitel and Deitel book "C++ How to Program" as well as in their Java book.

It's a great exercise that challenges CS students every year and does a very good job at teaching nested loops.

Later in the same set of exercises is the four triangles problem that ties increment and decrement operators to the use of nested loops. It's one of my personal favorites.

CS students who legitimately complete these exercises always do well with chapter 4 (Arrays, Simpletron), chapter 5 (pointers) and chapter 6 (structs and classes) and come away with insight and a sense of confidence that echoes for a long time.

It is literally one of those defining problems that separates those who are serious from those who are not.

The responder at comp.lang.c knew that. Kudos to him.

Re:For loops help - comp.lang.c by kmt on Wednesday May 12, 2010 @01:43PM (#1341648)
Exactly. I'm surprised to see many people here saying that the responder was unhelpful or rude. I think that the answer was not only fascinating but also responsible, pedagogical and, in a way, even compassionate.
Re:For loops help - comp.lang.c by cabalamat on Wednesday May 12, 2010 @03:50PM (#1342036)
Not rude, but has waaaay too much time on his hands!
Re:For loops help - comp.lang.c by cabalamat on Wednesday May 12, 2010 @03:51PM (#1342041)
> It is literally one of those defining problems that separates those who are serious from those who are not.

It's up there with FizzBuzz; anyone who can't do it simply isn't up to being a programmer.

Re:For loops help - comp.lang.c by telemachos on Wednesday May 12, 2010 @07:54AM (#1340708)
I subscribed to comp.lang.c for a few months about two years ago when I was taking a course on C. Like a lot of older or more high-volume lists, the regulars are complete characters. Small battles over minor points of style go from 0 to A.E. Housman in no time at all. (If the Housman reference is too odd, substitute Erik Naggum.)

I eventually quit the list because it was too much of a time suck, but it's well worth a half a day swimming through the archives. Heathfield's posts are almost always worth a look.

Re:For loops help - comp.lang.c by ableal on Wednesday May 12, 2010 @02:51PM (#1341865)
go from 0 to A.E. Housman

Piqued. Lessee, http://en.wikipedia.org/wiki/Alfred_Edward_Housman ... colleagues were unnerved by his scathing critical attacks ... they "were often savage in the extreme."

Also, for other reasons: A fellow don described him as being "descended from a long line of maiden aunts".

Re:For loops help - comp.lang.c by telemachos on Wednesday May 12, 2010 @08:15PM (#1342732)
Once upon a time, I was a Classics student, and even in the 1980s and 1990s, people still got a lot of mileage out of Housman stories and quotations. (He died in 1936.)

Apparently, after he died, they found notebooks in his office that he had filled with long elaborate, witty, hurtful insults -- aimed at nobody in particular. When he wrote a review, he would tweak the pre-fab insults as needed and voila. A sick, twisted person, but fascinating. (If you're really curious, check out Tom Stoppard's The Invention of Love.)

Re:For loops help - comp.lang.c by ableal on Thursday May 13, 2010 @08:49AM (#1344038)
Thanks for the pointer. I tried desultorily to get a (net-)look at the Gow text, but no dice. I'll have to leave it to some weekend/vacation time to chase sources.

It's curious that the A.E.H. vinegar does not get wider circulation. My copy of Matthew Parris' Scorn only features a couple of harmless barbs (one the 'malt/Milton' line). There are some scraps over at http://en.wikiquote.org/wiki/Alfred_Edward_Housman , and here http://www.lrb.co.uk/v29/n13/frank-kermode/nothing-for-ever-...

(Also, a passing appreciation by Chesterton, a guide I trust, here http://wikilivres.info/wiki/Autobiography_%28Chesterton%29/C...)

Re:For loops help - comp.lang.c by gursikh on Thursday May 13, 2010 @09:59AM (#1344239)
"Nature, not content with denying to Mr — the faculty of thought, has endowed him with the faculty of writing."

The name was left blank in the original, but was intended to be filled in and used when a suitable subject should turn up.

Re:For loops help - comp.lang.c by ubernostrum on Wednesday May 12, 2010 @03:51AM (#1340451)
Now I'm just waiting for someone to post a Perl version shaped like a camel.
Re:For loops help - comp.lang.c by mst on Wednesday May 12, 2010 @08:50PM (#1342829)
Hm. Or I could take the obsfucated C version, dump it into an Inline::C block and then run it through Acme::EyeDrops. Then we could have a C version shaped like a camel. I am, however, far too tired to actually do so - anybody more awake is welcome to steal the idea ...
Re:For loops help - comp.lang.c by jf on Wednesday May 12, 2010 @04:25AM (#1340497)
If you're too lazy to compile it yourself, you can run the code online: http://ideone.com/QKQG6
Re:For loops help - comp.lang.c by kuszi on Wednesday May 12, 2010 @10:26AM (#1341003)
Thanks!
Re:For loops help - comp.lang.c by RyanMcGreal on Wednesday May 12, 2010 @08:29AM (#1340745)
A one-line solution in Python:

    print '\n'.join(['%s%s%s' % (' '*((9-step)/2), '*'*step, ' '*((9-step)/2)) for step in [1, 3, 5, 7, 9, 9, 7, 5, 3, 1]])
Not diamond shaped, but moderately obfuscated.
Re:For loops help - comp.lang.c by BoppreH on Wednesday May 12, 2010 @04:01PM (#1342062)
I think the hardest part of the problem is defining the formula that generates the number (or position) of asterisks at each line.

And you hard-coded it in a list. I consider this cheating.

Re:For loops help - comp.lang.c by BoppreH on Wednesday May 12, 2010 @04:25PM (#1342140)
Here, a one-liner that makes its own calculations:

  print '\n'.join(max(4-i, i-5)*' ' + (min(i, 9-i)*2+1)*'*' for i in range(0, 10))
Edit: shaved off a few more characters and somehow made it slightly clearer. I would make it diamond shaped, but python will whine because of "invalid indentation".
Re:For loops help - comp.lang.c by paulbaumgart on Wednesday May 12, 2010 @07:27PM (#1342629)
Parameterized!

  def d(n): return '\n'.join(int(max((n-1)/2-i,i-n/2))*' '+(min(i,n-1-i)*2+1)*'*' for i in xrange(0,n))
Re:For loops help - comp.lang.c by BoppreH on Wednesday May 12, 2010 @08:41PM (#1342803)
I have made a version of that myself just before posting here, but I made n be width, not the height. Still an interesting solution.
Re:For loops help - comp.lang.c by rbonvall on Saturday May 15, 2010 @05:35PM (#1350708)
If you enclose the expression with parenthesis, you can indent it however you like:

    print ('\n'
        .join(max
       (4-i, i-5) *
      ' ' + (min(i, 9
     -i) * 2 + 1) * '*'
       for i in range
          (0, 10)
             ))
Re:For loops help - comp.lang.c by joss82 on Thursday May 13, 2010 @10:06AM (#1344263)
Slightly shorter:

print '\n'.join([" "abs(4-i) + ""(9-abs(8-i2)) for i in range(9)])

Re:For loops help - comp.lang.c by BoppreH on Thursday May 13, 2010 @10:27PM (#1346370)
That actually doesn't do the trick because there is only one line 9 asterisks wide when it should be two. I don't think you can accomplish this replacing max/min with any combination of abs.
Re:For loops help - comp.lang.c by joss82 on Friday May 14, 2010 @05:07PM (#1348603)
You're right, but I couldn't correct it right away (because of noprocrast and life). Also, formatting was wrong:

print '\n'.join([" "abs(int(4.5-i)) + ""(10-abs(9-i2)) for i in range(10)])

Re:For loops help - comp.lang.c by donaq on Wednesday May 12, 2010 @03:30AM (#1340417)
That's just cruel. Very clever, though.
Re:For loops help - comp.lang.c by mseebach on Wednesday May 12, 2010 @07:26AM (#1340673)
A bit, perhaps. But the guy asking the question is clearly just offloading his homework to a newsgroup, which is also rude.
Re:For loops help - comp.lang.c by donaq on Wednesday May 12, 2010 @09:47PM (#1342966)
True, true. The response falls somewhere between ignoring him and telling him to run "del ." in C: in terms of cruelty.
Re:For loops help - comp.lang.c by rick_2047 on Wednesday May 12, 2010 @07:36AM (#1340687)
Rude, No. Lazy, yes.
Re:For loops help - comp.lang.c by seabee on Wednesday May 12, 2010 @08:03AM (#1340714)
How is telling a group "I have made no effort to do this task, can you guys do this for me" not rude?
Re:For loops help - comp.lang.c by rick_2047 on Wednesday May 12, 2010 @08:10AM (#1340721)
> I'm having a hard time writing a code for this one. I tried several

> times, but I can't get it to work properly. I live in Japan and I take

> an online C++ course. The instructor lives in the US so whenever I am

> awake, he's asleep. Therefore, I cannot contact him directly when I

> need help. I have to get this program up and running ASAP. I tried

> searching everywhere for help, but there's none except for this group.

> My textbook isn't much of a help either. I WILL GREATLY APPRECIATE THE

> HELP I WILL GET!

This is what he said. And trust me I have seen people struggle with writing a function to add two numbers after taking a college course of 1 year in C. I don't think he didn't try at all.

Re:For loops help - comp.lang.c by mseebach on Wednesday May 12, 2010 @08:26AM (#1340742)
The way he presents the problem gives no point of reference for a nice person to say "oh, I see where you misunderstood what foo does" and make it a learning experience. He could, for example, have included one of the several some-what working attempts.

The only "good" answers to such a request are a: here's the code and b: lesson one, here's how a for-loop works. Since his textbook doesn't help, option b doesn't seem viable.

Rude, lazy, it's a fine line, I suppose. Being lazy in front of strangers you're asking for help is perhaps not rude, but doesn't exactly scream for a super-constructive response.

Re:For loops help - comp.lang.c by stralep on Wednesday May 12, 2010 @09:47AM (#1340911)
His "C: The Complete Reference" textbook?

http://news.ycombinator.com/item?id=1339884

:)

Re:For loops help - comp.lang.c by Silhouette on Wednesday May 12, 2010 @10:52AM (#1341070)
Ah, yes. Another little piece of comp.lang.* lore: the expression "Bullschildt" was not invented for nothing.

See also his annotated version of the official C standard, which at the time it was published contained not only Schildt's comments but also the complete text of the standard itself. The standard was available from the international standards bodies for a small fortune, yet the annotated version was available in bookshops for a modest fraction of the cost. It was widely speculated that the difference in price reflected the value added by Schildt's comments.

Re:For loops help - comp.lang.c by kabdib on Wednesday May 12, 2010 @09:39AM (#1340877)
Perhaps the key is "ASAP" -- he waited until the last minute, then realized he was in trouble with a deadline.

Programming takes time as well as understanding and effort.

[I always finished my assignments early. I learned not to mention this, because I was beset with demands of the "help me with this" nature, where the people needing help really just needed to do the work.

Of course, towards the end of the semester, with a week left and three projects remaining and a /really/ loaded mainframe -- a Univac 1108 in my case -- with turnarond time measured in hours, well, this was beyond pathetic or poignant, it was simple stupidity for them to expect that they could do that much work in so little time.]

Re:For loops help - comp.lang.c by dkarl on Wednesday May 12, 2010 @04:37PM (#1342188)
And trust me I have seen people struggle with writing a function to add two numbers after taking a college course of 1 year in C.

Think about how they got so far while remaining so helpless. If someone convinces you that they're working really hard and trying to be independent, yet their helplessness proves they have a long history of being dependent and getting credit for other people's work, then odds are they're just good actors. Or they're fooling themselves. Either way, don't waste your time.

I know that's harsh on people who actually are trying to turn themselves around, but they should expect to weather some initial skepticism after coasting for so long.

Re:For loops help - comp.lang.c by Silhouette on Wednesday May 12, 2010 @10:47AM (#1341056)
> This is what he said.

The problem is, these groups get dozens of posts like that every week. Most of them are probably just lies, as you can tell because the posters disappear when asked politely to show what they have come up with so far.

There are people on those groups who volunteer their time to help others, but there is only so much time to go around. There are plenty of people who have made a real effort and, per the FAQs, post the results they have so far to demonstrate this. It is entirely fair that the volunteers should want to prioritise helping these people instead.

Re:For loops help - comp.lang.c by helium on Wednesday May 12, 2010 @08:32AM (#1340751)
It's not cruel at all. If the Japanese guy could understand that program, he would have no need to do the C++ course anymore.
Re:For loops help - comp.lang.c by mahmud on Wednesday May 12, 2010 @09:39AM (#1340878)
I am a C programmer and could work through that code with a bit more effort than code I have maintained. However, I have seen some C++ code that baffled me more than this ever would.

C++ != trivially obfuscated C.

Re:For loops help - comp.lang.c by edge17 on Wednesday May 12, 2010 @03:26AM (#1340412)
funny... though that was easier to read and less obfuscated and hideous than code i've encountered in real life.
Re:For loops help - comp.lang.c by confoocious on Wednesday May 12, 2010 @04:53AM (#1340524)
Bleh. Perl + Acme::EyeDrops >> * Nice try though.
Re:For loops help - comp.lang.c by n2linux on Wednesday May 12, 2010 @09:38AM (#1340874)
There goes the rest of my morning...
Re:For loops help - comp.lang.c by huherto on Wednesday May 12, 2010 @12:03PM (#1341287)
A little of topic. I may need to dive back in C++. I knew the language pretty well (circa 1994), the STL was brand new and namespaces were experimental. I don't know what happened after that. Can anyone recommend any online resources to fill the gap? Thanks
Re:For loops help - comp.lang.c by xenthral on Wednesday May 12, 2010 @04:04PM (#1342073)
One of the things you want to hit up right away after you get reacquainted is the boost library: http://www.boost.org/

If you use it well, it does a lot of heavy lifting, adds a lot of safety and expressiveness (boost::bind is just pure magic) that just wasn't there before, and a lot of the boost libraries are going into the standard - and are already implemented in the TR1 namespace.

Re:For loops help - comp.lang.c by eru on Wednesday May 12, 2010 @03:40PM (#1342012)
You could have a look at the "Frequently Questioned Answers" (http://yosefk.com/c++fqa/). But that will probably produce more frustration with C++ than it helps..
Re:For loops help - comp.lang.c by mkeblx on Wednesday May 12, 2010 @03:05PM (#1341909)
What percent of HN users could figure this out without using references? I've been .NET programming for 5+ years and couldn't figure it out giving it a few hours to try.
Re:For loops help - comp.lang.c by hernan7 on Wednesday May 12, 2010 @04:48PM (#1342221)
Wow, how do you even begin to write something so obfuscated? Do people do this by hand, or are there any computer-assisted tools for it? I mean:

   for( z = 002354l / 000644l / 000132l / 000322l / 000374l ; ...
has all the hallmarks of machine-generated code.
Re:For loops help - comp.lang.c by lolcraft on Wednesday May 12, 2010 @04:58PM (#1342246)
Not really. Almost any numbers would do, since z is an int and integer division "a/b" in C means not exactly a/b, but its floor. For instance, "2/2/2".
Re:For loops help - comp.lang.c by pmiller2 on Wednesday May 12, 2010 @07:02PM (#1342576)
Nobody's yet mentioned the poster's very first mistake -- asking for help with C++ in a C newsgroup.
Re:For loops help - comp.lang.c by yurifury on Wednesday May 12, 2010 @06:40AM (#1340630)
That wasn't helpful at all!
Re:For loops help - comp.lang.c by moron4hire on Wednesday May 12, 2010 @10:13AM (#1340972)
Regardless of whether or not the original post was rude is no excuse for a rude answer. Kindness begets kindness, patience begets patience. We have a duty to not escalate stuff like this into shit-storms. If the kid didn't post correctly, then we should answer by asking the right questions to guide him to the correct question. If you can't do that, then just move along, you don't need to post.

I remember being that kid. Well, not quite so bad that I literally wanted someone to write code for me, but I'm sure someone thought my poorly worded questions were asking people to write code for me. How many kids are we scaring away because we're being assholes to them?

Re:For loops help - comp.lang.c by jerf on Wednesday May 12, 2010 @11:51AM (#1341249)
Have you ever hung out on a comp.lang.X site being helpful to people? I acknowledge your description of the POV of the questioner, but from the POV of someone who spends significant time helping out people who have (ahem) "real" problems, see if you're still so polite after your 200th "please solve my homework problem!" post.

To whatever riposte you might be planning, I would point out that I'm not talking about theory, either. Yeah, sure, everyone deserves care and love and ponies. I'm talking about, in practice, would you still be so caring and loving and pony-dispensing after hundreds of these things? If so, you're better than most. (Too much better for me to believe in, in fact.)

Re:For loops help - comp.lang.c by DCoder on Wednesday May 12, 2010 @01:21PM (#1341579)
There's just so many times you can direct people to http://www.catb.org/~esr/faqs/smart-questions.html without losing your sanity. I spent several years trying to help people on a specific topic and eventually gave up because the stream of people who haven't put their brain to use before asking was reaching 90%. And that's on a niche topic, I don't even want to know how much more noise common topics get, and I have great respect for people like Heathfield for not giving up.
Re:For loops help - comp.lang.c by Silhouette on Wednesday May 12, 2010 @10:42AM (#1341040)
> Regardless of whether or not the original post was rude is no excuse for a rude answer.

The answer wasn't rude, it was just unhelpful.

For those not familiar with the culture on comp.lang.c and the various related C and C++ groups, it is very simple: people will generally be helpful in dealing with homework questions as long as the asker has made a genuine effort.

On the other hand, anyone who just reposts homework verbatim with no effort whatsoever to solve the problem first is likely to find themselves given a very clever answer that is technically correct but clearly completely implausible for a beginner, thus making it very obvious to the teacher that the person is cheating on their homework. I recall a similar example where someone asked how to work out factorials in C++, and was presented with a highly efficient answer... because it used template metaprogramming and generated that answer at compile time.

This was certainly clear in the related FAQs last time I checked, so anyone who asks a homework question without making any effort to solve it themselves is not only lazy but also incapable of following basic netiquette. I doubt you'd find many regular posters on those groups who had much sympathy, even though some of those same posters give up a great deal of time to help those who are making a genuine effort to improve.

Re:For loops help - comp.lang.c by foldr on Wednesday May 12, 2010 @03:37PM (#1342009)
>The answer wasn't rude, it was just unhelpful.

Being deliberately unhelpful is rude.

Re:For loops help - comp.lang.c by moron4hire on Thursday May 13, 2010 @12:00AM (#1343242)
So is your time valuable or not? Is your time is too valuable to waste on people who don't put in a genuine effort, or is it not valuable enough that posting an unhelpful answer isn't a waste of effort? That's my point, it's easier for you to just ignore the bad poster. And yes, a deliberately unhelpful answer is rude.
Re:For loops help - comp.lang.c by domdefelice on Wednesday May 12, 2010 @05:42AM (#1340581)
This is too funny xD