• October 8th, 2011, 09:12 PM
Staticity
So, I just came back from a CS Competition, and there was this one question that asked this:
"You will be given a .in file that will contain a number "n" which is the number of times to loop the program. The next lines will consist of two integers each that you will need to add. Develop a program to determine the number of arithmetic carries during this process"

For example: 9 + 1 = 10 ... You carried once

Data Input:
3
123 456
555 555
469 101

Output:
0
3
1

So here's my code that gave me the correct answers for the provided input:

Code :

```import java.util.*;   public class friday { public static void main(String args[]){ Scanner scan = new Scanner(System.in); System.out.println("Enter number of times to run"); int count = scan.nextInt(); while(count > 0){ System.out.println("Enter A:"); int a = scan.nextInt(); System.out.println("Enter B:"); int b = scan.nextInt(); String aword = a+""; String bword = b+""; char array1[] = aword.toCharArray(); char array2[] = bword.toCharArray(); int carries = 0;   for(int i = array2.length; i > 0; i--){ if((array1[i-1] + array2[i-1]) > 9+48+48){ carries++; } }   System.out.println(carries); count--; }   } }```

It doesn't work if the number's lengths are variable.. like A has a length of 2, and B has a length of 3.. It won't work that way, or if you have 999 + 1, the logic behind it wouldn't work either... Can someone point me in the correct direction? My friend who was in the advanced division told me he would use recursion.. But his logic was kind of skewed when he gave me an answer.. Any help would be Greatly Appreciated.
• October 8th, 2011, 09:34 PM
helloworld922
The simplest way is to take your code and force the two arrays to be the same length (whichever is longer). You can do this by adding zeros to the left ("011" = "11" numerically, should work with your code).
• October 8th, 2011, 09:40 PM
Staticity
I don't believe it would work with 49 + 51..
.. Because 9 + 1 is greater than 9 and carries a 1..
However, the 1 doesn't carry over to 5 + 4.. so 5+4 = 9 and won't add a carry..

To do the carrying properly, could I have a separate integer that is set to 0 or 1 depending on the if statement? And it resets back to 0 or 1 after the if statement has been executed? Also, how would I add zeros to the left? I'm not entirely sure on how to do this.
• October 8th, 2011, 09:42 PM
helloworld922
Just add an extra zero. Adding two numbers can never go more than 1 digit over the longer of the two numbers. You will need a separate carry variable.

049 + 051 = 100

You'd count 2 carries.

Code Java:

```if(aword.length() > bword.length()) { // normalize to aword.length() + 1 aword = "0" + aword; int zeros_to_add = aword.length() - bword.length()+1; for(int i = 0; i < zeros_to_add; ++i) { bword = "0" + bword; } } else { // ... basically the same code, except bword is the longer one now. You get handling of equal length numbers here, too }```
• October 8th, 2011, 10:06 PM
Staticity
Say I wanted to add 199991 and 9... The leading 0 wouldn't really work. Is there a better resolution? I know I could use a For loop to add 0 to the beginning until the lengths are the same, but that doesn't seem to clean.
• October 8th, 2011, 10:31 PM
helloworld922
I don't see why it wouldn't work (at least in theory, there may be some bugs in the implementation):

0199991
0000009
_______
0200000

It should count 5 carries.

To be honest the for-loops isn't a very clean way to do it. There is another way to do it by creating arrays after you've converted the strings to char arrays and copying the data over and filling the rest with '0', but either way it's going to look messy because you're trying to discretize a very simple operation and quantify something rather "odd". And this isn't the only method available, it's just the one I chose because I assumed your original code worked already with equal-length strings.
• October 8th, 2011, 10:35 PM
Staticity
```import java.util.*;   public class friday { public static void main(String args[]){ Scanner scan = new Scanner(System.in); System.out.println("Enter number of times to run"); int count = scan.nextInt(); while(count > 0){ System.out.println("Enter A:"); int a = scan.nextInt(); System.out.println("Enter B:"); int b = scan.nextInt(); String aword = a+""; String bword = b+""; int carries = 0; int carry = 0;   if(aword.length() < bword.length()) for(int c = aword.length(); c < bword.length(); c++) aword = "0" + aword; if(bword.length() < aword.length()) for(int d = bword.length(); d < aword.length(); d++) bword = "0" + bword;   System.out.println(aword); System.out.println(bword); char array1[] = aword.toCharArray(); char array2[] = bword.toCharArray();   for(int i = array2.length; i > 0; i--){ if((array1[i-1] + array2[i-1]) + carry> 9+48+48){ carry = 1; carries++; } else carry = 0; }   System.out.println(carries); count--; }   } }```