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

A

Andrei Ursan

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

Richard Conroy

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
 
A

Andrei Ursan

Richard said:
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.
 
J

John Joyce

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

Andrei Ursan

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

Stefan Schmiedl

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
 
T

Tim Becker

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
 
J

John Joyce

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




c'est bon!
 
M

Michael Ulm

Andrei said:
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: (e-mail address removed)
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.
---------------------------------------------------------------
 
T

Tim Pease

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
 
A

Andrei Ursan

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 )
 
S

Stefan Schmiedl

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

Sammy Larbi

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

Andrei Ursan

Sammy said:
Andrei Ursan wrote, On 4/25/2007 4:29 AM:


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

Lyle Johnson

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

Michael Ulm

Andrei said:
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: (e-mail address removed)
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.
---------------------------------------------------------------
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top