### Help With Adding Reserved Numbers Together

Hello everyone, been snooping these forums all through college and have found a lot of advice so I would first like to thank you for that. Now though I do have a homework assignment that I am quite stumped on. I'll give you the questions first then post what I have.

Question 1: Calculate the reversed numbers from the input. Reversed number is a number written in arabic numerals but the order of digits is reversed (e.g. 543 reversed would be 345). You need to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus you must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).

Input

The input consists of N cases (equal to about 10000). The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the numbers you are to reverse and add together.

Output

For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers (Get the sum of the 2 integers that have been reverses, then reverse that sum also). Omit any leading zeros in the output.

Sample

 ```2 // First line 50 65 // Input 16 // Output 1543 1345 // Input 2888 // Output```

I believe I have the first question solved cause my program does what it is suppose to do (I think?), but I am having trouble with the second bonus question.

Question Two (Bonus Question): Complete this program without using std::string.

Anyways here is my code.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include #include #include std::string convertInt(unsigned number) { std::stringstream ss; ss << number; return ss.str(); } template T StringToNumber (const std::string &Text) { std::stringstream ss(Text); T result; return ss >> result ? result : 0; } int main() { unsigned limit; std::cin >> limit; for (unsigned index = 0; index != limit; ++index) { unsigned one, two; std::string totalString; unsigned totalInt; std::string string1, string2; std::cin >> one >> two; string1 = convertInt(one); string2 = convertInt(two); std::reverse(string1.begin(), string1.end()); std::reverse(string2.begin(), string2.end()); totalInt = StringToNumber(string1) + StringToNumber(string2); totalString = convertInt(totalInt); std::reverse(totalString.begin(), totalString.end()); totalInt = StringToNumber(totalString); std::cout << totalInt << std::endl; } return 0; }``````

If you could help me with the second question that would be great. Right now I'm thinking that it can't be done without converting the numbers to a string because I have been working on this for days and can't find a answer. Also I realize that you guys are not here to just give me the code but if you could point me in the right direction that would be great.

Last edited on
You could create vectors of digits (integers) with each element containing the place value of the associated integer value. Converting from the integer to the vector and from the vector to a reversed integer value would be pretty easy.

Edit: Example: 123 = {1, 2, 3}
Last edited on
Hey Pryious, glad you've gotten good help from us.
And I also really appreciate your method of asking questions. Only once every few months does a new member come by and ask a homework question in such a reasonable manner. I've grown so accustomed to seeing only a homework prompt being posted and nothing else (not even a statement asking us to do their homework, merely throwing out a question and expecting someone to answer.)

So to answer your question, you most certainly can reverse an int without using std::string.
The trick is in the modulus, or remainder, operator (%). The remainder of a number divided by 10 is the most significant digit of the number (farthest to the right). The remainder of 1234/10 = 4. The remainder of 123/10 = 3, etc, etc. Here's an example of how this is useful.
 ``1234567891011`` ``````int number = 1234; while(number != 0) { int mostSignificantDigit = number % 10; //is equal to the rightmost digit of number cout << mostSignificantDigit << endl; number /= 10; //(decrease the number by a factor of ten). //number will equal zero when it divides a single digit number by 10 //because of how integer math is done. }``````
 ```4 3 2 1```

So you could use this bit to create a vector or array of digits, then create a reversed int out of them by multiplying and adding proper digit place. (4 would turn into the thousands place, so newNumber += 4*1000, and 3 is the hundreds place, so newNumber += 3*100, etc).

EDIT:
Or use the recursive method below me.
Man recursion is cool. I can never wrap my brain around it, though.
Last edited on
 ``12345`` ``````unsigned int reverse( unsigned int n, unsigned int part_reversed = 0 ) { if( n == 0 ) return part_reversed ; else return reverse( n/10, part_reversed*10 + n%10 ) ; }``````
Thank you both Thumper for the great advice, and JLBorges for the function for something to base mine around (Don't want to copy and paste just to be safe). For some reason I never thought of that process thank you again.
Topic archived. No new replies allowed.