Print a sinusoidal wave (vertically)

C, 74 73 70 69 67 characters

67 character solution with many good ideas from @ugoren & others:

i;main(j){main(poll(printf("%*c|\n",j=21+sin(i++*.1)*20,0),0,50));}

69 character solution with while loop instead of recursion:

i;main(j){while(printf("%*c|\n",j=21+sin(i++*.1)*20,0))poll(0,0,50);}

Approaching perl territory. :)


Mathematica 121 104 80 67 64

n=1;While[0<1,Spacer[70 Sin[n Pi/32]+70]~Print~"|";[email protected]; n++]

sine


APL (35)

(Yes, it does fit in 35 bytes, here's a 1-byte APL encoding)

{∇⍵+⌈⎕DL.05⊣⎕←'|'↑⍨-21+⌈20×1○⍵×.1}1

Explanation:

  • {...}1: call the function with 1 at the beginning
  • 1○⍵×.1: close enough for government work to sin(⍵×π÷30). (1○ is sin).
  • -21+⌈20: normalize to the range 1..40 and negate
  • '|'↑⍨: take the last N characters from the string '|' (which results in a string of spaces with a | at the end
  • ⎕←: display
  • ⌈⎕DL.05: wait 50 ms and return 1. (⎕DL returns the amount of time it actually waited, which is going to be close to 0.05, rounding that value up gives 1).
  • ∇⍵+: add that number (1) to and run the function again.