# Thread: Reverse of an integer

1. ## Reverse of an integer

The problem:
Code:
```The name of the function's name should be reverse. Let the
name of its value parameter be x (which is an int).  The
return datatype should be int.  Use the main() driver program
provided below to test your function:

#include <iostream>
using namespace std;

// insert the reverse function here.

int main()
{
int num;
cin >> num;
cout << "Reverse of " << num << " is " << reverse(num) << endl;
return 0;
}```
I know what I should do to obtain the reverse number, but its harder when using a function int. The program should handle any as much digits it can handle. So I know I should use a loop, and also you should know the number of digits - I've done that. My question is what is the proper way of returning the reverse digits. And by the way our lesson is about REFERENCES AND REFERENCE PARAMETER.

2. This particular bit of code doesn't (as far as I can determine) need to use references.

How would you go about reversing an integer if you didn't have to use a function?

--
Mats

3. Use a modulo?then output the result side by side in a loop?

4. Reverse in what way? 43,621 => 12,634? 1 => -1? Sorry for sounding like a smart allec, but your goal seems ambiguous.

Example
Code:
```#include <cstring>
#include <cstdlib>

int reverse(int number)
{
char buffer[256];

ssprintf(buffer, "&#37;d", number);
return atoi(strrev(buffer));
}```
Nothing too fancy... To be honest though, I am not sure if this is even what you are wanting to do.

5. my interpretation of "reverse" is 123 -> 321. And using sprintf/strrev/atoi is a bit overkill, isn't it - the problem can be solved with two or three variables. Modulo is one of the operations you need, you will also need divide, add and multiply. Put those bits in the right order, and you should be able to solve the number.

--
Mats

6. >And using sprintf/strrev/atoi is a bit overkill, isn't it
Barring atoi because it sucks and defeats the benefit of using strings, no, it isn't overkill. In fact, turning the integer into a string and reversing the string is vastly superior to breaking down and reconstructing the value as an integer. Why? What happens if you want to reverse 1999999999 with 32-bit integers? There are far too many cases like that, where the reversed value can't be represented by the data type.

7. I think Freddy steered you right. You want to figure out the number of digits represented, which you can do by progressively dividing by 10 (inside a loop)(incrementing a counter) until you return a zero. Once you know the number of digits, you can proceed accordingly. Looks like an interesting little task that is similar to reversing a string using the old array-of-characters manipulation.

And Prelude is right... some ints won't reverse as valid numbers, so you need to determine the size of an int and make sure that it won't overflow on reversing--whether you use strings or not.

Keith

You can tune a file system, but you can't tune a fish.

8. Originally Posted by Prelude
>And using sprintf/strrev/atoi is a bit overkill, isn't it
Barring atoi because it sucks and defeats the benefit of using strings, no, it isn't overkill. In fact, turning the integer into a string and reversing the string is vastly superior to breaking down and reconstructing the value as an integer. Why? What happens if you want to reverse 1999999999 with 32-bit integers? There are far too many cases like that, where the reversed value can't be represented by the data type.
But the function should return an integer, so whether it copes with huge numbers DURING the reversal makes little difference when it gets converted back to integer [unless you plan to detect it, of course].

--
Mats

9. Only in this case, and I don't think Prelude was trying to scold anyone, per se.

Reversing a string also needs tweaking if the number is negative, ie:
Code:
```if (number < 0) {
strrev(result + 1);
}
else {
strrev(result);
}
return result;```
But I'm only mentioning it since I'm not positive 8765- is correct.

10. #include <cstring>
#include <cstdlib>

int reverse(int number)
{
char buffer[256];

ssprintf(buffer, "&#37;d", number);
return atoi(strrev(buffer));
}
Using that is a lot better, but I cant really use that.
Code:
```#include <iostream>
using namespace std;

// insert the reverse function here.

int main()
{
int num;
cin >> num;
cout << "Reverse of " << num << " is " << reverse(num) << endl;
return 0;
}```
This is what we should use as our main. Then will just insert our function. I've done it using void, its "amatuerish",but at least I know how to do the logic part. And I think were required to use a function int, and so Im lost there. I have no idea how to properly return the digits in a loop.

11. Why can't you use that?

12. Code:
```#include <iostream>
using namespace std;

// insert the reverse function here.

int main()
{
int num;
cin >> num;
cout << "Reverse of " << num << " is " << reverse(num) << endl;
return 0;
}```
This should be our main

13. I think freddy's point is that you can't include further header files - which goes back to my suggestion to use simple math.

What happens if you do this:
Code:
```int x = 21;
int y;

y = x &#37; 10;
x = x / 10;

cout << "x = " << x << "y = " << y << endl;```
Now I've just about given away the solution.

--
Mats

14. I know how to get the individual digits, but when using function I dont know how to return it properly using loop

15. Originally Posted by freddyvorhees
I know how to get the individual digits, but when using function I dont know how to return it properly using loop
So, try to write something, using what you know, post it, and we'll help you sort out any problems.

--
Mats