check my work: reverse number

Started by
22 comments, last by Julian Spillane 18 years, 8 months ago
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 ;

}

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;}
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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.
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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 :)
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;}
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;}
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...
"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.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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.

This topic is closed to new replies.

Advertisement