Public Group

# check my work: reverse number

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

## 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 on other sites
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 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 on other sites
Quote:
 Original post by gnomerLet 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 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 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 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 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 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 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×