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

# Thread: get the average of the linked list

1. ## 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. ## 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. ## 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.

4. ## Re: get the average of the linked list

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...

5. ## Re: get the average of the linked list

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

6. ## 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. ## Re: get the average of the linked list

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
```	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. ## 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. Have two variables
- long counter
- long sum
use them wisely while iterating through the LL.

at the end print avg=sum/counter