Why is my solution for adding two binary arrays not working?
-
Following is the problem from CLRS book Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n +1)element array C. State the problem formally and write pseudocode for adding the two integers. I found the pseudocode as Adding-A-B(A, B, C) key â 0 for i â 1 to n do sum â A[i] + B[i] + key C[i] â sum mod 2 key â sum div 2 C[n + 1] â key Here is my code public static void main(String[] args) { int[] a = {1,1,1,1}; int[] b = {1,0,1,0}; int[] c = new int[a.length+1]; StringBuffer sb1 = new StringBuffer(); StringBuffer sb2 = new StringBuffer(); int key = 0; for (int i = 0; i < a.length; i++) { c[i] = (a[i] + b[i] + key) % 2; key = (a[i] + b[i] + key) / 2; } c[a.length] = key; for (int j = 0; j < c.length; j++) System.out.println(c[j]); } Expected output = 11001 Actual output = 00101. 1) May I know where I am making mistake 2) How does module by 2 and div by 2 for key works?
-
Answer:
Did you try to debug it? Edit: Aha!! Your program works perfectly. It;s a feature, not a bug. Probably the problem is that you didn;t realize how arrays work. Since, your least significant integer is at position 0, what you are doing is 0101 + 1111 , not 1010+1111. 0101+1111 = 10100, not 11001. Since you are printing the least significant integer is being printed first, you are printing 10100 as 00101. Debug through the code and you might see the problem. If you want it to work as you expect, you should reverse your for loop
Jayesh Lalwani at Quora Visit the source
Other answers
Firstly I dont think Quora is a place to ask your debugging issues to people. There are other places and forums on the web which are made for these things. Now because you have asked, I will reply. Well you are making your code run from left to right to find the sum, whereas in ur maunal calculations, you do it from right to left. Just invert the arrays a and b, and you should get the answer.
Lokesh Khandelwal
Its a simple mistake of indexing. Addition is done from LSB to MSB. Here you have done the opposite. Otherwise your code is fine.
Amit Gupta
Related Q & A:
- How to merge two arrays of objects in angular?Best solution by Stack Overflow
- Why, when i have set my sign in for two weeks, are you asking for my password every day?Best solution by Yahoo! Answers
- What is it like working two jobs?Best solution by Yahoo! Answers
- Why are there two black ink cartridges in my printer?Best solution by community.usa.canon.com
- Why do eggs float by adding salt to water?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.