Determine sum of every other value in list.

This is a discussion on Determine sum of every other value in list. within the C Programming forums, part of the General Programming Boards category; Honestly I don't know anything about lists or pointers so I'm beyond lost on this. "Write a function that takes ...

  1. #1
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19

    Determine sum of every other value in list.

    Honestly I don't know anything about lists or pointers so I'm beyond lost on this.

    "Write a function that takes a pointer to the front node of a linked list and determines the sum of every other value in the list starting with the first. (Note: If there are no items in the list, return 0.)

    Code:
    int addEveryOther(struct ll* list)
    "

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    So you got an assignment to do something that your instructor never taught anything about?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19
    Basically. It's a review problem for our final.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    In pseudo-code :

    Code:
    create variable sum, set it to 0
    create variable count, set it to 0
    
    while list pointer is not NULL
        if (count is even) add list pointer's data to sum
        if (next element in list is not NULL)
            move list pointer to next element
            increment count
    
    return sum
    Looks like an opportunity for an obfuscated code contest to me. Anyone able to code this in one line?
    Last edited by KCfromNC; 12-08-2010 at 02:02 PM. Reason: Fix code tag

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,747
    Steps
    1. Write a program to get the sum of every item in the linked list
    2. Change it to skip every other item

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 01:05 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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