puts "Enter 5 numbers and I'll calculate the sum:"
puts "The sum is #{Array.new(5){gets.to_i}.inject(:+)}"
Note your C++ example is kind of odd. It is more conventional to have x start at 0, and test for while (x < 5). "total = total + number " is equivalent to "total += number", you ought to be really using a for loop instead of a while loop - what you really want is to iterate something 5 times, you don't actually care what x is through every iteration, yet it is being kept around visible to you. The first Ruby solution I present solves this issue elegantly using blocks - no worries about bounds.
Re: Fonzie's Tips on Getting started with programming
Posted: Tue Nov 15, 2011 10:27 am
by draconic74
Nitpicky, but true.
Also, HOLY HELL BUGDOM. When I used to have an old HPP desktop oh so many years ago, before I knew of this community, my dad would occasionally bring home one of the fruity-colored iMacs from the university. I remember I had to go behind the couch in that house, because that was the only place that had both an outlet and an internet cable (why, I don't know. I was young back then, I couldn't care). I would sit there for hours playing Bugdom, Nanosaur, and flash games on CartoonNetwork.com. Because apparently my HP didn't have flash, or was too slow to run them, or something. I just remember I never played those games on the HP, and now I can't remember why. I could never get far in Nanosaur, but luckily Bugdom was a bit easier. I think I actually managed to beat the game. There was also Cro-Mag Rally, but that wasn't nearly as fun as the other two.
Oh look, more musical nostalgia. If you ever played Bugdom for even a bit, you should remember this. As soon as I started hearing it, memories... Oh my.
I also remember playing MDK on his insanely old laptop. Anyone remember that one? That was a goddamn hard game. And even further back... In fact, you know, I think the first game I might have ever played was Asteroids. It was on one of these:
That thing is still sitting on a shelf back at home. We still have the charger too. Shame it doesn't work.
Re: Fonzie's Tips on Getting started with programming
Posted: Tue Nov 15, 2011 1:27 pm
by Fonzeh
Ah yes, I did change it to 0 later, noticing it was off, this picture was taken beforehand.
Also I counter with this.
Re: Fonzie's Tips on Getting started with programming
with Ada.Text_IO;
use Ada.Text_IO;
procedure age_thing is
ageTotal, age, derp : integer := 0;
begin
put("I heard you like ages, so Imma put ages inside of ages just for you. Or -7 to quit, whatevs.");
loop
get(age);
exit when age = -7; -- I love this command, btw. but thats the only one so far that I like in Ada
ageTotal := ageTotal + age; -- Ada doesnt have +=, :sad face:
derp := derp + 1; -- No ++ either! ARGH!
end loop;
put("Number of ages aged into ages: ");
put(derp);
New_Line;
put("Average age: ");
put(ageTotal/derp);
-- Yeah, without some more advanced command things you can't put variables
-- and strings into the same line. Herp derp. Also, no quote blocks, like C++'s /* Helloes */
end age_thing;
Oh damn, multiple spacing works in code blocks. Hell yeah!
Re: Fonzie's Tips on Getting started with programming
Posted: Tue Nov 15, 2011 3:16 pm
by Sparky
Procedural programming is fun for simple things.
Draconic, MDK was really fun, but for some reason kept crashing on me, if I recall correctly.
Re: Fonzie's Tips on Getting started with programming
def get_ages
age = gets.to_i
return (age == -1) ? [] : ([age] + get_ages)
end
puts "Enter ages for people. Type -1 to stop."
ages = get_ages()
puts "Number of people entered: #{ages.count}"
exit if ages.count == 0 #Forgot to avoid divide by zero, edited this later in - so, there is no average age if this is the case
puts "Average age: #{ages.inject(:+) / ages.count}"
Nitpicking is important, especially when there is code that could be improved upon. I'll nitpick's Fonzie's code above soon when I get the chance.
Re: Fonzie's Tips on Getting started with programming
Posted: Tue Nov 15, 2011 7:41 pm
by Sparky
nil, please give reasons why you prefer ruby over other languages.
Re: Fonzie's Tips on Getting started with programming
Posted: Tue Nov 15, 2011 8:07 pm
by Mgalekgolo
Ruby is awesome.
Re: Fonzie's Tips on Getting started with programming
Posted: Wed Nov 16, 2011 12:59 pm
by draconic74
Diamond was better. As was Silver.
Also, yes, nitpicking is a good thing. But try not to use too much of the advanced stuff if you do so. We are not at your level yet. The idea of using '+=' and starting at 0 is good, but in code this simple the idea of using 'for' or 'while' is debatable. To me, anyway. In more advanced code there is a definite reason to use one or the other, but I don't really see a reason to use one or the other in this one. Also, someone's choice language is simply that, their choice. They have reasons they like it better than other languages.
These two lines are essentially the same. Instead of using '=', you can use '+='. It's meaning is simply "This variable is equal to itself plus whatever comes after the '+=. "
For starting at zero, well... It's just a computer thing. Everything starts at zero. When you start using arrays, in C++ they start at zero (in other languages you can define their range). It's just a consistency thing.
Re: Fonzie's Tips on Getting started with programming
Posted: Wed Nov 16, 2011 8:05 pm
by nil
draconic74 wrote:Also, yes, nitpicking is a good thing. But try not to use too much of the advanced stuff if you do so. We are not at your level yet. The idea of using '+=' and starting at 0 is good, but in code this simple the idea of using 'for' or 'while' is debatable. To me, anyway. In more advanced code there is a definite reason to use one or the other, but I don't really see a reason to use one or the other in this one. Also, someone's choice language is simply that, their choice. They have reasons they like it better than other languages.
I can point out issues in code while understanding that one is not using the more *different* approaches like I am. Exposure to different approaches and ways of programming is a good thing as well as exposure to different programming languages. One can choose to start with any programming language, but he should not limit himself to one. Just because one uses a particular language does not convince me that he actually likes it better than others, which is often the case when one does not have enough experience to make a proper judgement.
+= is a shorthand like you explained, the benefit is obvious: less typing and code. I mentioned this before, though. I also explained starting at 0 as being a convention like you mention. I guess this is kind of a debatable point but many languages like starting at 0 and it often makes sense when accessing indexes or referencing memory and doing arithmetic on them.
I personally think for loops should be used in cases like:
int index = 0;
while (index < 5)
{
//...
index++;
}
..where the while loop is merely simulating the for loop.
In Fonzie's particular case, he just wants to repeat doing something 5 times - the counter is not actually important. Ruby offers a nice way of iteration for this scenario using closures as I showed above.