Hi im trying to create a function to multiply two linked lists filled with integers.
I have a function that correctly adds two already.
I have a function that multyplies one list by 10(ie adds a 0 to end)
I have a function that reverses the list.
I have a function that multiplies two digits and returns the result along with any carry. (shown later)
when i first input numbers they go in backwards.
So inputing the number 43 generates a list of 3->4.
The add function gives the answer non backwards tho so adding 3->4 and 2->5 gives the list 5->9.
Now i need to create a multiply function. I just need a basic idea of how to do it.
Basically im thinking given two lists N1 and N2 to multiply:
Keep the first digit in N1 and multiply it with every number in N2 to get a list. Then add this list to 10 times the list generated by taking the second digit of N1 and multiplying by all the digits in N2.
Continue doing this untill all numbers in N1 have been exhausted and continually multiply the adding number by a different power of 10. Basically exactly how one would do it in real life.
Im having trouble seeing the code for this however.
So here is my multiply two digits function:
Code:
int multiply_two_digits(int d1, int d2, int *carry_in, int *result, int *carry_out){
*result = d1*d2+*carry_in;
*carry_out=*result/10;
if(*result>=10){
*result=*result%10;
}
return 0;
}
which returns the result and any carry.
Any ideas or tips? Everytime i try writing this function it gets way to complicated and i cant figure it out. Thanks for any help.