• Post Reply Bookmark Topic Watch Topic
  • New Topic

Average Sum  RSS feed

 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, what's up... I need help solving a problem as my homework (I got around 6hours left to complete it lol)...

Find the numbers which is closest to the average sum for a given array of N (1<=N<=50) of natural numbers. If there are two numbers who meet the requirement, return the smaller of the two.
For example for the array of: 1,2,3,4,5,6 the average sum is 3.5, so both 3 and 4 are the closest to that, but the program has to return 3, because it's smaller than 4.

The array can also contain duplicates.
First we type the number of elements in the array, then in each line we add the numbers. Name of the class: Array

**Note**: Create a data structure array and use it.

And this is what they have given to me, I just need to type the code:

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

How do you work out such an average? How do you find the nearest whole number like that?
What have you written?
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

How do you work out such an average? How do you find the nearest whole number like that?
What have you written?


Hey, thanks a lot!

That's not my code, that's the peace of code it was given by the school, now we just have to fill it in... I didn't add anything yet...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote: I didn't add anything yet...

Okay. So lets go with what Campbell suggested and write some code to work out an average.
Suppose you had this array in your code

Can you write some code to work out the average of those numbers ?
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that already written in this part:



I don't quite get this to be honest, can you please explain shortly, I might get more confortable once I understand this part...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:Isn't that already written in this part:.

It is indeed. I missed that.

Bashkim Ukshini wrote:I don't quite get this to be honest, can you please explain shortly, I might get more confortable once I understand this part...

System.in is what is known as standard input. Anything you type whilst your program is running will be added to this stream so that your program can access it. If you don't know what BufferedReader and InputStreamReader are, don't worry for the moment. Just accept that they improve the performance of reading what you type.
This read a line of text that you type - i.e. everything you type until you hit the Enter/Return key. It stores the text in a String referred to by the s variable
This converts the contents of the String into an integer variable
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, assuming you are comfortable with reading in the numbers of values you are going to use, first you need to create an array to hold those values.
You know how many values there are going to be, so can you write the code that creates that array ?
If not, you either need to check your class notes or check out this tutorial.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I use a Foor loop?
I also don't get this part: http://prntscr.com/4xq4gu
Why <E> ??
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:Should I use a Foor loop?

Not yet. For the moment I just want to see the line of code that declares the array variable and assigns an array of the correct size to it.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I've come up with this, I hope it's ok:



 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good. You don't need to create a new BufferedReader on line 14 - you can still use the one you created on line 3.
The next thing to do is to calculate the average of all those numbers you read in. Don't forget to think about the type of the variables you use.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay then, I removed it, now that part looks like this:


Now I have the array, I need to call the function inside main:


and the function body is outside of many, and this is it:



I just need to add the part that calculates the closest member to the "average"... Any ideas?
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:



you should change the line on how you compute the average.

Remember, average = (sum of all elements) / (total number of elements)
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aj Prieto wrote:
Bashkim Ukshini wrote:



You should change the line on how you compute the average.

Remember, average = (sum of all elements) / (total number of elements)


Ohh, right... it should be average=sum/x.length;
thank you.

how about finding the closest to the average? I found out how that is done, but what if I get two numbers with the equal distance to the average.. how to save them both? I need an idea.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:what if I get two numbers with the equal distance to the average.. how to save them both? I need an idea.

You go back and read the specification
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe I did it.. maybe it's complicated, but I hope it's correct.

This is the function that finds the closest number to the average:




What do you think guys?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:What do you think guys?

The last bit seems a bit complicated. In the second loop you're checking the whole of the array for an equal number, but you've already checked all the numbers up to the current one in the first loop. In fact you don't even need the second loop.
What about this
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the 2nd line should be -average, not -sum..

Anyways, your way is way way better than mine... I optimized it a bit more, and this is my final solution:




Thank you so so so much guys!
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:Isn't that already written in this part:
. . .
Yes, and no.

What you could have done is separate calculating averages (more precisely arithmetical means) from the process of creating the array. So I think Joanne was correct to supply you with a ready made array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!