# Thread: need qucik help with writing a function

1. ## need qucik help with writing a function

Hi everyone i need help. There are list which are consisting of digits of a number in revers order. For example 456 is in the list like 6 - > 5 -> 4 . I have to write a function which takes two lists like these and add them and write to a new list. For example a list like 2 -> 1 and another like 3 -> 4 -> 2 will reslut two a list like 5 -> 5 -> 2 (12 + 243 = 255 ). I need a qucik help. The fucntion i wrote is always giving errors out.

2. Well, we are not here to do your homework. Why don't you post the code you have so far, and what errors it gives you.

--
Mats

3. well this is the code i wrote.

Code:
```void addition(int hdr1,int hdr2){

digitNodePtr tmp1 = (digitNodePtr)malloc(sizeof(digitNode));
digitNodePtr tmp2 = (digitNodePtr)malloc(sizeof(digitNode));
digitNodePtr tmpLast = (digitNodePtr)malloc(sizeof(digitNode));
tmp1 = headerArray[hdr1]; //temp pointer to traverse first number
tmp2 = headerArray[hdr2]; //temp pointer to traverse second number

//the code part for the first digit of the sum list
digitNodePtr newNodePtr = (digitNodePtr)malloc(sizeof(digitNode));
newNodePtr -> digit = (tmp1 -> digit + tmp2 -> digit) % 10;
if ((tmp1 -> digit + tmp2 -> digit) >= 10 )
carry = 1;
else
carry = 0;

tmp1 = tmp1 -> next;
tmp2 = tmp2 -> next;
free(newNodePtr);

//the part to add the other digits to the list
while( tmp2 != NULL ) {
digitNodePtr newNodePtr = (digitNodePtr)malloc(sizeof(digitNode));
tmpLast -> next = newNodePtr;
tmpLast = newNodePtr;
newNodePtr -> digit = (tmp1 -> digit + tmp2 -> digit + carry) % 10;
if ((tmp1->digit + tmp2->digit + carry) > 10)
carry = 1;
else
carry = 0;

tmp1 = tmp1 -> next;
tmp2 = tmp2 -> next;
free(newNodePtr);
}

//after the add operation if there is a carry it adds 1 at the beginning of the number
if(carry == 1){
digitNodePtr newNodePtr = (digitNodePtr)malloc(sizeof(digitNode));
tmpLast -> next = newNodePtr;
tmpLast = newNodePtr;
newNodePtr -> digit = 1;
free(newNodePtr);
}

}```

for example 5 and 6 is a one node list. When i add them 3 becomes the first node of the new sum list. But when trying to assigna newNodePtr's digit to one it doesnt make a new node. Just changes the node 3 's digit to 1 and it becomes a one node list 1. I think it is a problem about this tmpLast or free ing newNodePtr and then malloc it again. Dunno what to do about it.

PS: i use integer parameters because i have a pointer array headerArray[1000] pointing to the lists. Thats why i use index as parameters.

4. No one can help ???

5. Probably, though we might not be in any particular hurry to solve the problem.
People who help do have lots of other things to do than just sit on a message board all day.

Perhaps if you posted some actual error messages, and added some declarations, it would result in a more specific answer.

http://www.catb.org/~esr/faqs/smart-...ns.html#urgent

6. there is no error. It compiles but i dont get the thing i want

7. Originally Posted by bren
The fucntion i wrote is always giving errors out.
Originally Posted by bren
there is no error. It compiles but i dont get the thing i want
Either it is or it isn't.. which one is it? What are you getting instead of what you want?

8. sorry it is because of my english is not that good. I meant it is not working properly when i said The fucntion i wrote is always giving errors out.

9. Can you post some error messages then?

10. Bren,

If you want (good) help, you need to describe as best you can, as precisely as possible, what the exact problem is [and if you get the english a bit wrong, don't worry about that in itself, but you need to be precise about what the actual problem is].

For example, if you make a calculation, it's easy to say "the calculation doesn't give the right result". But if you say "I calculate with inputs 7 and 14, and the result I expect is 0.5, but I get 2.0" [which probably means you divide A / B instead of B/A in this case].

If it's an error message from the compiler, copy and paste the exact error message, and try to indicate as best you can which line corresponds to the line the compiler complains about.

It is very hard to spot general errors in code that is more than a few lines long.

--
Mats

11. hey guys i solved the problem. I know i couldnt tell it. But thx anyway. It is good to know someone there to help me. My problem was using free . I removed it and it is working nice now. .