Maybe a better way to describe it is:
What math do we need to do in each step to make this happen:
--Code:r = 0; // reverse number a = 123; // original number r = 3; a = 12; r = 32; a = 1; r = 321; a = 0;
Mats
Maybe a better way to describe it is:
What math do we need to do in each step to make this happen:
--Code:r = 0; // reverse number a = 123; // original number r = 3; a = 12; r = 32; a = 1; r = 321; a = 0;
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
So what you mean is that I treat the final output as a whole integer instead of individual digits printed side by side?
Nice!!!r = 0; // reverse number
a = 123; // original number
r = 3;
a = 12;
r = 32;
a = 1;
r = 321;
a = 0;
Yup, that's what your function should be doing - it returns ONE integer, right? At least how I understand it, and I'm pretty sure I'm not alone here - if you have any doubt about that, please ask your tutor - we wouldn't be wanting to guide you in the wrong direction.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
It depends on what you mean by final output.
The function's prototype is
int reverse(int);
This means you can return one number. So yes, you want to return a single number.
You cannot even return "individual digits printed side by side" because the function is only supposed to return something, not print it.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I this ok?
I think its working.Code:#include <iostream> using namespace std; int reverse(int x) { int reverse = 0,exp = 1,counter = x; while (counter != 0) { counter = counter /10; reverse = (((x%(exp*10))/exp) + reverse)*10; exp = exp * 10; } return reverse/10; } int main() { int num; cin >> num; cout << "Reverse of " << num << " is " << reverse(num) << endl; return 0; }
That looks very convoluted to me. A simpler solution that also works for non-negative integers only would be:
EDIT:
Wait a minute, this is not just self-learning? I feel that you have a simple idea struggling to get out, but have the basic idea there, hence I posted a solution... but if this really is to be submitted as homework, then you might want to think a little further before looking at my solution.
One way to help you think is for you to explain to us what is the idea that you are currently using.
Last edited by laserlight; 08-23-2008 at 12:24 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I made a new one but I can only input numbers with 8 or less digits, just like the other one but it has less variables now.
And please, I really appreciate all your help but its much better if you can answer me without sarcasm, it doesn't really boost my moralCode:#include <iostream> using namespace std; int reverse(int x) { int reverse = 0; while (x != 0) { reverse = ((x % 10) + reverse)*10; x = x / 10; } return reverse/10; } int main() { int num; cin >> num; cout << "Reverse of " << num << " is " << reverse(num) << endl; return 0; }
You will reach the limit of int eventually, though in this case you are even more limited since your algorithm is still slightly more convoluted than necessary. Nonetheless, I feel that you have come close enough, so here is the solution I had in mind:I made a new one but I can only input numbers with 8 or less digits, just like the other one but it has less variables now.
The main difference between my code and yours is that my code multiplies the partially reversed result by 10 before adding the next digit, whereas your code adds the next digit and then multiplies by 10, thus forcing you to divide the result by 10 at the end, which in turn reduces the range that can be handled by a magnitude.Code:int reverse(int x) { int x_reversed = 0; while (x > 0) { x_reversed *= 10; x_reversed += x % 10; x /= 10; } return x_reversed; }
To be rid of the limit of ints, change the problem into one of reading a string of characters and printing the string in reverse.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Nice thanks, and maybe I could just remove the quantity. Like this:
It's the same right?Code:int reverse(int x) { int reverse = 0; while (x != 0) { reverse = (x % 10) + reverse*10; x = x / 10; } return reverse/10; }
Now the division at the end looks superfluous.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Oh yeah, I forgot. Fixed it already
Minor detail - it is always a good idea to use different names for variables and functions - your function and your variable are both called "reverse".
And it only took about 40 posts to get there....
--
Mats
Last edited by matsp; 08-24-2008 at 12:45 PM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.