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

#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…

On 4/25/07, Andrei U. [email protected] 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 C. wrote:

On 4/25/07, Andrei U. [email protected] 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.

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

cout<<“v[”<<i<<"]=";
for(i=0;i<n;i++)

cout<<" no = ";
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.

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.

There are still ways to ask for things.

On Wed, Apr 25, 2007 at 06:29:35PM +0900, Andrei U. wrote:

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

Please read:
http://www.catb.org/~esr/faqs/smart-questions.html#urgent

Then read the whole of the rest of that document.

On Wed, 25 Apr 2007 20:44:52 +0900, Andrei U. 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

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[i]<<" ";
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[i]<<” ";
}

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

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

cout is called STDOUT, you don’t need semicolons, and the string

Putting it all together:

s

c’est bon!

Andrei U. wrote:

cout<<“v[”<<i<<"]=";
for(i=0;i<n;i++)

cout<<" no = ";
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 U.
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-542, fax: +43 2236 21081
e-mail: [email protected]
Visit our Website: www.isis-papyrus.com

On 4/25/07, Andrei U. removed_ema[email protected] 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 H. and Ryan D… 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

doh, sorry to all of you cos i’m not so polite as you want… :frowning: 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 :slight_smile:

This is what i was trying to do :

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 )

Andrei U. 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.

On Thu, 26 Apr 2007 00:16:01 +0900, Andrei U. wrote:

This is what i was trying to do :

This is something I can read :slight_smile:
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.

Sammy L. wrote:

Andrei U. 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 :)) :stuck_out_tongue:
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.

On Thu, Apr 26, 2007 at 02:49:45AM +0900, Andrei U. wrote:

if u have some good
ruby doc pleas links gimme to me i’m ruby doc hungry.

http://www.rubycentral.com/book/ (or better: buy the 2nd edition)

http://www.ruby-lang.org/en/documentation/quickstart/

Andrei U. [email protected] writes:

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.

Here’s a link for you: http://www.dictionary.com

sherm–

Guys, let’s back off, please. I think the OP has gotten the message
loud and clear.

Andrei U. wrote:

strings. Thats page one stuff in any language, I would think.

No, i did rftm and i’m doing it even now :)) :stuck_out_tongue:
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 U.
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-542, fax: +43 2236 21081
e-mail: [email protected]
Visit our Website: www.isis-papyrus.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs