Breaking a three-digit integer. Is this a good way?

Started by
49 comments, last by Cecil_PL 15 years, 9 months ago
Either my answer is too simple (as in wrong) or yours is a bit more involved than expected.

Beginner in Game Development?  Read here. And read here.

 

Advertisement
This would be my quick 'n' dirty solution:

void split(unsigned int number){    static const unsigned int base = 10;    if (number > base)        split(number / base);    std::cout << number % base << "\n";}int main(){    unsigned int number;    std::cin >> number;    split(number);}


Quote:Original post by Alpha_ProgDes
Either my answer is too simple (as in wrong) or yours is a bit more involved than expected.

The implied logic of yours is correct but your actual answer wouldn't work, it's close though.
@sheep19: Try to do it without using log10(). It will actually be simpler that way. Hint: Don't think about how many iterations the loop needs to run. Think instead of when it should stop running.

Also, note that the question simply asked you to print the digits, so there is no need to store them, and therefore no need to use an array or std::vector. The whole thing can be done in one loop.

@Alpha_ProgDes: Have you tried running your code? There are at least two problems with it (or at least one, depending on what language you are using). Try to find them without using a compiler. Also try doing it without an array like I explained above.

As a similar exercise, write a program that reads in a number and checks to see if it's a palindrom, i.e., reads the same from backwards. For example, 123 is not a palindrom, while 1221 is.

The program should not use an array or any other type of storage container, should not use log10(), and should only use one loop. Once again, it should work for any number.
Quote:Original post by Gage64
As a similar exercise, write a program that reads in a number and checks to see if it's a palindrom

That's laughably simple using the STL (3 lines, I think - and only one of those is the actual check), presumably though you're asking for a more "algorithm theory-esque" answer.
Quote:Original post by dmatter
Quote:Original post by Gage64
As a similar exercise, write a program that reads in a number and checks to see if it's a palindrom

That's laughably simple using the STL (3 lines, I think - and only one of those is the actual check), presumably though you're asking for a more "algorithm theory-esque" answer.


Given the original post, what do you think? [wink]

The point here is to get some exercise using loops, arithmetic operations and other fundamental language constructs, not the STL. If you are using the STL for any of this, that's cheating.
Ok, I did it again, but it displays the digits in reverse order:

do	{		i = 1;		std::cout << num / i % 10 << " ";		num /= 10;		i *= 10;	} while( num != 0 );
Quote:Original post by dmatter
Quote:Original post by Gage64
As a similar exercise, write a program that reads in a number and checks to see if it's a palindrom

That's laughably simple using the STL (3 lines, I think - and only one of those is the actual check), presumably though you're asking for a more "algorithm theory-esque" answer.

Yeah I'm assuming the instructor want's to see the use of the mod operator somewhere in your solution instead of the STL since this is usually one of the questions that shows up early in a C++ course before you get to the STL.
Variations include using binary instead of decimal numbers.

p.s. To original poster I hope you at least wrote out a majority of your program and maybe some pseudocode so you can get some credit? At least that's what I would've done if I drew a blank on a test which did happen to me once and I got partial credit.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Quote:Original post by sheep19
Ok, I did it again, but it displays the digits in reverse order:

do	{		i = 1;		std::cout << num / i % 10 << " ";		num /= 10;		i *= 10;	} while( num != 0 );


Think about what the i variable is actually doing there.

In order to display the symbols in order you will need some kind LIFO (Last In First Out) structure, such as a stack. My solution used recursion, which makes use of the call stack for this purpose.
Quote:Original post by dmatter
Quote:Original post by sheep19
Ok, I did it again, but it displays the digits in reverse order:

do	{		i = 1;		std::cout << num / i % 10 << " ";		num /= 10;		i *= 10;	} while( num != 0 );


Think about what the i variable is actually doing there.


Oh. I was diving by 10 and then multiplicating by 10. It's isn't needed I think.
Quote:Original post by sheep19
Oh. I was diving by 10 and then multiplicating by 10. It's isn't needed I think.


Well, think about what happens upon each iteration:

First Iteration:
You let i = 1
You evaluate num / i % 10 but dividing by 1 has no effect.
You let i *= 10 so now it contains the value 10.

Second Iteration:
You let i = 1 so the previous multiply is of no consequence.
You evaluate num / i % 10 but dividing by 1 has no effect.
You let i *= 10 so now it contains the value 10.

N'th Iteration:
You let i = 1 so the previous multiply is of no consequence.
You evaluate num / i % 10 but dividing by 1 has no effect.
You let i *= 10 so now it contains the value 10.

In short, you have a variable that you're manipulating but it has no side effects and so could be removed entirely. [smile]

This topic is closed to new replies.

Advertisement