Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: get the average of the linked list

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default get the average of the linked list

    Hi how can I get the average of the linked list? I mean take the whole list and divide by two...

    eg: average = 13213216546847651321 / 2

    any help I'm confuse on how to start...


  2. #2
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: get the average of the linked list

    You counld start on how to input the number into your program.

    Try googling the Scanner function.
    That one might help.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: get the average of the linked list

    You calculate the average the same way as if you had a bunch of numbers written on paper. Add hte numbers and divide the sum by how many numbers there are. If all the values are store in a List or some other Collection then check out what methods the Collection has to allow you to access the numbers. And of course you will need a loop and a variable to hold the sum.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: get the average of the linked list

    Quote Originally Posted by Purple01 View Post
    You counld start on how to input the number into your program.

    Try googling the Scanner function.
    That one might help.
    Thanks btw, but I don't know if you know what I'm asking...

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: get the average of the linked list

    Quote Originally Posted by Junky View Post
    You calculate the average the same way as if you had a bunch of numbers written on paper. Add hte numbers and divide the sum by how many numbers there are. If all the values are store in a List or some other Collection then check out what methods the Collection has to allow you to access the numbers. And of course you will need a loop and a variable to hold the sum.
    No, what I mean is if I have 1000 in a linked list that is 1>0>0>0 so it its 1000 / 2 not 1+0+0+0 / 2

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: get the average of the linked list

    If you are asking how to get the average of the elements of a linked list, the answer is to add every element to a total and divide by the number of elements.

    Dividing by 2 only gives the average when there are 2 elements.
    Unless you have a special list (1,2,3,4,5,6,7,8,9,) for example, where the center element is the average, in which case the number of elements / 2 gives the index of the element at the center of the list, and supposedly contains the average.

    If you still have a question, try to give details on what you are trying to do and reword the question to be clear about what part you are having problems with.

  7. The Following User Says Thank You to jps For This Useful Post:

    dolcce (October 22nd, 2012)

  8. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: get the average of the linked list

    Quote Originally Posted by jps View Post
    If you are asking how to get the average of the elements of a linked list, the answer is to add every element to a total and divide by the number of elements.

    Dividing by 2 only gives the average when there are 2 elements.
    Unless you have a special list (1,2,3,4,5,6,7,8,9,) for example, where the center element is the average, in which case the number of elements / 2 gives the index of the element at the center of the list, and supposedly contains the average.

    If you still have a question, try to give details on what you are trying to do and reword the question to be clear about what part you are having problems with.
    actually my program is getting the sum of two (2) big numbers using linked list and not using BigInteger or BigDecimal dataTypes and I figure it out how to that.. My problem now is getting the average of the sum that is why I'am dividing that sum into two (2) only to get the average...

    so far I got this but it returns null so sad.. tsk3
    	Node getAverage(Node res) {
    		Node pointer = res;
    		int carry = 0;
    		while (pointer != null) {
    			if (carry == 0) {
    				carry = pointer.data % 2;
    				pointer.data = pointer.data / 2;
    			} else {
    				carry = (pointer.data + 10) % 2;
    				pointer.data = (pointer.data + 10) / 10;
    			}
    			pointer = pointer.next;
    		}
    		return pointer;
    	}

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: get the average of the linked list

    Lets drop the code back to pseudo-code and analyze the algorithm.

    while ( pointer is NOT null )

    // do some things
    // else do some things
    // ok that is enough doing

    Now that pointer is null, exit the loop.

    return pointer


    Why does the method return null? Because you told it to exit the loop when pointer is null and then return pointer.

  10. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Have two variables
    - long counter
    - long sum
    use them wisely while iterating through the LL.

    at the end print avg=sum/counter

Similar Threads

  1. linked list
    By javasohard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 02:22 AM
  2. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  3. Linked List Help
    By BuhRock in forum Collections and Generics
    Replies: 3
    Last Post: September 26th, 2011, 08:43 AM
  4. linked list help
    By tjoney in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 3rd, 2011, 06:54 PM
  5. [SOLVED] Linked List Help
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2011, 10:32 PM