PDA

View Full Version : even more, how is this possible

Orphious
11-21-01, 08:09 AM
Stu, is it possible to display a number in reverse order using the % operator. Like have 12345 and return it 54321 using this thing %. Doesn't that give you the remainder?

Thanks

kanarde
11-23-01, 08:36 PM
I'm not Stu, but I'll try to help :)

The % operator is the same as the mod operator in VB. It returns the remainder of a division problem.

10 / 7 = 1 remainder 3
10 % 7 = 3

10 / 3 = 3 remainder 1
10 % 3 = 1

10 / 2 = 5 remainder 0
10 % 2 = 0

Got it? Good

As for reversing the numbers... Thats for you to figure out 8-)

Orphious
11-23-01, 09:59 PM
But I don't. I haven't figured out how you can reverse a number with a modulus operator or even why. Something with recursion perhaps? Hell I don't know

Stu
11-24-01, 07:17 AM
Sorry I didn't get back to you sooner, I was out of town. Anyhow, I'm assuming you mean to do this in more than one line of code.

So, on with how to tackle this. kanarde explained how the division (/) and modulus (%) operators work on integers--so, I'll just skip over that much. First the number you proposed, 12345, is an integer base ten. Therefore, dividing the integer 12345 by 10 results in 1234 and taking the modulus of 12345 and 10 yields 5. Following this, we can take the modulus of 1234 and 10 (which is 4) and add it to the previous result (5) times 10, yielding 54. We can continue this until the whole number reverses. Here is a code example:

#include <iostream>

int reverse_int(int);
int reverse_int(int input){

int output = 0; // Workspace for the
// reversed int to be built

// While there are more
// digits to deal with, loop...
while(input != 0){

// First, use modulus (%) and
// add the remainder to the output times ten...
output = (output * 10) + (input % 10);

// Next, divide input by ten to get rid of the
// digit we just "appended" to the output...
input /= 10;

} // End while

return output;

} // End reverse_int()

int main(int argc, char *argv[]){

int i = 12345;

cout << i
<< " reversed is "
<< reverse_int(i)
<< '\n'
<< ends;

i = 123456789;

cout << i
<< " reversed is "
<< reverse_int(i)
<< '\n'
<< ends;

} // End main()

Keep in mind, however, this does not deal with the case of a number that--in its original form--is a multiple of ten. For instance, 1230 will reverse to 321, because base ten numbers--in C/C++--have no leading zeroes. Only base eight (octal) numbers have leading zeroes (and base sixteen (hexadecimal) numbers are represented with a leading 0x).

The easiest way around this is to prepend a zero when the modulus of original integer and ten equals zero. Here is a modified version of the main() from above that does just that.

// Includes and reverse_int() here...

int main(int argc, char *argv[]){

int i = 123450;

cout << i
<< " reversed is ";

// Deal with the the case
// where i is a multiple of 10...
if((i % 10) == 0)
cout << 0;

cout << reverse_int(i)
<< '\n'
<< ends;

i = 123456789;

cout << i
<< " reversed is ";

// Deal with the the case
// where i is a multiple of 10...
if((i % 10) == 0)
cout << 0;

cout << reverse_int(i)
<< '\n'
<< ends;

} // End main()

There are other ways to do this, probably much nicer than above, but this is probably the least complex--and it gets the job done.

Orphious
11-24-01, 03:13 PM
Once again, thank you stu. You are quit the asset for me :D Thanks a bunch, I really appreciate it!!!!

While we are on the subject of int's, is there an easy way to show 1234 as 1 2 3 4 with spaces in between?

Stu
11-25-01, 01:21 AM
I'd convert it to a string then insert the spaces. Example:

#include <iostream>
#include <string>
#include <cstdio> // For sprintf()

int main (int argc, char *argv[]){

int i = 1234; // Initial int value
char *buf; // Buffer to transform int to string
string ws; // Workspace to transform the string
size_t pos; // Position being worked on

// Convert int to char*...
sprintf(buf, "%d", i);

// Assign char* to workspace...
ws = buf;

// Loop through workspace;
// insert spaces between digits...
for(pos = 0; pos < ws.size() - 1; pos++)
ws.insert(++pos, " "); // Insert after the current position

// Print out the result...
cout << i
<< " transformed to |"
<< ws.c_str()
<< "|\n"
<< ends;

} // End main()

Orphious
11-25-01, 02:56 PM
Is this seemingly neverending knowledge just from years of work, or did you just get it and it was always easy for you. Because if it was, I am trying to do the wrong thing, because is sure isn't easy for me.

Stu
11-26-01, 01:35 AM
I've always been pretty good at programming, but I've been programming for a number of years (since about 1984). So, I have a fair amount of experience to draw on. That being said, programming is nothing more than logical problem-solving--regardless of which language you choose to use. If you can take a problem, break it down into less complex parts, and solve those parts, then programming is a piece of cake.

For instance, your previous question "is there an easy way to show [the integer] 1234 as 1 2 3 4 with spaces in between?" This was my thought process:

You can't have stored character spaces in the type integer, but you can in a character string. So, I have to convert the the integer to some kind of character string. Following that, I considered which type/class of character string would be the best to use (char*, char [], or class string) for both ease of use and to satisfy your goal (learning C++). So, I chose the class string. Then all that was left, was to decide how to insert the spaces into the string (I could use an iterator, but that would be a little more confusing than it had to be). The approach I chose was to insert a space after each pre-existing character in the string, except for the final character. To do this I had to know which position I was currently working on (this is where the "size_t pos" came in), and where the next position should be after the string was modified (this is the reason for two "++" operations per loop). I could have chosen to move backwards through the string, which in retrospect probably would look a little cleaner and be a little more optimal ( for(pos = ws.size() - 1; pos != 0; pos--) ws.insert(pos, " "); ). Finally, I needed to decide when I was finished modifying the string, which was when the loop reached the last character in the string (ws.size() is the total character count of the string, but the string starts at cell 0, meaning that the last character is in a position one less than the total character count or ws.size() - 1), otherwise there would be a space outside of the digits.

At that point, all that was left was to code. As you can probably tell, I comment everything. But the fact of the matter is, I write the comments before I write the code--they are in fact my original pseudo-code. I do that so I can read through it, in english, and answer the question "Does this solve the problem I was presented?" before I concern myself with the syntax. It's a lot less likely that you'll get compile errors because you changed the comments, than if you changed the actual code.

SRF01
11-26-01, 03:37 AM
I was always taught to write it out in pseudo-code. That way it was pretty much solved and then fun part was writing the actual code.

It worked very well when I did it, but when I needed the work done fast I usually skipped that step and regretted it later. Now it's a staple no matter what language i'm using. (not that I get to program much anymore...)