|
-
Aug 10th, 2003, 02:28 PM
#1
Thread Starter
Hyperactive Member
Anyone know a algorithm to reverse a number's order?.:Resovled:.
Hello everyone,
I'm trying to figure out how i'm suppose to ouput a number reversed, such as, 123 output it as 321.
I'm not allowed to use arrays so i'm thinking there has to be a mathamatical way of doing this. There is no fixed amount of numbers, it can be 2 to whatever the user wants to enter, any idea's? Like this almost worked....
input: 155
155%10 = 5;
15%10 = 5;
10%10 = 0;
almost got 551
% is mod in C++ but u all probably knew that just making sure!
Thanks for listening
Last edited by voidflux; Aug 17th, 2003 at 12:40 PM.
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Aug 10th, 2003, 04:24 PM
#2
This is VB but...
VB Code:
Private Sub Form_Load()
Dim Inp As String
Dim i As Integer
Dim Res As String
Inp = InputBox("Enter a number")
Res = ""
For i = Len(Inp) To 1 Step -1
Res = Res & Mid$(Inp, i, 1)
Next
MsgBox Res
End Sub
Has someone helped you? Then you can Rate their helpful post. 
-
Aug 10th, 2003, 11:01 PM
#3
Thread Starter
Hyperactive Member
Thanks for your reply, here's what comerak showed me:
Code:
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Please enter a number (integer): ";
cin >> number;
while (number)
{
cout << number%10 << " ";
number/=10;
}
cout << "\n";
return 0;
}
works like a charm!
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Aug 12th, 2003, 06:56 AM
#4
-
Aug 12th, 2003, 12:06 PM
#5
Thread Starter
Hyperactive Member
If it was up to me I would have just stored each digit into an array and then started with the last element and iterate to the first element but arrays weren't allowed nor STL
THanks for the advice though!
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Aug 12th, 2003, 12:41 PM
#6
Re: Anyone know a algorithm to reverse a number's order?
Originally posted by voidflux
Hello everyone,
I'm trying to figure out how i'm suppose to ouput a number reversed, such as, 123 output it as 321.
I'm not allowed to use arrays so i'm thinking there has to be a mathamatical way of doing this. There is no fixed amount of numbers, it can be 2 to whatever the user wants to enter, any idea's? Like this almost worked....
input: 155
155%10 = 5;
15%10 = 5;
10%10 = 0;
almost got 551
% is mod in C++ but u all probably knew that just making sure!
Thanks for listening
10%10 = 0 -- yep, that's true.... mod returns the remainder after division
1%10 = 1
2%10 = 2
3%10 = 3
4%10 = 4
5%10 = 5
6%10 = 6
7%10 = 7
8%10 = 8
9%10 = 9
10%10 = 0
-
Aug 15th, 2003, 08:14 PM
#7
What if you are told its a number, but its base is not fixed at ten,
ie, WIF its base is 9, 12, 16, anything? And, they use Alpha Chars for numerical representation?
I'd stick with mid if I were you.
-Lou
-
Aug 15th, 2003, 08:18 PM
#8
Hmmm, makes me wonder:
If we have some base ten number, perhaps 123456, and represent it as K in hex,
if we say that 654321 = reversed{123456},
is 654321 = Reversed{K}?
-Lou
-
Aug 16th, 2003, 08:39 PM
#9
Fanatic Member
C++ strings arent usually considered arrays, so you can use strings, and STL reverse command. or if you want mathematics,
int reverse(int n){
int t=0;
for (;n>0;n/=10) t=t*10+n%10;
return t;
}
doesnt work with negative numbers or decimals
Massey RuleZ! ^-^__  Cheers!  __^-^ Massey RuleZ!
Did you know that...
The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!
-
Sep 19th, 2003, 07:09 AM
#10
New Member
Reversing the Digits
Reversing the digits (in binary) is an important step in unscrambling the output of the Fast Fourier Transform. The only fast way I know to do it is the memory-consuming table lookup. Some compromises are possible, such as limiting the table size to 256 entries to give instant reversal of 8 bits at a time, then reversing the bytes (instead of bits) in software.
Reversing the bytes might be done by strings, as previously shown. But in your case, you are working with integers in number base ten, so you could construct while decimating:
R is the result, N is the number to be reversed.
R=0
while N>0
R:=10*R+mod(N,10)
N=N/10
wend
Only integer arithmetic is used here, and minimal memory... HOWEVER, EXTREME WARNING!!!! NOT VALID FOR NEGATIVE NUMBERS! I suspect that using the string method is equivalent to this, with the loop and logic efficiently coded by VB and invisible to you.
I apologise for any contamination by HotPaw basic. My Palm is my main machine.
-
Sep 26th, 2003, 09:03 PM
#11
One final word on the string reverse: you can use the StrReverse function instead of a loop
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Sep 29th, 2003, 04:15 PM
#12
-
Sep 29th, 2003, 04:58 PM
#13
From his sig.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|