Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Exercise: Beeping!
Index -> General Programming
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: Tue May 06, 2008 10:45 pm   Post subject: Re: Exercise: Beeping!

using java, not sure about which beeping sound...
Java:
import java.awt.*;
import java.util.*;
public class Beeping {
  public static void main(String args[]) {
    String line = new Scanner(System.in).nextLine();
    for (int i = 0 ; i < line.length(); i++){
      System.out.println(line.charAt(i));
      Toolkit.getDefaultToolkit().beep();
      Thread.sleep(1000);
    }
  }
}
Zampano




PostPosted: Tue May 06, 2008 10:50 pm   Post subject: Re: Exercise: Beeping!

code:
var word: string
get word
for c: 1 .. length (word)
    put word (c)
end for
Music.Sound (400,200)
Music.SoundOff
delay (1000)

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++:
code:
for (int c = 0; c != word.size (); 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?
HeavenAgain




PostPosted: 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 Rolling Eyes

Quote:
After each character is printed make the computer beep, then wait one second.
thats probably the tricky part..... Crying or Very sad
Zampano




PostPosted: 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?
code:
var word:string
get word
for c:1..length (word)
    put word(c)
    Music.Sound(400,200)
    Music.SoundOff
    delay (1000)
end for

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.
wtd




PostPosted: Tue May 06, 2008 11:53 pm   Post subject: RE:Exercise: Beeping!

Io

code:
outputWithBeepingAndDelay := method(msg, delay,
    msg foreach(c,
        (c asCharacter .. "\a") print
        System sleep(delay)
    )
    "" println
)

userInput := File standardInput readLine
delay := 1

outputWithBeepingAndDelay(userInput, delay)
rdrake




PostPosted: Wed May 07, 2008 1:38 am   Post subject: Re: Exercise: Beeping!

This probably counts as more than one line, but here's Ruby:
Ruby:
gets.chomp; scan(/./m).each do |c| print c + "\a"; sleep(1) end


EDIT: What the hell, here's Python too:
Python:
import time
str = raw_input("")
for c in str:
     print c + "\a"
     time.sleep(1)
btiffin




PostPosted: Wed May 07, 2008 2:06 am   Post subject: Re: Exercise: Beeping!

REBOL using console beep
code:
REBOL []

charbeep: func [str] [foreach ch str [prin ch prin #"^G" wait 00:00:01]]
charbeep ask "? "

; As a gui
view layout [field [charbeep get-face face] button "Close" [unview/all halt]]


REBOL using soundcard
code:
REBOL []
; Generate a ping waveform as a binary value:
ping: #{}
for amplitude 200 1 -1 [
    for phase 1 360 16 [
        val: 128 + to-integer 127 * sine phase
        val: amplitude * val / 200
        append ping to-char val
    ]
]
; Set up the sound sample parameters
sample: make sound [
    rate: 44100 / 2
    channels: 1
    bits: 8
    volume: 0.5
    data: #{}
]
append sample/data ping
beep: does [
    sound-port: open sound://
    insert sound-port sample
    wait sound-port
    close sound-port
]

; The task at hand, delay as function arg this time
charbeep: func [str delay] [foreach ch str [prin ch beep wait delay]]
charbeep ask "? " 00:00:01

; As a go function gui
go: does [view layout [field [charbeep get-face face 00:00:01] button "Close" [unview/all halt]]] go
Sponsor
Sponsor
Sponsor
sponsor
michaelp




PostPosted: Wed May 07, 2008 4:14 pm   Post subject: RE:Exercise: Beeping!

C++:
code:
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    cout << "Please enter a word: ";
    string word;
    cin >> word;
    for( int i = 0; i < word.length(); i++ )
    {
        cout << word[i] << "\a" << endl;
    }

    return 0;
}


Without a new letter on every line:

code:
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    cout << "Please enter a word: ";
    string word;
    cin >> word;
    for( int i = 0; i < word.length(); i++ )
    {
        cout << word[i] << "\a";
    }

    return 0;
}
md




PostPosted: Wed May 07, 2008 4:49 pm   Post subject: Re: RE:Exercise: Beeping!

michaelp @ 2008-05-07, 4:14 pm wrote:
C++:
code:
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    cout << "Please enter a word: ";
    string word;
    cin >> word;
    for( int i = 0; i < word.length(); i++ )
    {
        cout << word[i] << "\a" << endl;
    }

    return 0;
}


Without a new letter on every line:

code:
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    cout << "Please enter a word: ";
    string word;
    cin >> word;
    for( int i = 0; i < word.length(); i++ )
    {
        cout << word[i] << "\a";
    }

    return 0;
}

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.
michaelp




PostPosted: 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.
rdrake




PostPosted: Wed May 07, 2008 7:42 pm   Post subject: Re: Exercise: Beeping!

A little C#:
C#:
public class Test
{
    public static void Main(string[] args)
    {
        var line = System.Console.ReadLine();

        foreach (var c in line)
        {
            System.Console.Write(c + "\a");
            System.Threading.Thread.Sleep(1000);
        }
    }
}
HeavenAgain




PostPosted: Wed May 07, 2008 8:08 pm   Post subject: Re: Exercise: Beeping!

more c..!
c:
#include <stdio.h>

int main()
{
    char line[256];
    gets(line);
    char *i = line;
    while(*i != '\0')
    {
            printf("%c\7", *i++);
            sleep(1000);
    }
    return 0;
}
is there a way to get around the line[256]? so that i dont have to limit the number of characters... hmm... Sad
CodeMonkey2000




PostPosted: Wed May 07, 2008 8:24 pm   Post subject: RE:Exercise: Beeping!

/me waits for zylum to do this in bf.....
rizzix




PostPosted: Wed May 07, 2008 9:15 pm   Post subject: RE:Exercise: Beeping!

Haskell:
import Control.Concurrent (threadDelay)

main = getLine >>= mapM_ (\c -> putChar c >> putChar '\a' >> threadDelay 1000000)
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 35 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: