+ 1

Could you please look into the logic of my Java program and help me find what's wrong in it.

Hi, I am trying to write a java code that can multiply any two numbers existing in any (one) number system(like binary, decimal, etc) and display the result in the same number system. The issue is, the code is working fine for smaller numbers and failing for larger input values. My code: https://code.sololearn.com/czSTtV3yzooX You may refer to the complete question & answer from here: https://www.pepcoding.com/resources/online-java-foundation/function-and-arrays/any_base_multiplication/topic Overview of my program: I am taking two numbers and their number system(say 2 for binary, 8 for octal, 10 for decimal systems) as inputs. In the 1main, I am extracting each digit of a number, multiplying it with the other number, and summing all my results based on the place values. In the 2nd method, I am actually multiplying the digit with the number and sending it to the main. In the 3rd method, I am summing the input values in the number system and returning the result.

5th Mar 2022, 6:27 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
12 Respostas
+ 3
The only problem I found with some tests is in the sumInSystem method, line 49, that should be: ans += d * multiplier;
5th Mar 2022, 1:16 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 3
I suppose that the complexity of your code is intentional and part of your practice. Because it is obvious that you can achieve the same result much faster. I guess that "failing for larger input values" means especially larger input values and a low base value (e.g. base 2). Because those numbers quickly grow in length. Since you work exclusively with numeric types, in particular int, the largest int is 2^31-1 or roughly 10^10. That is, the maximum length in digits is about 10. That limits the largest posiible value in base 2 to 2^10-1, and that is 1023, decimal. Other bases have other limits, but that would be the lowest. I also just noticed that it doesn't work for smaller values either. So, there must be a logical flaw there, too. But that will take a more thorough analysis.
5th Mar 2022, 7:28 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Ani Jona 🕊 ok, thank you.
5th Mar 2022, 5:47 PM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
+ 2
Why is this not yet solved? 2156 74 8 results in 204710, as expected after making that one change I mentioned (line 49).
12th Mar 2022, 5:36 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Oh, sorry for not making myself clearer. Good that it works now 👍
12th Mar 2022, 6:46 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
Ani Jona 🕊 Thanks for looking into my code. Yes, the complexity is intentional. Yes, there's another way for this: converting the given numbers from the given system into decimal, multiplying them and then converting the product back to the given system. Please let me know if there's yet another way. Thanks for the insight into the largest possible for binary. And Yes, I am sure there's a logical flaw in the code and it's more prevalent for larger numbers (however it still exists for smaller numbers, after all it's a logical flaw). I am still trying to figure out that flaw. A small help for that will be greatly appreciated. Thanks once again for your time.
5th Mar 2022, 10:24 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
+ 1
In passing sumInSystem, just pass num, instead of num*multfactor sum= sumInSystem(sum, num, s ); // multiplicationFactor*=10; And in sumInSystem method int num=d1+d2+q; dont add q try these changes and check.. hoping it works...
11th Mar 2022, 1:05 PM
Jayakrishna 🇮🇳
+ 1
Ani Jona 🕊 Thanks a lot. I apparently misinterpreted the statement (The only problem I found...). I have made the change and it's working fine now.😊
12th Mar 2022, 6:42 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
+ 1
Ani Jona 🕊 It's just a small language barrier 😄, nothing to be sorry. I am grateful for your help.
12th Mar 2022, 6:48 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
+ 1
Oh. My mistake, that I tried for addition instead of multiplication.. 😌 It's completely wrong then. Hope am not confused much..... Hope fine now.. 👍
12th Mar 2022, 9:52 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 ya, it happens sometimes for everyone. Yes, it's completely fine now. Thank you
12th Mar 2022, 10:13 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
0
Jayakrishna🇮🇳 I have made the changes that you stated, and still failing. The inputs I am giving for testing are 2156,74, 8 and the output should be 204710. Update: There was a small flaw in the logic of the SumInSystem for which Ani Jona has suggested correction. Now it's working perfectly fine. Thanks again for going through all of the code for me.😊
12th Mar 2022, 5:26 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar