For a number to be reversed, your instructor may want to see you "break
apart" the number. So 1,234 is really:
1 x 1,000,
2 x 100,
3 x 10
4 x 1.

In a 6 element array, (which will handle everything up to a billion),
we would get these values perhaps:
num[0] == 4, (the one's place)
num[1] == 3, (the ten's place)
num[2] == 2, (the hundreth's place)
num[3] == 1, (the thousand's place)
num[4] == -1, etc.
num[5] == -1

And we need a loop to help us make this breakdown of the number.
int num[6];
(all array elements should be assigned to -1)
unsigned long subtractor = 1000000
int powerTen = 6

while (number > 0)  {
    if subtractor >= number {
       number =  number - subtractor
       subtractor = subtractor / 10
       powerTen = powerTen - 1
       num[powerTen]++
    }
}

//now you need to print out the num array, beginning with num[0], and
stopping when you first reach an array value of -1.


I haven't tested this pseudo code, AT ALL, it's offered here as food
for thought ONLY. You haven't even mentioned what language you're
trying to program with.

The logic is easier to understand with a series of "if" statements, but
you'll learn more by doing it in a loop.

Pretty much the same thing can be done with individual letters
(char's), and you don't have to use any arithmethic. Probably the
easiest thing is just to take the string of char's and "walk" it, with
a pointer, but an array of char's may be easier to understand if you
haven't had pointers yet in your class.

char letters[] = { 'a', 'b', 'c', 'd', '\0' }

for int i highest array element; i >= 0; i--   {
   if letters[i] > = 31 //ASCII 32 is a space, and you'll want that
      print letters[i]
}

If finding the length of the string is a problem, you can just make the
letters array really large (like 256 or something), and then just start
"walking" down the array until you reach some letters. You should fill
the array with some impossible value, like 0. If you leave it with just
who knows what in it, you may get quite a surprise! :)

Hope that helps, but also makes you "stretch" a bit to learn more about
it.

Adak


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to