Computer Science Canada Exercise: Beeping! |
Author: | wtd [ Tue May 06, 2008 10:34 pm ] |
Post subject: | Exercise: Beeping! |
In the language of your choosing, write a program that gets a line of input from the user, then print it character by character. After each character is printed make the computer beep, then wait one second. Please state the language you're using. |
Author: | HeavenAgain [ Tue May 06, 2008 10:45 pm ] | ||
Post subject: | Re: Exercise: Beeping! | ||
using java, not sure about which beeping sound...
|
Author: | Zampano [ Tue May 06, 2008 10:50 pm ] | ||||
Post subject: | Re: Exercise: Beeping! | ||||
Can I take this opportunity to ask something? In these for loops, is the value c is to be compared to determined once at the initiation of the for loop, or is it done each time the loop cycles? If it was done in C++:
You would do well to define a const to hold word.size () because you need to call the member method size each time you want to check the condition, creating a redundancy. In Turing, is the value represented by word.size () here created once or many times? |
Author: | HeavenAgain [ Tue May 06, 2008 10:57 pm ] |
Post subject: | RE:Exercise: Beeping! |
Quote: In these for loops, is the value c is to be compared to determined once at the initiation of the for loop, or is it done each time the loop cycles? If it was done in C++: if you are asking about the order of the for loop structure, it goes something like this,
initialize first, then check, if true then run, and then do the 3rd block (normally increment/decrement), then check, then run, if false then stop and move on. and yes, it is better if you use a variable to keep track of length, instead of having to call to another method/function every time, and i think it goes the same for turing as well ![]() Quote: After each character is printed make the computer beep, then wait one second. thats probably the tricky part..... ![]() |
Author: | Zampano [ Tue May 06, 2008 11:13 pm ] | ||
Post subject: | Re: Exercise: Beeping! | ||
Thank heavens and thank you HeavenAgain for the help. I guess I ought to revise my answer to accomodate for my misunderstanding, right?
But since Turing finds the value of length (word) right at the beginning and only at that time, do I need to use that const? I think not. |
Author: | wtd [ Tue May 06, 2008 11:53 pm ] | ||
Post subject: | RE:Exercise: Beeping! | ||
Io
|
Author: | rdrake [ Wed May 07, 2008 1:38 am ] | ||||
Post subject: | Re: Exercise: Beeping! | ||||
This probably counts as more than one line, but here's Ruby:
EDIT: What the hell, here's Python too:
|
Author: | btiffin [ Wed May 07, 2008 2:06 am ] | ||||
Post subject: | Re: Exercise: Beeping! | ||||
REBOL using console beep
REBOL using soundcard
|
Author: | michaelp [ Wed May 07, 2008 4:14 pm ] | ||||
Post subject: | RE:Exercise: Beeping! | ||||
C++:
Without a new letter on every line:
|
Author: | md [ Wed May 07, 2008 4:49 pm ] | ||||
Post subject: | Re: RE:Exercise: Beeping! | ||||
michaelp @ 2008-05-07, 4:14 pm wrote: C++:
Without a new letter on every line:
using std::* is so utterly pointless. Either use "using namespace std;" or just preface everything with "std::". I prefer the latter, though it seems the former is somehow becoming standard practice. |
Author: | michaelp [ Wed May 07, 2008 5:03 pm ] |
Post subject: | RE:Exercise: Beeping! |
I do this sometimes so I know what is in the std namespace. Because sometimes, when books use using namespace std; I'm not really sure if a new concept introduced is in the namespace, so I do that just to remind myself. I will probably stop using std::* and use using namespace std; eventually. |
Author: | rdrake [ Wed May 07, 2008 7:42 pm ] | ||
Post subject: | Re: Exercise: Beeping! | ||
A little C#:
|
Author: | HeavenAgain [ Wed May 07, 2008 8:08 pm ] | ||
Post subject: | Re: Exercise: Beeping! | ||
more c..!
![]() |
Author: | CodeMonkey2000 [ Wed May 07, 2008 8:24 pm ] |
Post subject: | RE:Exercise: Beeping! |
/me waits for zylum to do this in bf..... |
Author: | rizzix [ Wed May 07, 2008 9:15 pm ] | ||
Post subject: | RE:Exercise: Beeping! | ||
|
Author: | rdrake [ Wed May 07, 2008 9:31 pm ] | ||
Post subject: | Re: Exercise: Beeping! | ||
Since there's so much C/C++, I suppose there should be more Python.
|
Author: | rizzix [ Wed May 07, 2008 10:11 pm ] | ||
Post subject: | RE:Exercise: Beeping! | ||
|
Author: | rizzix [ Wed May 07, 2008 10:18 pm ] | ||
Post subject: | RE:Exercise: Beeping! | ||
|
Author: | rizzix [ Wed May 07, 2008 10:20 pm ] |
Post subject: | RE:Exercise: Beeping! |
Hmm why is it that the syntax tags are messing up the "case" of the class names. :s |
Author: | btiffin [ Thu May 08, 2008 12:30 am ] | ||
Post subject: | Re: Exercise: Beeping! | ||
Heres one in SNOBOL4
|
Author: | btiffin [ Thu May 08, 2008 3:38 am ] | ||
Post subject: | Re: Exercise: Beeping! | ||
HeavenAgain @ Wed May 07, 2008 8:08 pm wrote: more c..!
Well, yes there is; look into fgets and realloc (or fread or ...) or loop across getch, and then google "buffer overflow" as to why it is not just a limit the number of characters problem. Code like this is susceptible to attack. Learn early and learn often. Potential buffer overflow in C is bad. Very very bad. Learn to avoid it. Write a safe gets for yourself and then carry it with you through your programming career. Bosses will love you and you will help to make the world a better place for everyone. And anyone reading this and thinking, but stdin only accepts 128 or 256 keystrokes; ponder what happens if someone redirects input from a file with 1000 characters and no newline. Then ponder what might happen if those 1000 characters are hand crafted to look like machine instructions.
![]() Sorry for the interruption, but this is an important thing to learn. And when you do, bosses (and co-workers) really will love you for it. ![]() Cheers |
Author: | wtd [ Thu May 08, 2008 8:36 am ] | ||||
Post subject: | Re: Exercise: Beeping! | ||||
rdrake @ Thu May 08, 2008 8:42 am wrote: A little C#:
|
Author: | btiffin [ Thu May 08, 2008 12:48 pm ] | ||
Post subject: | Re: Exercise: Beeping! | ||
One in D 1.0 (by a D noob)
Dang wtd; this is a fun exercise. Great way to waste away coffee breaks. Edit: trying the syntax tag |
Author: | apomb [ Fri May 09, 2008 12:14 am ] | ||
Post subject: | Re: Exercise: Beeping! | ||
|
Author: | wtd [ Fri May 09, 2008 12:28 am ] | ||
Post subject: | RE:Exercise: Beeping! | ||
Pascal!
|
Author: | apomb [ Fri May 09, 2008 12:40 am ] | ||
Post subject: | RE:Exercise: Beeping! | ||
variation to my previous one:
as you can see in the first one, i forgot to take the comment out when i took out the array and foreach loop. this is what my original idea looked like, and it stil works, its just a little longer |
Author: | wtd [ Fri May 09, 2008 12:46 am ] | ||
Post subject: | RE:Exercise: Beeping! | ||
|
Author: | btiffin [ Fri May 09, 2008 9:07 pm ] | ||
Post subject: | Re: Exercise: Beeping! | ||
The palindrome exercise made me think about Icon, so heres a beeper in Icon
|
Author: | btiffin [ Sat May 10, 2008 7:51 pm ] | ||
Post subject: | Re: Exercise: Beeping! | ||
Here's one if Forth, gforth in particular
|
Author: | michaelp [ Sun May 11, 2008 7:56 am ] |
Post subject: | RE:Exercise: Beeping! |
Some of this code looks so confusing to me! Although Pascal looks very straightforward. ![]() |
Author: | btiffin [ Sun May 11, 2008 11:31 am ] |
Post subject: | Re: RE:Exercise: Beeping! |
michaelp @ Sun May 11, 2008 7:56 am wrote: Some of this code looks so confusing to me! Although Pascal looks very straightforward.
![]() Never fear michealp. ![]() My choice of well-rounded knowledge; Assembler, AWK, bash, BASIC, Logo, Smalltalk, Pascal, Lisp, COBOL, Fortran, Prolog, C, C++, D, Java, Eiffel, Erlang, Python, perl, Ruby, Oz, J, R, SNOBOL, Icon, Forth, REBOL. Those environments cover a lot (but not all) of the bases and paradigms. Given those, picking up the syntax of any other language becomes a lot easier. And those are my personal opinions. Your mileage will vary. For instance; learning Java is far more than just the syntax. There are common mistakes, idioms, thousands of ready made classes and entire libraries to learn on the way to mastery. This applies to most programming environments. This weekend I'm playing in Inform 7 (pretty much programming in English ... and I dozed off too much in English class; relearning basic sentence structure and concise grammar; but it is kinda fun). Luckily, just about every language now has open and free implementations so exploring won't cost you anything but time. Note; my list is fairly long, but I've been doing this for 30 years now. Don't think you need to have all this under your belt the first week. My short list is Assembler, bash, AWK, C, Icon, Forth, REBOL (again a very personal list choice - and I don't do Windows - again personal choice, but it means I can't/don't apply for the plethora of .NET jobs out there). If you ever get bored, check out http://en.wikipedia.org/wiki/Categorical_list_of_programming_languages and try and pick one or two languages from each of the categories (over time - there are 40 of them, last I looked). You don't have to explore the breadth and depth of each, but getting past Hello World and then a small exercise or two should do to make you a world class coder able to take on anything and everything in whatever environment management may throw at you. Cheers |
Author: | michaelp [ Sun May 11, 2008 11:47 am ] |
Post subject: | RE:Exercise: Beeping! |
I know C++. But all of the other stuff is very confusing to me. Thanks for that though + that link. ![]() |
Author: | btiffin [ Wed Jun 11, 2008 10:10 pm ] | ||||
Post subject: | Re: Exercise: Beeping! | ||||
I can't leave this one alone. It has become my language refresher exercise. ![]() This would have looked shorter, but DISPLAY char WITH BELL NO ADVANCING END-DISPLAY was no good OpenCOBOL 1.1 http://www.opencobol.org/
I hope no one minds if I reopened a month-old. And yes COBOL is indented 8 spaces, column 7 is * for comment, and the first 6 spots are reserved for punch card deck numbers. When a coder dropped a box of cards (one line per card, 80 spaces) on the floor before getting them loaded into a reader ... think of playing 52 card pick up with 2000 cards and you have to pick them up in order ... so they "numbered" the lines of source code in the source code, just in case. ![]() And a stupid trivia (myth?) item; the size of a punch card had something to do with the size of the American dollar bill for easy access to cutting machines and boxes. Nowadays COBOL can be freeform, removing the 1-6, 7, 8-72 special columns, making edits easier. OpenCOBOL defaults to FIXED form, unless you
And don't fear the COBOL. Boomers are at 60, the COBOL crunchers get paid top dollar, there are billions of lines of legacy, you may get to play with big-iron on top-secret stuff, and well, it can make for a lucrative gig. OpenCOBOL is free, supports a lot of the COBOL 85 standard, a fair chunk of 2002, other common extensions, links with C; so learning COBOL only costs time. Reserved word heavy the COBOL is. My OC 1.1 build has 510 show up with cobc --list-reserved | wc. To me thats titillating; wanting to know what all the words can help me with or teach me, before I even crack open any libraries. Cheers |
Author: | r691175002 [ Wed Jun 11, 2008 10:48 pm ] | ||
Post subject: | Re: Exercise: Beeping! | ||
Actionscript 3.0:
A pretty roundabout way of doing it, flash is way too high level for this kind of stuff. Also, you cannot generate sounds or output the bell character using only code. |
Author: | alex.john95 [ Thu Jul 31, 2008 11:52 am ] |
Post subject: | RE:Exercise: Beeping! |
Very Funny... |