need qucik help with writing a function

This is a discussion on need qucik help with writing a function within the C Programming forums, part of the General Programming Boards category; Hi everyone i need help. There are list which are consisting of digits of a number in revers order. For ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Turkey
    Posts
    6

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Turkey
    Posts
    6
    well this is the code i wrote.


    Code:
    void addition(int hdr1,int hdr2){
    	
    	headerArray[hdr2 +1] = (digitNodePtr)malloc(sizeof(digitNode));
    	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));
    	headerArray[hdr2 + 1] = newNodePtr;
    	tmpLast = headerArray[hdr2 + 1];
    	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. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Turkey
    Posts
    6
    No one can help ???

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Location
    Turkey
    Posts
    6
    there is no error. It compiles but i dont get the thing i want

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by bren
    The fucntion i wrote is always giving errors out.
    Quote 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?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Nov 2007
    Location
    Turkey
    Posts
    6
    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. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Can you post some error messages then?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Location
    Turkey
    Posts
    6
    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. .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21