# get the average of the linked list

• October 19th, 2012, 01:53 AM
dolcce
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...
• October 19th, 2012, 04:59 AM
Purple01
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. :)
• October 21st, 2012, 07:45 PM
Junky
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.
• October 22nd, 2012, 02:58 AM
dolcce
Re: get the average of the linked list
Quote:

Originally Posted by Purple01
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...
• October 22nd, 2012, 03:01 AM
dolcce
Re: get the average of the linked list
Quote:

Originally Posted by Junky
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
• October 22nd, 2012, 03:06 PM
jps
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.
• October 22nd, 2012, 08:56 PM
dolcce
Re: get the average of the linked list
Quote:

Originally Posted by jps
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
Code :

``` 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; }```
• October 23rd, 2012, 12:54 AM
jps
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.
• October 24th, 2012, 12:36 AM
Mugambo
Have two variables
- long counter
- long sum
use them wisely while iterating through the LL.

at the end print avg=sum/counter