Reverse Number Order

Reverse number order

Page 1 of 1

12 Replies - 36109 Views - Last Post: 31 March 2009 - 09:11 AM Rate Topic: -----

#1 babyboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-March 07

Reverse Number Order

Posted 27 March 2007 - 07:21 AM

I am new to C++ and need help. I need to write a program where I enter and number ( 321) and then enter that same number reversed (123). The program need to tell if the 2nd number is the same as the 1st number in reversed order. If not the program would say "no, the reverse order of 321 is 123.

Do anyone know of a simple way I can do this using functions. I know very little. Here is what I have so far




int number1, number2;
int reverse(int);

main()
{
	cout << "Please enter the first positive number  ";
	cin >> number1;
	cout << "Please enter the second second number:  ";
	cin >> number2;
	
	
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Reverse Number Order

#2 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: Reverse Number Order

Posted 27 March 2007 - 08:34 AM

A procecure like this might work:
* Convert the number into a string of characters.
* reverse the string. (posible separate function)
* Convert the string back in to a number
OR conver the second number into a string.
* Compare the two numbers (posible separate function)
Was This Post Helpful? 0
  • +
  • -

#3 babyboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-March 07

Re: Reverse Number Order

Posted 27 March 2007 - 02:46 PM

I know what to do but not sure of the code. Can anyone give me some code.

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is online

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Reverse Number Order

Posted 27 March 2007 - 03:01 PM

There are now two snippets giving an example of how to reverse the digits of a number. First there is: reverse a number By frog and then I just added the following code:
/* Code by NickDMax, public domain code example */
#include <iostream>
#include <cmath>
using namespace std;
 
int main()
{
	 unsigned int num=123456; //Using an unsigned int's incase the reverse is grater than MAX_INT
	 unsigned int rev;
	 int digits = log10(num) + 1; //Tells us how many digits are in the number
	 int i; //used to loop though digits...
	 rev=0; //Initialize....
	 for (i=0; i<digits; i++)
	 {
		  rev *= 10; //Make room for the next digit...
		  rev += (num % 10); //add in the next digit.  123 % 10 = 3
		  num /=10; //Shift the digits to the right... 123/10 = 12
	 }
	 
	 cout << digits <<" " << rev <<"\n";
	 return 0;
}


The code above will need to be customized of course. I would make a function out of it so that you main routine can just call the function.

Once you have a reversed digit number all you need to do is use a standard if (a==b ) to tell if the two are equal.

This post has been edited by NickDMax: 27 March 2007 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Reverse Number Order

Posted 27 March 2007 - 03:01 PM

Here is another idea:
Let say 123 is the number you enter
after (123%10)*100+(123%100)/10+(123)/100
you'll get 321

So check it out, it's not a code but it could be just think it through.
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Reverse Number Order

Posted 27 March 2007 - 04:15 PM

As the original requirements did not actually call for any math, one could also simply take the input as strings, reverse one, and compare.
Was This Post Helpful? 0
  • +
  • -

#7 bibhuti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-March 07

Re: Reverse Number Order

Post icon  Posted 30 March 2007 - 08:57 AM

//might this code help you..................
#include<stdio.h>
#include<conio.h>
void main()
{
int i,k=0;
i=12345;
while(i>=9)
{
k+=i%10;
i=i/10;
k=k*10;
}
if(i!=0)
{
k=k+i;
}

printf("%d",k);
getch();
}
Was This Post Helpful? 0
  • +
  • -

#8 drshorty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 09

Re: Reverse Number Order

Posted 29 March 2009 - 04:56 PM

View PostNickDMax, on 27 Mar, 2007 - 02:01 PM, said:

There are now two snippets giving an example of how to reverse the digits of a number. First there is: reverse a number By frog and then I just added the following code:
/* Code by NickDMax, public domain code example */
#include <iostream>
#include <cmath>
using namespace std;
 
int main()
{
	 unsigned int num=123456; //Using an unsigned int's incase the reverse is grater than MAX_INT
	 unsigned int rev;
	 int digits = log10(num) + 1; //Tells us how many digits are in the number
	 int i; //used to loop though digits...
	 rev=0; //Initialize....
	 for (i=0; i<digits; i++)
	 {
		  rev *= 10; //Make room for the next digit...
		  rev += (num % 10); //add in the next digit.  123 % 10 = 3
		  num /=10; //Shift the digits to the right... 123/10 = 12
	 }
	 
	 cout << digits <<" " << rev <<"\n";
	 return 0;
}


The code above will need to be customized of course. I would make a function out of it so that you main routine can just call the function.

Once you have a reversed digit number all you need to do is use a standard if (a==b ) to tell if the two are equal.



Well, I think honestly you go further than necessary in determining the digits. You could simply implement a while loop and check to make sure that num is greater than 0. It may not account for a leading 0, but nor will log10, but log 10 is also prone to errors(although rather insignificant). Say if you enter 999998 your result may be 8999990 adding an extra 0. If all you check is whether there is a value at all, you know you need to do another iteration or not.

It's basically using log10 to count digits, when you're already counting digits by dividing by 10 each time. How many digits are in the number? Keep track of how many times the loop ran, and keep the loop running until it's out of digits. We don't need to predetermine how long the loop will run, it'll figure itself as each digit is chopped off of num.

This post has been edited by drshorty: 29 March 2009 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is online

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Reverse Number Order

Posted 29 March 2009 - 05:29 PM

lol. OK... I can see your point I suppose. Though I would like to see some proof the log10 might be error prone -- every example I run seems to work just fine... its mathematically sound so if there is a known bug in log10 I would like to know.

It was just an example....

[edit]
I find it amusing that this is such a controversial bit of code. This is not the first time I have had to defend it. I realize that this is not the only way to build a bike shed. I like the log10 line because when allocating memory for a string to hold the number you will need to know the number of digits before you do the conversion. While it maybe true that a while-loop maybe faster this is clearer. Yes it is overkill for the purpose of reversing an integer, but in truth it would probably be better to do this as a string and never even touch integers.
Was This Post Helpful? 0
  • +
  • -

#10 drshorty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 09

Re: Reverse Number Order

Posted 29 March 2009 - 07:22 PM

View PostNickDMax, on 29 Mar, 2009 - 04:29 PM, said:

lol. OK... I can see your point I suppose. Though I would like to see some proof the log10 might be error prone -- every example I run seems to work just fine... its mathematically sound so if there is a known bug in log10 I would like to know.

It was just an example....

[edit]
I find it amusing that this is such a controversial bit of code. This is not the first time I have had to defend it. I realize that this is not the only way to build a bike shed. I like the log10 line because when allocating memory for a string to hold the number you will need to know the number of digits before you do the conversion. While it maybe true that a while-loop maybe faster this is clearer. Yes it is overkill for the purpose of reversing an integer, but in truth it would probably be better to do this as a string and never even touch integers.


Well, it's actually just a very unlikely situation that the user will input a value that log10 will not calculate correctly on a machine. It really won't affect anything except for high numbers, like 9999998 returns a value of 89999990 using that code on my machine(returning an extra 0). log10 causes a precision issue because instead of going on a digit-by-digit basis, were determining a very precise number and truncating it. For larger numbers a precision error is enough to push it over to counting 1 more digit than actually exists, although again it will be a very high number very close to the next highest log10 exponent before it happens.

And yah I agree strings would be better. Using math we're not going to be able to reverse a number that starts out with any amount of 0's. Using log10 for a string though would be a good way, but at the same time it still isn't solving the issue of leading 0s. So if we're using log10 with a string, we're still missing the main benefit of using the string anyway, --EDIT: I realized that there is one more bigger benefit dealing with overflow and more freedom on the size of the number going in--. With that in mind why allocate memory for a string at all when implementing it doesn't provide that advantage.

This post has been edited by drshorty: 29 March 2009 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#11 drshorty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 09

Re: Reverse Number Order

Posted 29 March 2009 - 07:54 PM

Here's my code, using your concepts for switching digits:

#include <iostream>

int main()
{
	using namespace std;

	unsigned int userNum;
	cout << "Enter a number: ";
	cin >> userNum;

	unsigned int reverseNum = 0;

	while(userNum > 0)
	{
		reverseNum *= 10;
		reverseNum += (userNum % 10);
		userNum /= 10;
	}

	cout << "Reversed: " << reverseNum << endl;

	return 0;
}


This post has been edited by drshorty: 29 March 2009 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is online

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Reverse Number Order

Posted 30 March 2009 - 06:21 AM

Quote

Well, it's actually just a very unlikely situation that the user will input a value that log10 will not calculate correctly on a machine.
In programming you just can't take that chance. We should never say, "well yea its possible, but its unlikely that the user will ever do that." -- because users will.

I suppose that there are times when a protocol is given when we can get away with saying, "Well, that should never happen because the protocol forbids it." -- but even then it tends to happen anyway.

in the above program the truncation of the log10() should always be 1 less than the # of digits. Since on your machine it is not then it is clear the your compiler chose:

float log10(float x); rather than double log10(double x); -- this would be fine if you int was 16 bits, but since you can enter a number like 999998 your int must be 32 bits (999998 would only be 16958 which when reversed would have given you 20425)

So if I were to change the code to:

int digits = log10((double)num) + 1;

That should correct the issue that you are seeing. If it does not that you compiler or processor is broken.

Of course this little bit of code has the big set back that if you enter a value whose reverse-digit representation is > MAX_INT then it will return a false answer.

basically it is far better to do this in strings. :)
Was This Post Helpful? 0
  • +
  • -

