buffingtonr
Nov 11, 2008, 08:42 PM
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?
/** Java Code **/
private static String equalAdditions(String s) {
String[] terms = s.split(" "); // Input String: "123 456"
char[] a = terms[0].toCharArray(); // Char Array a: { '1', '2', '3' }
char[] b = terms[1].toCharArray(); // Char Array b: { '4', '5', '6' }
String diffString="";
for(int i=b.length-1; i>=0; i--) {
if(a[i]<b[i]) {
int x=( (int) a[i]+10 ) - ( (int) b[i] );
diffString = x + "" + diffString;
b[i-1]++;
}
else {
diffString = "" + (a[i]-b[i]) + diffString;
}
}
return diffString;
}
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.
Algorithm: (not important but provided for the curious)
Input: String representation of two numbers
Output: number1 - number2
Assume both numbers are the same amount of digits
Assume number1 is >= number2 (ie: no negative outputs)
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.
If Number1.digit(n) < Number2.digit(n), add 10 to Number1.digit(n), add 1 to Number2.digit(n-1).
3: Return the difference of Number1 and Number2.
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?
/** Java Code **/
private static String equalAdditions(String s) {
String[] terms = s.split(" "); // Input String: "123 456"
char[] a = terms[0].toCharArray(); // Char Array a: { '1', '2', '3' }
char[] b = terms[1].toCharArray(); // Char Array b: { '4', '5', '6' }
String diffString="";
for(int i=b.length-1; i>=0; i--) {
if(a[i]<b[i]) {
int x=( (int) a[i]+10 ) - ( (int) b[i] );
diffString = x + "" + diffString;
b[i-1]++;
}
else {
diffString = "" + (a[i]-b[i]) + diffString;
}
}
return diffString;
}
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.
Algorithm: (not important but provided for the curious)
Input: String representation of two numbers
Output: number1 - number2
Assume both numbers are the same amount of digits
Assume number1 is >= number2 (ie: no negative outputs)
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.
If Number1.digit(n) < Number2.digit(n), add 10 to Number1.digit(n), add 1 to Number2.digit(n-1).
3: Return the difference of Number1 and Number2.
Thanks for the help. Happy Travels!
< Buffalo >
