|
From: Jesper E. <hag...@ho...> - 2001-04-19 11:25:01
|
How do I do if I want to use a number backwards? For example: If I give the variable 'n' the number 12345 and want to use it as 54321. In cout for example. Can i retype it or what? |
|
From: Bolek V. <vr...@pa...> - 2001-04-19 11:47:01
|
> How do I do if I want to use a number backwards?
> For example: If I give the variable 'n' the number 12345
> and want to use it as 54321. In cout for example.
unsigned backwards( unsigned n)
// Reverse order of digits (in decadic)
{
unsigned result = 0;
while (n > 0) {
result = 10*result + n % 10;
n = n / 10;
}
return result;
}
--
============================================================
Ing. Boleslav Vrany
Stanice techniku DDM hl. m. Prahy
Pod Juliskou 2a
Praha 6 - Dejvice
16000
Czech Republic
tel. (420 2) 243 10 240
|
|
From: Lorenzo <co...@in...> - 2001-04-19 11:51:57
|
> How do I do if I want to use a number backwards? > For example: If I give the variable 'n' the number 12345 > and want to use it as 54321. WHY? Though you can convert int to string with itoa and use the strrev to invert order of character and convert string to int with atoi. |
|
From: Bolek V. <vr...@pa...> - 2001-04-19 14:09:47
|
> > How do I do if I want to use a number backwards?
> > For example: If I give the variable 'n' the number 12345
> > and want to use it as 54321.
>
> WHY?
> Though you can convert int to string with itoa and use the strrev to invert
> order of character and convert string to int with atoi.
My function is much more effective than the above way. It doesn't need
auxiliary arrays and works in a very straightforward way. The only thing
you must take care of is that if you want to use it for signed numbers
then:
a) I am not sure about behaviour of the % operator for negative numbers.
b) abs(minimum int, long, ...) might be > maximum int, long, ... => you
cannot simply check the sign and convert -number
Moreover - itoa is not a part of C/C++ standard.
--
============================================================
Ing. Boleslav Vrany
Stanice techniku DDM hl. m. Prahy
Pod Juliskou 2a
Praha 6 - Dejvice
16000
Czech Republic
tel. (420 2) 243 10 240
|
|
From: Lorenzo <co...@in...> - 2001-04-19 14:47:30
|
> My function is much more effective than the above way. You are right!! congratulations i have answered this because i don't know how Jesper Ekberg wants to use this function. And i ask again: Jesper Ekberg why? :-) |
|
From: Bolek V. <vr...@pa...> - 2001-04-19 14:27:55
|
BTW, you can modify my function to revert the number in any base:
unsigned backwards( unsigned n, unsigned base) throw( invalid_argument)
// Reverse order of digits of n in base base
{
if (base < 2) throw invalid_argument( "Base must be > 1");
unsigned result = 0;
while (n > 0) {
result = base*result + n % base;
n = n / base;
}
return result;
}
Works for any base > 2, even for 312, for example.
However, it has a disadvantage to converting via string. If you have a
number like 12345000 and want 00054321, you get just 54321 - unless you
set the output field width properly which requires further algorithm.
--
============================================================
Ing. Boleslav Vrany
Stanice techniku DDM hl. m. Prahy
Pod Juliskou 2a
Praha 6 - Dejvice
16000
Czech Republic
tel. (420 2) 243 10 240
|
|
From: Purohit P. J.. <pra...@si...> - 2001-04-20 07:03:39
|
Hello,
To use a number backward first you need to separare the digits of =
the no.
To do this say you have nNo=3D5234
To separate thousands digit ie 5 thousnad_digit =3D nNo/1000;
To get the hundred digit ie 2 hunreds_digit =3D =
(nNo%1000)/100;
( In above nNo%1000 will give 234 and then 234/100 will give =
2 )
To get tens digit ie 3 tens_digit =3D =
(nNo%100)/10;
( In above nNo%100 will give 34 and then 34/10 will give 3 )
To get the units digit ie 4 unit_digit =3D (nNo%10);
( In Above nNo%10 Will give 4 )
% is Called MODULUS OPERATOR and it give remainder of a =
division.
Now you have separated all the digits of original no now to revert =
the no. simply change the units digit to thousands digit,
tens digit to hundreds digit, hundreds digit to tens digit, thousands =
digit to unit digit. The nos get reversed.
=20
RevNo =3D unit_digit*1000 + tens_digit*100 + hundreds_digit*10 + =
thousand_digit ;
=20
( This above expression is equal to RevNo =3D 4*1000 + 3*100 =
+ 2*10 + 5 ; therefore RevNo=3D4325 )
Haha we reversed the no.
Bye.,
Prahlad Purohit
----- Original Message -----=20
From: "Lorenzo" <co...@in...>
To: <dev...@li...>
Sent: Thursday, April 19, 2001 5:21 PM
Subject: Re: [Dev-C++] Number backwards?
>=20
>=20
> > How do I do if I want to use a number backwards?
> > For example: If I give the variable 'n' the number 12345
> > and want to use it as 54321.
>=20
> WHY?
> Though you can convert int to string with itoa and use the strrev to =
invert
> order of character and convert string to int with atoi.
>=20
>=20
>=20
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Bolek V. <vr...@pa...> - 2001-04-21 17:19:09
|
Hi,
what palindrom numbers are?
> Hello,
> To use a number backward first you need to separare the digits of
> the no.
>
> To do this say you have nNo=5234
> To separate thousands digit ie 5 thousnad_digit = nNo/1000;
> To get the hundred digit ie 2 hunreds_digit =
> (nNo%1000)/100;
> ( In above nNo%1000 will give 234 and then 234/100 will
> give 2 )
> To get tens digit ie 3 tens_digit =
> (nNo%100)/10;
> ( In above nNo%100 will give 34 and then 34/10 will give 3 )
> To get the units digit ie 4 unit_digit = (nNo%10);
> ( In Above nNo%10 Will give 4 )
>
> % is Called MODULUS OPERATOR and it give remainder of
> a division.
>
> Now you have separated all the digits of original no now to revert
> the no. simply change the units digit to thousands digit,
> tens digit to hundreds digit, hundreds digit to tens digit, thousands
> digit to unit digit. The nos get reversed.
>
> RevNo = unit_digit*1000 + tens_digit*100 + hundreds_digit*10 +
> thousand_digit ;
>
> ( This above expression is equal to RevNo = 4*1000 + 3*100
> + 2*10 + 5 ; therefore RevNo=4325 )
Sorry for talking about my function again. I am not sure what Jesper
Ekberg needs to do - if he needs to convert 00012345 to 54321000 then
the above is probably the best algorithm - provided you know the maximum
number of digits. However, if he needs to convert 00012345 to just
54321, then he needs to find the number of digits first. That's why it
is better to do it this way:
unsigned backwards( unsigned n)
// Reverse order of digits (in decadic)
{
unsigned result = 0;
while (n > 0) {
result = 10*result + n % 10;
n = n / 10;
}
return result;
}
Sorry for not explaining it in previous mails. result is simply the
result. 10*result shifts the result one digit left. n % 10 is the last
digit of n and n = n/10 shifts n one digit right. So assuming n=12345 at
the beginning it does the following:
result=0, n=12345
first iteration:
result = 10*0 + 5 // == 5
n = 1234;
second it.:
result = 10*5 + 4; // == 54
n = 123;
third it.:
result = 10*54 + 3; // == 543
n = 12;
etc.
This way he needs not know the number of digits.
!!! WARNING: (I should have realized it immediately) There is a problem
you will always have - all algorithms will lead to integer overflow in
some cases. Assume max. unsigned is 65535 and you want to convert 12347.
The correct result is 74321, which outside the range of unsigned. The
correct version of my function is
#include <stdexcept>
#include <limits>
unsigned backwards( unsigned n)
// Reverse order of digits (in decadic)
{
unsigned result = 0, max = numeric_limits<unsigned>.max;
while (n > 0) {
if ((result > max / 10) ||
// avoid overflow in multiplying 10*result
(n % 10 > max - 10*result))
// avoid overflow in adding n % 10
throw overflow_error( "Overflow in function backwards");
result = 10*result + n % 10;
n = n / 10;
}
return result;
}
Or you must either restrict the range of n or the type of result must be
at least one digit wider than the type of n. Might be double result and
unsigned (long) n.
--
============================================================
Ing. Boleslav Vrany
Stanice techniku DDM hl. m. Prahy
Pod Juliskou 2a
Praha 6 - Dejvice
16000
Czech Republic
tel. (420 2) 243 10 240
|
|
From: Fabien V. <fv...@ne...> - 2001-04-21 20:19:33
|
Hi.
Palindrom numbers are numbers which you can "read" from the begining or the
end.
11
151
1551
15251
152251
and so on....
----- Original Message -----
From: "Bolek Vrany" <vr...@pa...>
To: <dev...@li...>
Sent: Saturday, April 21, 2001 2:50 PM
Subject: Re: [Dev-C++] Number backwards?
> Hi,
>
> what palindrom numbers are?
>
> > Hello,
> > To use a number backward first you need to separare the digits of
> > the no.
> >
> > To do this say you have nNo=5234
> > To separate thousands digit ie 5 thousnad_digit = nNo/1000;
> > To get the hundred digit ie 2 hunreds_digit =
> > (nNo%1000)/100;
> > ( In above nNo%1000 will give 234 and then 234/100 will
> > give 2 )
> > To get tens digit ie 3 tens_digit =
> > (nNo%100)/10;
> > ( In above nNo%100 will give 34 and then 34/10 will give 3 )
> > To get the units digit ie 4 unit_digit = (nNo%10);
> > ( In Above nNo%10 Will give 4 )
> >
> > % is Called MODULUS OPERATOR and it give remainder of
> > a division.
> >
> > Now you have separated all the digits of original no now to revert
> > the no. simply change the units digit to thousands digit,
> > tens digit to hundreds digit, hundreds digit to tens digit, thousands
> > digit to unit digit. The nos get reversed.
> >
> > RevNo = unit_digit*1000 + tens_digit*100 + hundreds_digit*10 +
> > thousand_digit ;
> >
> > ( This above expression is equal to RevNo = 4*1000 + 3*100
> > + 2*10 + 5 ; therefore RevNo=4325 )
>
> Sorry for talking about my function again. I am not sure what Jesper
> Ekberg needs to do - if he needs to convert 00012345 to 54321000 then
> the above is probably the best algorithm - provided you know the maximum
> number of digits. However, if he needs to convert 00012345 to just
> 54321, then he needs to find the number of digits first. That's why it
> is better to do it this way:
>
> unsigned backwards( unsigned n)
> // Reverse order of digits (in decadic)
> {
> unsigned result = 0;
>
> while (n > 0) {
> result = 10*result + n % 10;
> n = n / 10;
> }
> return result;
> }
>
> Sorry for not explaining it in previous mails. result is simply the
> result. 10*result shifts the result one digit left. n % 10 is the last
> digit of n and n = n/10 shifts n one digit right. So assuming n=12345 at
> the beginning it does the following:
> result=0, n=12345
> first iteration:
> result = 10*0 + 5 // == 5
> n = 1234;
> second it.:
> result = 10*5 + 4; // == 54
> n = 123;
> third it.:
> result = 10*54 + 3; // == 543
> n = 12;
> etc.
>
> This way he needs not know the number of digits.
>
> !!! WARNING: (I should have realized it immediately) There is a problem
> you will always have - all algorithms will lead to integer overflow in
> some cases. Assume max. unsigned is 65535 and you want to convert 12347.
> The correct result is 74321, which outside the range of unsigned. The
> correct version of my function is
>
> #include <stdexcept>
> #include <limits>
>
> unsigned backwards( unsigned n)
> // Reverse order of digits (in decadic)
> {
> unsigned result = 0, max = numeric_limits<unsigned>.max;
>
> while (n > 0) {
> if ((result > max / 10) ||
> // avoid overflow in multiplying 10*result
> (n % 10 > max - 10*result))
> // avoid overflow in adding n % 10
> throw overflow_error( "Overflow in function backwards");
> result = 10*result + n % 10;
> n = n / 10;
> }
> return result;
> }
>
> Or you must either restrict the range of n or the type of result must be
> at least one digit wider than the type of n. Might be double result and
> unsigned (long) n.
> --
> ============================================================
> Ing. Boleslav Vrany
> Stanice techniku DDM hl. m. Prahy
> Pod Juliskou 2a
> Praha 6 - Dejvice
> 16000
> Czech Republic
>
> tel. (420 2) 243 10 240
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|