C++ code into Ruby, I need it fast, no time for RTFM

Discussion in 'Ruby' started by Andrei Ursan, Apr 25, 2007.

  1. Andrei Ursan

    Andrei Ursan Guest

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    void r_string(int n, int v[])
    {
    int i;
    
    for(i=0;i<n;i++)
    {
    cout<<"v["<<i<<"]=";
    cin>>v[i];
    }
    
    }
    
    void p_string(int n, int v[])
    {
    int i;
    cout<<"\nThe string is : ";
    
    for(i=0;i<n;i++)
    cout<<v[i]<<"  ";
    
    }
    
    float avarage_string(int n, int v[])
    {
    int i,s=0;
    float av;
    for(i=0;i<n;i++)
    s+=v[i];
    
    av=(float)s/n;
    
    return av;
    }
    
    void main()
    {
    clrscr();
    
    int no, a[20];
    cout<<" no = ";
    cin>>no;
    r_string(no,a);
    p_string(no,a);
    
    cout<<"String avarage is : "<<avarage_string(no,a);
    
    getch();
    }
    
    Strings value are read from the keyboard, and i need a getch and clrscr
    method.
    And how can I export the ruby code into *.exe ?

    Can somebody translate this into ruby ? I need it fast...

    --
    Posted via http://www.ruby-forum.com/.
    Andrei Ursan, Apr 25, 2007
    #1
    1. Advertising

  2. On 4/25/07, Andrei Ursan <> wrote:
    > Strings value are read from the keyboard, and i need a getch and clrscr
    > method.
    > And how can I export the ruby code into *.exe ?
    >
    > Can somebody translate this into ruby ? I need it fast...


    DYOH - Do Your Own Homework
    Richard Conroy, Apr 25, 2007
    #2
    1. Advertising

  3. Andrei Ursan

    Andrei Ursan Guest

    Richard Conroy wrote:
    > On 4/25/07, Andrei Ursan <> wrote:
    >> Strings value are read from the keyboard, and i need a getch and clrscr
    >> method.
    >> And how can I export the ruby code into *.exe ?
    >>
    >> Can somebody translate this into ruby ? I need it fast...

    >
    > DYOH - Do Your Own Homework


    It's not homework. I need the code for personal use today. I want to
    learn ruby and if i have that code in ruby i can understand some things.
    I wan to use ruby code 4 some algorithms and if i have that code
    translated i can do it myself the rest of them.

    --
    Posted via http://www.ruby-forum.com/.
    Andrei Ursan, Apr 25, 2007
    #3
  4. Brian Candler, Apr 25, 2007
    #4
  5. Andrei Ursan

    John Joyce Guest

    On Apr 25, 2007, at 6:29 PM, Andrei Ursan wrote:

    >
    Code:
    > #include <iostream.h>
    > #include <conio.h>
    >
    > void r_string(int n, int v[])
    > {
    > int i;
    >
    > for(i=0;i<n;i++)
    >  {
    >  cout<<"v["<<i<<"]=";
    >  cin>>v[i];
    >  }
    >
    > }
    >
    > void p_string(int n, int v[])
    > {
    > int i;
    > cout<<"\nThe string is : ";
    >
    > for(i=0;i<n;i++)
    >     cout<<v[i]<<"  ";
    >
    > }
    >
    > float avarage_string(int n, int v[])
    > {
    > int i,s=0;
    > float av;
    > for(i=0;i<n;i++)
    > s+=v[i];
    >
    > av=(float)s/n;
    >
    > return av;
    > }
    >
    > void main()
    > {
    > clrscr();
    >
    > int no, a[20];
    > cout<<" no = ";
    > cin>>no;
    > r_string(no,a);
    > p_string(no,a);
    >
    > cout<<"String avarage is : "<<avarage_string(no,a);
    >
    > getch();
    > }
    > 
    >
    > Strings value are read from the keyboard, and i need a getch and
    > clrscr
    > method.
    > And how can I export the ruby code into *.exe ?
    >
    > Can somebody translate this into ruby ? I need it fast...
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >

    Translate this for me, right now. No, by yesterday. == A time when
    you pay someone to do it. Or ask much nicer. But if you are trying to
    say that you're going to learn Ruby by comparing that C++ code to
    some Ruby code that accomplishes the same thing, you are either
    fooling us OR fooling yourself. Ruby and C++ are different enough
    that a side-by-side comparison isn't going to teach you much of
    anything in this case. It would more likely just confuse you, friend.
    John Joyce, Apr 25, 2007
    #5
  6. Andrei Ursan

    Andrei Ursan Guest

    > Translate this for me, right now. No, by yesterday. == A time when
    > you pay someone to do it. Or ask much nicer. But if you are trying to
    > say that you're going to learn Ruby by comparing that C++ code to
    > some Ruby code that accomplishes the same thing, you are either
    > fooling us OR fooling yourself. Ruby and C++ are different enough
    > that a side-by-side comparison isn't going to teach you much of
    > anything in this case. It would more likely just confuse you, friend.


    I need that 'cause i want to use ruby for some algorithms, i don't want
    to write them in C i want them in ruby. So if i had that example i can
    do the rest myself.

    --
    Posted via http://www.ruby-forum.com/.
    Andrei Ursan, Apr 25, 2007
    #6
  7. On Wed, 25 Apr 2007 20:44:52 +0900, Andrei Ursan wrote:

    > I need that 'cause i want to use ruby for some algorithms, i don't want
    > to write them in C i want them in ruby. So if i had that example i can
    > do the rest myself.


    Still, being polite does not hurt.
    And not everyone does read C++ fluently.

    As far as I can see, the code is
    concerned with printing some stuff.
    But I have evaded C++ for long
    enough to not be sure about its intention.

    What is it that you're trying to do?

    We can show you how to do this in Ruby,
    which might be surprisingly different from
    a line-by-line translation of the code you posted.

    s
    Stefan Schmiedl, Apr 25, 2007
    #7
  8. Andrei Ursan

    Tim Becker Guest

    > We can show you how to do this in Ruby,
    > which might be surprisingly different from
    > a line-by-line translation of the code you posted.


    But line by line is more fun:

    void p_string(int n, int v[])

    Throw out return type, argument type and give the method a rubyesque name:

    def chunky_chunky_bacon(chunky, bacon)

    {
    lose the burly bracket.

    int i;
    get rid of the local declaration.

    cout<<"\nThe string is : ";

    cout is called STDOUT, you don't need semicolons, and the string
    you're printing isn't ruby enough;

    STDOUT << "\nWelcome to the madhouse :"

    for(i=0;i<n;i++)

    for loop is different

    for ding_dong in 0..chunky-1


    cout<<v<<" ";
    turns into
    STDOUT << bacon[dingdong] << " " # this should look familiar by now
    end

    end the closely curly brace
    }
    becomes:
    end

    Putting it all together:

    void p_string(int n, int v[])
    {
    int i;
    cout<<"\nThe string is : ";
    for(i=0;i<n;i++)
    cout<<v<<" ";
    }

    becomes:

    def chunky_chunky_bacon(chunky, bacon)
    STDOUT << "\nWelcome to the madhouse :"
    for ding_dong in 0..chunky-1
    STDOUT << bacon[ding_dong] << " "
    end
    end



    >
    > s
    >
    >
    Tim Becker, Apr 25, 2007
    #8
  9. Andrei Ursan

    John Joyce Guest

    On Apr 25, 2007, at 10:16 PM, Tim Becker wrote:

    >> We can show you how to do this in Ruby,
    >> which might be surprisingly different from
    >> a line-by-line translation of the code you posted.

    >
    > But line by line is more fun:
    >
    > void p_string(int n, int v[])
    >
    > Throw out return type, argument type and give the method a
    > rubyesque name:
    >
    > def chunky_chunky_bacon(chunky, bacon)
    >
    > {
    > lose the burly bracket.
    >
    > int i;
    > get rid of the local declaration.
    >
    > cout<<"\nThe string is : ";
    >
    > cout is called STDOUT, you don't need semicolons, and the string
    > you're printing isn't ruby enough;
    >
    > STDOUT << "\nWelcome to the madhouse :"
    >
    > for(i=0;i<n;i++)
    >
    > for loop is different
    >
    > for ding_dong in 0..chunky-1
    >
    >
    > cout<<v<<" ";
    > turns into
    > STDOUT << bacon[dingdong] << " " # this should look familiar by now
    > end
    >
    > end the closely curly brace
    > }
    > becomes:
    > end
    >
    > Putting it all together:
    >
    > void p_string(int n, int v[])
    > {
    > int i;
    > cout<<"\nThe string is : ";
    > for(i=0;i<n;i++)
    > cout<<v<<" ";
    > }
    >
    > becomes:
    >
    > def chunky_chunky_bacon(chunky, bacon)
    > STDOUT << "\nWelcome to the madhouse :"
    > for ding_dong in 0..chunky-1
    > STDOUT << bacon[ding_dong] << " "
    > end
    > end
    >
    >
    >
    >>
    >> s
    >>
    >>

    >


    c'est bon!
    John Joyce, Apr 25, 2007
    #9
  10. Andrei Ursan

    Michael Ulm Guest

    Andrei Ursan wrote:
    >
    Code:
    > #include <iostream.h>
    > #include <conio.h>
    >
    > void r_string(int n, int v[])
    > {
    > int i;
    >
    > for(i=0;i<n;i++)
    >  {
    >  cout<<"v["<<i<<"]=";
    >  cin>>v[i];
    >  }
    >
    > }
    >
    > void p_string(int n, int v[])
    > {
    > int i;
    > cout<<"\nThe string is : ";
    >
    > for(i=0;i<n;i++)
    >     cout<<v[i]<<"  ";
    >
    > }
    >
    > float avarage_string(int n, int v[])
    > {
    > int i,s=0;
    > float av;
    > for(i=0;i<n;i++)
    > s+=v[i];
    >
    > av=(float)s/n;
    >
    > return av;
    > }
    >
    > void main()
    > {
    > clrscr();
    >
    > int no, a[20];
    > cout<<" no = ";
    > cin>>no;
    > r_string(no,a);
    > p_string(no,a);
    >
    > cout<<"String avarage is : "<<avarage_string(no,a);
    >
    > getch();
    > }
    > 
    >
    > Strings value are read from the keyboard, and i need a getch and clrscr
    > method.
    > And how can I export the ruby code into *.exe ?
    >
    > Can somebody translate this into ruby ? I need it fast...
    >


    Well, since you ask _that_ nicely...
    This may get you started

    print "number of entries: "
    no = gets.to_i

    a = []
    no.times do |i|
    print "enter item nr. #{i}: "
    a << gets.to_i
    end

    p a

    puts a.inject(0){|s, x| s + x}.to_f / a.size unless a.empty?


    It's rather quick-and-dirty, but then, so was your C++ code.

    HTH,

    Michael

    --
    Michael Ulm
    R&D Team
    ISIS Information Systems Austria
    tel: +43 2236 27551-542, fax: +43 2236 21081
    e-mail:
    Visit our Website: www.isis-papyrus.com

    ---------------------------------------------------------------
    This e-mail is only intended for the recipient and not legally
    binding. Unauthorised use, publication, reproduction or
    disclosure of the content of this e-mail is not permitted.
    This email has been checked for known viruses, but ISIS accepts
    no responsibility for malicious or inappropriate content.
    ---------------------------------------------------------------
    Michael Ulm, Apr 25, 2007
    #10
  11. Andrei Ursan

    Tim Pease Guest

    On 4/25/07, Andrei Ursan <> wrote:
    >
    > Strings value are read from the keyboard, and i need a getch and clrscr
    > method.
    > And how can I export the ruby code into *.exe ?
    >
    > Can somebody translate this into ruby ? I need it fast...
    >


    Have you gotten a suitable answer yet?

    There are several options for you here.
    1) Transliterate into Ruby (as you imply)
    2) Leave your code as-is and wrapper it using Ruby's C api
    3) Leave your code as-is and use the Ruby DL module to load your
    library and invoke the methods
    4) Use the RubyInline module to simply "inline" your C/C++ code into a
    Ruby program

    By far the quickest solution is going to be the RubyInline route.
    You'll need a compiler installed on your machine to handle the C++
    code compilation step RubyInline performs. You can install RubyInline
    via the gem command:

    gem install -r rubyinline

    Shoot a nice note of thanks off to Eric Hodel and Ryan Davis. They've
    done a great job with that little gem.

    Have fun learning Ruby! I've learned far more about programming by
    using Ruby than I ever learned using C/C++ or Java.

    Blessings,
    TwP
    Tim Pease, Apr 25, 2007
    #11
  12. Andrei Ursan

    Andrei Ursan Guest

    doh, sorry to all of you cos i'm not so polite as you want... :( c'est
    la vie;
    some person aren't so polite but that don't means that they don't
    respect your work or help.

    Thanks to all off you who tried to help me :)

    This is what i was trying to do :

    Code:
    def r_string( n, v )
    n.times do |i|
    print "v[#{i}] = "
    v << readline.to_i
    end
    end
    
    def p_string( v )
    print "\nThe string is: " + v.join("  ")
    end
    
    def average_string( v )
    v.inject {|sum, x| sum + x} / v.size.to_f
    end
    
    print "no = "
    no = readline.to_i
    a = []
    r_string( no, a )
    p_string( a )
    
    puts "\nString average is #{average_string(a)}"
    
    readline
    
    Thanks again to those who helped me !!
    Have a nice day !
    (sorry for any speeling mistakes, english isn't my first language )

    --
    Posted via http://www.ruby-forum.com/.
    Andrei Ursan, Apr 25, 2007
    #12
  13. On Thu, 26 Apr 2007 00:16:01 +0900, Andrei Ursan wrote:

    > This is what i was trying to do :


    This is something I can read :)
    You want to calculate the arithmetic mean of some numbers.

    I'd do the following as a basic attempt without any
    bells and whistles:

    1 lines = $stdin.readlines
    2 numbers = lines.collect { |line| line.to_f }
    3 total = numbers.inject(0) { |sum, x| sum + x }
    4 mean = total / numbers.size
    5 puts("Arithmetic mean: #{mean}")

    You could insert some filter for non-parseable input
    between 1 and 2, skip over the unnecessary calculations
    in case there was no input with a conditional between 2 and 3,
    show the entered values like you did between 4 and 5.

    There's no need to "preallocate" arrays, as you can return
    any object in ruby. You can even return multiple objects and
    access them immediately or through the wrapping array.

    HTH,
    s.
    Stefan Schmiedl, Apr 25, 2007
    #13
  14. Andrei Ursan

    Sammy Larbi Guest

    Andrei Ursan wrote, On 4/25/2007 4:29 AM:
    >
    > Can somebody translate this into ruby ? I need it fast...
    >
    >



    I don't mean to be rude, but I'm with most of the others on this. I
    found it amusing (is that mean of me?) that you have no time to RTFM,
    but you have time to wait longer than it would have taken to RTFM for
    someone to answer. You don't need an entire book for loops and printing
    strings. Thats page one stuff in any language, I would think.
    Sammy Larbi, Apr 25, 2007
    #14
  15. Andrei Ursan

    Andrei Ursan Guest

    Sammy Larbi wrote:
    > Andrei Ursan wrote, On 4/25/2007 4:29 AM:
    >>
    >> Can somebody translate this into ruby ? I need it fast...
    >>
    >>

    >
    >
    > I don't mean to be rude, but I'm with most of the others on this. I
    > found it amusing (is that mean of me?) that you have no time to RTFM,
    > but you have time to wait longer than it would have taken to RTFM for
    > someone to answer. You don't need an entire book for loops and printing
    > strings. Thats page one stuff in any language, I would think.


    No, i did rftm and i'm doing it even now :)) :p
    It's ok... I'm fine, don't need to worry 4 me, btw if u have some good
    ruby doc pleas links gimme to me i'm ruby doc hungry.

    --
    Posted via http://www.ruby-forum.com/.
    Andrei Ursan, Apr 25, 2007
    #15
  16. Brian Candler, Apr 25, 2007
    #16
  17. Andrei Ursan

    Ari Brown Guest

    On Apr 25, 2007, at 5:35 AM, Richard Conroy wrote:

    > On 4/25/07, Andrei Ursan <> wrote:

    <snip>
    > DYOH - Do Your Own Homework


    Speaking of which, I have a biology lab report due next wednesday, so
    I need someone to do it for me - make sure it's GOOD!

    -------------------------------------------------------|
    ~ Ari
    crap my sig won't fit
    Ari Brown, Apr 25, 2007
    #17
  18. Andrei Ursan

    Lyle Johnson Guest

    Guys, let's back off, please. I think the OP has gotten the message
    loud and clear.
    Lyle Johnson, Apr 25, 2007
    #18
  19. Andrei Ursan

    Michael Ulm Guest

    Andrei Ursan wrote:
    > Sammy Larbi wrote:
    >> Andrei Ursan wrote, On 4/25/2007 4:29 AM:
    >>> Can somebody translate this into ruby ? I need it fast...
    >>>
    >>>

    >>
    >> I don't mean to be rude, but I'm with most of the others on this. I
    >> found it amusing (is that mean of me?) that you have no time to RTFM,
    >> but you have time to wait longer than it would have taken to RTFM for
    >> someone to answer. You don't need an entire book for loops and printing
    >> strings. Thats page one stuff in any language, I would think.

    >
    > No, i did rftm and i'm doing it even now :)) :p
    > It's ok... I'm fine, don't need to worry 4 me, btw if u have some good
    > ruby doc pleas links gimme to me i'm ruby doc hungry.
    >


    Welcome to Ruby, Andrei.

    A Ruby book like no other can be found at

    http://poignantguide.net/ruby/

    HTH,

    Michael

    --
    Michael Ulm
    R&D Team
    ISIS Information Systems Austria
    tel: +43 2236 27551-542, fax: +43 2236 21081
    e-mail:
    Visit our Website: www.isis-papyrus.com

    ---------------------------------------------------------------
    This e-mail is only intended for the recipient and not legally
    binding. Unauthorised use, publication, reproduction or
    disclosure of the content of this e-mail is not permitted.
    This email has been checked for known viruses, but ISIS accepts
    no responsibility for malicious or inappropriate content.
    ---------------------------------------------------------------
    Michael Ulm, Apr 26, 2007
    #19
  20. On 4/26/07, Michael Ulm <> wrote:
    > Andrei Ursan wrote:
    > > Sammy Larbi wrote:
    > >> Andrei Ursan wrote, On 4/25/2007 4:29 AM:
    > >>> Can somebody translate this into ruby ? I need it fast...
    > >>>
    > >>>
    > >>
    > >> I don't mean to be rude, but I'm with most of the others on this. I
    > >> found it amusing (is that mean of me?) that you have no time to RTFM,
    > >> but you have time to wait longer than it would have taken to RTFM for
    > >> someone to answer. You don't need an entire book for loops and printing
    > >> strings. Thats page one stuff in any language, I would think.

    > >
    > > No, i did rftm and i'm doing it even now :)) :p
    > > It's ok... I'm fine, don't need to worry 4 me, btw if u have some good
    > > ruby doc pleas links gimme to me i'm ruby doc hungry.
    > >

    >
    > Welcome to Ruby, Andrei.
    >
    > A Ruby book like no other can be found at
    >
    > http://poignantguide.net/ruby/


    Though that one's for people of alternative brain.
    Leslie Viljoen, Apr 26, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danno
    Replies:
    55
    Views:
    1,451
    Chris Smith
    Apr 25, 2006
  2. Replies:
    0
    Views:
    647
  3. Michele Simionato

    Python is darn fast (was: How fast is Python)

    Michele Simionato, Aug 23, 2003, in forum: Python
    Replies:
    13
    Views:
    552
  4. Kimmo Laine

    rtfm?

    Kimmo Laine, Jun 26, 2005, in forum: Javascript
    Replies:
    3
    Views:
    92
    Gus Richter
    Jun 27, 2005
  5. optimistx

    OT: RTFM

    optimistx, Aug 10, 2009, in forum: Javascript
    Replies:
    1
    Views:
    78
    Paul E. Schoen
    Aug 11, 2009
Loading...

Share This Page