The First Two Programs

September 7, 2012

In their book The C Programming Language, Brian Kernigan and Dennis Ritchie say that the first program every programmer should write is a program that writes “Hello, world!” to the console. Then they give the second program that produces a fahrenheit/celsius temperature conversion table, with fahrenheit temperatures every 20 degrees from 0 to 300 and the corresponding celsius temperature to the nearest degree, each pair written on its own line with the two temperatures separated by tabs.

Your task is to write the first two programs. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

About these ads

Pages: 1 2

13 Responses to “The First Two Programs”

  1. David said

    FORTH version. It’s actually a bit tricky to get the rounding correct when using integer math…

    
    : hw
       cr ." Hello, world!" ;
    
    : sgn ( n -- n )
       dup 0< swap 0> - ;
    
    : rounded ( n -- n )
        10 /mod swap  5 over sgn * +  10 / + ;
    
    : temp-table
       301 0 DO
          cr i 3 .r  i 32 - 50 9 */ rounded 8 .r
       20 +LOOP ;
    
    hw
    Hello, world! ok
    temp-table
      0     -18
     20      -7
     40       4
     60      16
     80      27
    100      38
    120      49
    140      60
    160      71
    180      82
    200      93
    220     104
    240     116
    260     127
    280     138
    300     149 ok
    
  2. Paul Hofstra said

    Rather straightforward in Python.

    def hello():
        print 'Hello, world!'
    
    def temp_table():
        for tf in range(0, 301, 20):
            print tf, '\t', int(round((tf - 32) * 5 / 9.0, 0))
    

    This produces:
    >>> hello()
    Hello, world!
    >>> temp_table()
    0 -18
    20 -7
    40 4
    60 16
    80 27
    100 38
    120 49
    140 60
    160 71
    180 82
    200 93
    220 104
    240 116
    260 127
    280 138
    300 149

  3. treeowl said

    Hello world is so dull I decided to do it three ways. It’s still dull.
    The normal Haskell way:

    main = putStrLn "Hello World"

    The silly Haskell way:

    main = mapM_ putChar "Hello, World\n"

    The sillier Haskell way:

    main = foldM (const putChar) () "Hello World\n"

    The temperature one isn’t horrible:

    main = mapM_ putStrLn [show f ++ "\t" ++ show c | counter <- [0..15], let f = counter*20, let c = round $ 5/9 * (fromIntegral $ f-32)]

  4. treeowl said

    Och, I forgot about a nice bit of Haskell syntax that really comes in handy here, making the table slightly shorter and substantially clearer.

    main=mapM_ putStrLn [show f ++ "\t" ++ show c | f<-[0,20..300], let c = round $ 5/9 * (fromIntegral $ f-32)]

  5. treeowl said

    Silly me…. I can make it even slightly clearer:

    main=mapM_ putStrLn [show f ++ "\t" ++ show c | f<-[0,20..300], let c = round $ 5/9 * fromIntegral (f-32)]

  6. treeowl said

    “me” gave a bulky version in Racket. Here’s a non-bulky one:

    (do ((f 0 (+ f 20)))
    ((> f 300))
    (display f)
    (write-char #\tab)
    (display (round (* (/ 5 9) (- f 32))))
    (newline))

  7. [...] doing, right? This time we have another two problems from Programming Praxis, aptly title “The First Two Problems“. The whole thing is based on the idea from Brian Kernighan and Dennis Ritchie that the first [...]

  8. jpverkamp said

    I think I may have overdone it. The problem was easy enough, so I decided to make it a bit harder by drawing the characters to the screen using turtle graphics rather than just printing them. The lion’s share of the time was spent actually making the font, but I think it turned out relatively well.

    Here’s the post: The First Two Problems @ jverkamp.com

  9. Ian said

    (displayln “Hello, world!”)

    ;; An even-less-bulky Racket temperature converter
    (for ([f (in-range 0 301 20)])
    (printf “~a\t~a\n” f (round (* 5/9 (- f 32)))))

  10. A Clojure solution:

    (use 'clojure.contrib.math)
    
    (defn hello-world []
     (println "Hello World"))
    
    (hello-world)
    
    (defn fahrenheit-to-celsius
     [min max step]
      (let [k (/ 5 9) limit (- max step)]
        (loop [f min]
          (printf "%d\t%d\n" f (round (* (- f 32) k)))
          (if (<= f limit)
            (recur (+ f step))))))
    
    (fahrenheit-to-celsius 0 300 20)
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 621 other followers

%d bloggers like this: