Fonzie's Tips on Getting started with programming

Everything related to programming.
nil
Halo Moderator
Halo Moderator
Posts: 1090
Joined: Sat Jul 05, 2008 8:38 am
Location: null zone

Re: Fonzie's Tips on Getting started with programming

Post by nil » Wed Nov 16, 2011 8:09 pm

Onto Fonzie's sentinel program example. I said I would be pointing out issues before, so I'm doing that now:

First off, please seriously name your variables. I know it may just be joking around, but it doesn't help code readability or me or anyone else reading it. This goes for draconic too.

Fonzie has duplicate lines of similar code. Bad, very bad. In particular, the culprit is the input being assigned to age, that is (cin >> age). Also, I do not think there is any significant difference between "first person" and the "next person."

It could be changed like this:

Code: Select all

//...declarations and stuff...

while (age != -1)
{
    cout << "Enter a person's age or -1 to quit" << endl;
    cin >> age;
    if (age != -1)
    {
        ageTotal += age;
        numberOfAges++;
    }
}

//...print output...
Which is better, but there is something still bad about the code. Notice it?

We are checking the condition age != -1 twice. Bad. One way I can think of solving this is breaking out an infinite loop instead, similar to draconic's approach, like so:

Code: Select all

while (true)
{
    cout << "Enter the next person's age or -1 to quit" << endl;
    cin >> age;
    if (age == -1) break;
    ageTotal += age;
    numberOfAges++;
}
Not looking so bad anymore. Can anyone think of a better way?

My approach by the way, instead of using a loop, was building a list of ages recursively (via recursion), then operating on the list to find the sum and number of ages -- I know, a different approach, but it doesn't hurt to hear about it.

[EDIT:] I completely forgot about the case where no ages are entered which may cause division by 0 error - that is a good thing to avoid ;P. Draconic forgot too, hehe.
Last edited by nil on Wed Nov 16, 2011 10:43 pm, edited 3 times in total.
I am no longer active to Halo or MGM, and don't guarantee a response on the forums or through email. I will however linger around the discord room for general chatting. It's been fun!

nil
Halo Moderator
Halo Moderator
Posts: 1090
Joined: Sat Jul 05, 2008 8:38 am
Location: null zone

Re: Fonzie's Tips on Getting started with programming

Post by nil » Wed Nov 16, 2011 8:10 pm

Sparky wrote:nil, please give reasons why you prefer ruby over other languages.
I don't prefer it over other languages all the time - it depends on the task I'm doing. I like Ruby because it's simple and easy to use, a highly dynamic language, truly object-oriented where everything is an object, less typing and lines of code, borrows concepts from functional programming languages - such as closures/blocks/lambda expressions (such a fundamental programming concept which is used extensively in Ruby, yet you may never see them in the land of C, Java, C++, etc). Much of the same can be said about say, Python, too which is close in comparison to Ruby, though I have a style preference for Ruby.
I am no longer active to Halo or MGM, and don't guarantee a response on the forums or through email. I will however linger around the discord room for general chatting. It's been fun!

draconic74
Green Beret
Posts: 3470
Joined: Sat Jun 03, 2006 11:08 am
Contact:

Re: Fonzie's Tips on Getting started with programming

Post by draconic74 » Thu Nov 17, 2011 12:18 am

Hmm... You are correct. We could further that by making it so that we check to see if anything was inputed at all (which is basically a "check for zero" anyway)

EDIT: This topic was getting clogged a little while ago with musical stuffs, so I posted a thread for music that you guys work to, or stuff like that. Either I clicked Music instead of Programming or it got stealth-moved.
Image

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests