Hw 5 Hints
Managing Complexity Many different ways to solve this problem Write and test one function at a time Try to use a statically allocated array with say, 100 char Convert to a dynamically allocated array later Creating little helper functions Reverse string Multiply up to 10 Max, min
Debugging Remember to check operands of different lengths Use a global debugging counter to track memory allocation Update the counter whenever new and delete are called
The Least Significant Digit First Integer to string Addition
Integer to String 123 -> “123” 123 % 10 = 3, ‘3’ = 3 + ‘0’ 123 / 10 = 12 12 % 10 = 2, ‘2’ = 2 + ‘0’ 12 / 10 = 1 1 % 10 = 1, ‘1’ = 1 + ‘0’ 1 / 10 = 0 // terminate
Addition 999 + 99 ------------ 8 98 999 + 99 ------------ 098 1098
The Most Significant Digit First Multiplication Comparison of two numbers of the same length
Multiplication 1111 X 111 ------------ + 1111 ------------- 12321
Same as… 1111 X 111 ------------ + 1111 ------------- 12321
Same as… 1111 x 111 = ((1 x 1111) x 10 + 1 x 1111) x 10 + 1 x 1111
Comparison If two numbers have the same length The most significant digit decides which number is bigger If they are the same, compare the next digit
Some Possible Ways to Store 123 “123” Easier to print and extract input Easier to write the string conversion constructor Easier to multiply Easier to compare numbers “321” Easier to write the integer conversion constructor Easier to add two numbers Lowest significant digit first