#13 drshorty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 09

Re: Reverse Number Order

Posted 31 March 2009 - 09:11 AM

View PostNickDMax, on 30 Mar, 2009 - 05:21 AM, said:

Quote

Well, it's actually just a very unlikely situation that the user will input a value that log10 will not calculate correctly on a machine.
In programming you just can't take that chance. We should never say, "well yea its possible, but its unlikely that the user will ever do that." -- because users will.

I suppose that there are times when a protocol is given when we can get away with saying, "Well, that should never happen because the protocol forbids it." -- but even then it tends to happen anyway.

in the above program the truncation of the log10() should always be 1 less than the # of digits. Since on your machine it is not then it is clear the your compiler chose:

float log10(float x); rather than double log10(double x); -- this would be fine if you int was 16 bits, but since you can enter a number like 999998 your int must be 32 bits (999998 would only be 16958 which when reversed would have given you 20425)

So if I were to change the code to:

int digits = log10((double)num) + 1;

That should correct the issue that you are seeing. If it does not that you compiler or processor is broken.

Of course this little bit of code has the big set back that if you enter a value whose reverse-digit representation is > MAX_INT then it will return a false answer.

basically it is far better to do this in strings. :)


Well I wasn't disagreeing that we should attempt to catch all, I was just stating for our purposes it probably wasn't significant, but it does happen(unless we're using a double, which I do have to explicitly state in my compiler) as log10(9999998) is actually a really precise number with quite a lot of 9s, like 7.999999---- after the decimal. A double does fix it, and I did do that. But we don't really have to go that far anyway. Also at the same time, we're declaring two unnecessary variables. But in the instance we find we haven't given enough precision, we won't have to worry about truncation, we'll have to worry about the number(because of precision issues) bumping up 1. And as far as I can tell, you're being overly defensive of your code. There's a better and more reliable method using mathematics where you won't have to account for precision at all.

And I did work with it to make it work using double. I'm merely pointing out the problems with it, and you're merely saying the problem is actually somebody else or somebody else's machine, or their compiler. There is a simpler and more effective, less breakable code(and less resource consuming). I don't understand the need to defend log10, it has its places, but there is a better way.

This post has been edited by drshorty: 31 March 2009 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1