Jump to content
  • Advertisement
Sign in to follow this  
gnomer

check my work: reverse number

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi just hoping someone can comment on how I solved this exercise and tell me how/if I could have done it better. I think this probably would work better with an array but at this point the book has not covered them so I don't use them here. The exercise:
Quote:
Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367.
My Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int flip( int n )
{
	int number = 0;
	int temp = n;
	int size = 10;
	
	while ( n > size )
		size *= 10;

	if ( size > n )
		size /= 10;

	for ( int i = size; i > 0; i /= 10 )
	{
		number += (temp % 10) * i;
		temp = temp / 10;

	}

	return number;
}

int main()
{
	int x;

	cout << "Enter a positive integer number: ";
	cin >> x;

	cout << "Reversed order is: " << flip( x ) << endl;

	return 0 ;

}

Share this post


Link to post
Share on other sites
Advertisement
Woulnd't work better with an array, seems like you got the right idea.

you could build it from the bottom to avoid some work:

int flip(int x)
{
int n = 0;
for(; x; x /= 10)
{
n *= 10;
n += x % 10;
}
return n;
}

Share this post


Link to post
Share on other sites
Let me add, one problem I have is that if i put a number ending in 0's it doesn't return the right number. Like 100 should be 001 but I dunno how to get it to put the 0's.

Share this post


Link to post
Share on other sites
Quote:
Original post by gnomer
Let me add, one problem I have is that if i put a number ending in 0's it doesn't return the right number. Like 100 should be 001 but I dunno how to get it to put the 0's.


Well I would say that's according to specification, 001 == 1 so that's mainly an output issue, if you really want the leading zeros add an output parameter for total length and zero-pad as needed.

Share this post


Link to post
Share on other sites
So I can't do it with just returning the int right? I would have to like count each time I get a leading zero and cout for each one? Is that what you mean?

And thanks for showing me how to simplify the function. Much cleaner than what I had. I'm still in the newbie way of writing code, breaking every little thing down :)

Share this post


Link to post
Share on other sites
How is this?


int flip( int x )
{
int lead = 0;

int n = 0;
for(; x; x /= 10)
{
n *= 10;
n += x % 10;

if ( n == 0 )
lead++;
}

for (; lead; lead-- )
cout << "0";

return n;
}

Share this post


Link to post
Share on other sites
It's not so good stylistically. You've sepearated output into two different locations, once in the flip, and once in the return. A better approach would be to have the flip() function return a string object instead of a number, so that the output function can output the zeros.

I personally would take an entirely different approach to doing this. First use boost::lexical_cast to turn the number into a string and then use std::reverse() to reverse the digits.

std::string flip(int n) {
std::string str = boost::lexical_cast<std::string>(n);
std::reverse(str.begin(), str.end());
return str;
}

Share this post


Link to post
Share on other sites
yea a string would be best but that also has not been covered yet :( I had thought about that and wished I could use it although I wouldn't have known how to do it exactly anyway :P Next chapter is arrays and then following that chapter is pointers and strings so I'll get there eventually...

Share this post


Link to post
Share on other sites
"Pointers and Strings"? Uh-oh. I hope the author isn't stupid enough to try introducing you to C-style strings. (i.e. "char *" instead of "std::string"). If so, drop the book and run, and don't pick it up again until you have at least two years of experience and a qualified technician to assist you.

Share this post


Link to post
Share on other sites
hmm I think it might be char* style unless it does both. The book is "C++ How to Program 4th Edition" by Deitel. I'm skimming to see what it covers later.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!