A better way to do it?

Started by
24 comments, last by JoeCooper 13 years, 6 months ago
What szecs says is truth. It was only explained here because the OP appeared to be missing the point entirely about procedural thinking.

For the OP, I have another exercise. No hints. If you understand our explanation of the first one, you can do this one.

It's called "Fizz Buzz".

Count from 1 to 100.

When a number is a multiple of 3, print out "Fizz".

When it is a multiple of 5, print out "Buzz".

When it is a multiple of both 3 and 5, print out "Fizzbuzz".

No hardcoding the results. Don't look it up. Go.

[Edited by - JoeCooper on October 10, 2010 12:39:40 PM]
Advertisement
fizzbuzz is a too common problem. It has trillions of hits with google.



Oh, wait... D'OH!!
Ok, I'll try but if I trouble may I ask a little help? I'm not asking for the solution, just a little hint.

I'm saying this because I only understood a little of the first one. But let me try since this exercise is simple.
Hint: it's like the other one, but asks a moderately simpler question about each number. Don't forget to look up what modulus does; what question it can answer. Carefully read Angle's sample.
Quote:
Ok, I'll try but if I trouble may I ask a little help?

Sure, never hesitate to ask.

By the way, these assignments are mathematical, and in my opinion not well suited for someone learning the language. Adding mathematical complexity is typical for programming assignments but can easily discourage people needlessly. Personally I have been programming for almost 20 years and working as a software engineer for some of them and I have never had any use of writing a prime function :D

Anyway, I thought I would give you a hint about the modulus operator as it is not something most people recognize.
I don't know about you but when I think back at elementary school I remember when we learned about multiplying, dividing etc.
Back then we hadn't learned anything about decimal numbers. It was all whole numbers, and when we learned how to divide, our teacher told us that when you can no longer take another divisor out of the dividend without getting a negative number, you are done.
It would typically look like this
23 / 3 = 721 2

That was it. No decimals.
The answer was 7 and the reminder was 2

Now, in C++ programming, if we are working with integer numbers we can get to those values using / and %

23 / 3 = 7
23 % 3 = 2

As you can see the modulus operator (%) gives us the reminder of an integer division. It turns out that getting this reminder is quite useful in many programming tasks.

If you feel this is over your head, don't sweat it. Take your time learning the basics of the language first
If you write in pseudo-code, we'd be happy to review it.

This topic is closed to new replies.

Advertisement