Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Confusion with a multiplication loop...

Greenhorn
Posts: 29
So I finally have my program nearly running, but am having some problems with getting my multiplication to work properly:

I think it's a problem with carrying, and it throws my numbers off quite a bit...sometimes. Smaller numbers, where you don't have to carry or only have to carry a few places, it's no problem. But for some reason once you get into carrying quite a bit, it's a huge problem. It's difficult to debug it because it requires going through some LONG multiplication. So I am hoping somebody will be able to spot something in my code that I can't.

Thanks!

(For example: 123*456 yields 41310108 when it should be 56088 - that's a pretty significant 'carrying' error!)

Marshal
Posts: 57482
175
Do you remember how you were taught long multiplication at the age of 8 at school? That should give you the algorithm to follow.

Ranch Hand
Posts: 230
Your ten's place variable is named "high" so why are you possibly adding it twice. I would rethink this logic:

You are saying if the ten's place is greater than 1 add that value to a certain place in the answer array. Then...always add that value to an additional place in the array? I suspect this isn't what you meant to do.

Lastly, each index inside your array should not have values greater than 10 because each one of them is supposed to be representative of 1 position in a normally represented number. I suspect that when you are adding your ones place and tens place number to answer it goes above 10 and when this happens you are not carrying. So when you add high and low to your array I would check in case this number goes above 9 redo your carry logic.

For example if you have 9 9 9 inside your answer array and you add 1 to the value at index 2 you currently are not checking for values > 9 so it would appear 9 9 10 when you printed it instead of checking.

D diller
Greenhorn
Posts: 29
Yeah it was something along those lines exactly, just figured it out actually. Thanks for the tips!

Campbell Ritchie
Marshal
Posts: 57482
175
Well done

Bartender
Posts: 1732
43
hey i am intrested in this solution too

I had once made a division algo using strings, it works so well i could even deploy it as a j2me project
into my mobile phone !!

btw a (very very) unoptimal method is "additive multiplication"
eg to calculate 5*3, calculate 5+5+5,

This will not be optimal when it comes to thousands and ten thousands,
But, I dont know why (I have nothing to support my theory),
I think the trade off will not be as much as we expect it to be.

After all, there are middle levels of multiplications involved AND middle levels of additions (with carries) involved for every digit in the multiplier

Just a thought ......

 If you are using a rototiller, you are doing it wrong. Even on this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS