Hey Friends,
I spent Friday afternoon trying to solve a pretty simple problem in Objective-C. I played around with it for two hours before taking 10 minutes to write it in Java. I've got to ask the question, what the heck am I doing wrong? I don't have a lot of experience with C-arrays and memory management. Furthermore, I'm just not sure if an NSArray of objects is the solution to this problem.
I pose a question to the gurus of the language, as I am still relatively new to it (4 months in). The following code is Java. What is the best way (or what are some ways) to translate this code into Objective-C?
PS: if anyone is wondering what the purpose of this code is, it is the algorithm of "Equal Additions" a method of subtracting by hand. Obviously, it would be easier to code num1-num2 but that is not my purpose. Also, I never fully debugged this Java code to make sure it did what I wanted it to do, but it looks like it works with any integer where String1 >= String2 and String1's length is equal to String2's length.
Thanks for the help. Happy Travels!
< Buffalo >
I spent Friday afternoon trying to solve a pretty simple problem in Objective-C. I played around with it for two hours before taking 10 minutes to write it in Java. I've got to ask the question, what the heck am I doing wrong? I don't have a lot of experience with C-arrays and memory management. Furthermore, I'm just not sure if an NSArray of objects is the solution to this problem.
I pose a question to the gurus of the language, as I am still relatively new to it (4 months in). The following code is Java. What is the best way (or what are some ways) to translate this code into Objective-C?
Code:
[COLOR="SeaGreen"]/** [B]Java Code[/B] **/[/COLOR]
private static String equalAdditions(String s) {
[INDENT]String[] terms = s.split(" "); [COLOR="SeaGreen"] // Input String: "123 456"[/COLOR]
char[] a = terms[0].toCharArray(); [COLOR="SeaGreen"]// Char Array a: { '1', '2', '3' }[/COLOR]
char[] b = terms[1].toCharArray(); [COLOR="SeaGreen"]// Char Array b: { '4', '5', '6' }[/COLOR]
String diffString="";
for(int i=b.length-1; i>=0; i--) {
[INDENT]if(a[i]<b[i]) {
[INDENT]int x=( (int) a[i]+10 ) - ( (int) b[i] );
diffString = x + "" + diffString;
b[i-1]++;[/INDENT]
}
else {
[INDENT]diffString = "" + (a[i]-b[i]) + diffString;[/INDENT]
}[/INDENT]
}
return diffString;[/INDENT]
}
PS: if anyone is wondering what the purpose of this code is, it is the algorithm of "Equal Additions" a method of subtracting by hand. Obviously, it would be easier to code num1-num2 but that is not my purpose. Also, I never fully debugged this Java code to make sure it did what I wanted it to do, but it looks like it works with any integer where String1 >= String2 and String1's length is equal to String2's length.
Code:
[B][U]Algorithm:[/U][/B] (not important but provided for the curious)
Input: String representation of two numbers
Output: number1 - number2
[I][B]Assume[/B] both numbers are the same amount of digits
[B]Assume[/B] number1 is >= number2 (ie: no negative outputs)[/I]
1: Put two numbers into two separate arrays of single digits
2: Work backwards subtracting digit(n) of Number2 from digit(n) of Number1, altering array where necessary.
[INDENT]If Number1.digit(n) < Number2.digit(n), add 10 to Number1.digit(n), add 1 to Number2.digit(n-1).[/INDENT]
3: Return the difference of Number1 and Number2.
Thanks for the help. Happy Travels!
< Buffalo >