# Greater Than the Average

Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last
• 04-01-2012, 12:40 AM
jmscarlet9
Greater Than the Average
Okay, here we go with the next question. I have NO idea how close I am to figuring this out. When I tried to change the teacher's code to test it, it got so much worse (I tried to define the array with values of 5, 10, 15, 20, 25 which would average out to 15 and thus print 20 and 25).

Here's the assignment:
The following portion of a main method computes and prints the sum of the elements of an int array list. Add comments and code to display all elements in the list that are greater than the average value of all the elements in the list.

Here's the code we were given, the code I added starts at line 32:

Code:

```import java.util.Scanner; public class Test2 {         public static void main(String[] args)   {                 // declare variables                 int[] list = new int[500];                 int sum;                 int count;                 int average;                 // populate the array list (code not shown-assume it is good)                 // initialize sum, count and average                 sum = 0;                 count = 0;                 average = 0;                 // add all values in the list to sum                 for (int j = 0; j < 500; j++)                 {                         sum = sum + list[j];                 }                 // print the sum of the values in list                 System.out.println (“The sum of the elements is “ + sum); // Add comments and code to display all elements in the // list that are greater than the average value of all the elements in the list.                                 count++; // Counts values                 if (count > 0)                 {                         double average = sum / count;                         if (int j = 0; j > average); j++)                         {                                 System.println.out(list[j])                         }                 }                 else                 {                         System.out.println("You didn't input enough data");                 }         } }```
HELP!
~Jaime
• 04-01-2012, 02:56 AM
Norm
Re: Greater Than the Average
What does the code print out when it executes?

Quote:

I tried to define the array with values of 5, 10, 15, 20, 25
where did you do that?
• 04-01-2012, 03:10 AM
jmscarlet9
Re: Greater Than the Average
I had changed the code to look like this (before I attempted to add my code to it):

Code:

```import java.util.Scanner; public class Test2 {         public static void main(String[] args)   {                 // declare variables                 int[] list = {5,10,15,20,25};                 int sum;                 int count;                 int average;                 // populate the array list (code not shown-assume it is good)                 // initialize sum, count and average                 sum = 0;                 count = 0;                 average = 0;                 // add all values in the list to sum                 for (int j = 0; j < 500; j++)                 {                         sum = sum + list[j];                 }                 // print the sum of the values in list                 System.out.println ("The sum of the elements is " + sum); } } // Add comments and code to display all elements in the // list that are greater than the average value of all the elements in the list.```
and got the following error when I ran it (it compiled without error):
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
• 04-01-2012, 03:11 AM
Norm
Re: Greater Than the Average
In your for loop use the length attribute of the array to control the number of times the loop goes around. Do NOT hardcode a numeric value like 500 or 5
• 04-01-2012, 03:12 AM
jmscarlet9
Re: Greater Than the Average
The problem is the 500 was in the code the teacher gave us.
When I tired to use a test array, did I hard code a 6 somewhere??
• 04-01-2012, 03:13 AM
Norm
Re: Greater Than the Average
No, the 6 was an example. Use the array's length
• 04-01-2012, 03:16 AM
jmscarlet9
Re: Greater Than the Average
Oh okay. :)

So, for the final code (what I posted originally), we have to assume that the user inputs the values and there's no limit to what they can put in (but by using the [500] I assume the teacher wants us to believe that the user wouldn't input 500 values).

The part of the code I need help with is starting at line 32 of my original post.

~Jaime
• 04-01-2012, 03:28 AM
Norm
Re: Greater Than the Average
Can you explain the logic of the lines of code starting at line 32?
Make a list in pseudo code of what you want the code to do to solve the problem.
• 04-01-2012, 03:32 AM
jmscarlet9
Re: Greater Than the Average
I want it to:
"count" the values of the array "list" (in this case whatever the user inputs)
Using the "sum" and the "count" find the "average" (sum / count)
Hold the "average"
Cycle through the "list" array and print any elements that are larger than "average"
• 04-01-2012, 03:34 AM
Norm
Re: Greater Than the Average
That is a high level design description of what you want the code to do.
Now can you make a list of the steps the code must do to achieve that design.
• 04-01-2012, 03:37 AM
jmscarlet9
Re: Greater Than the Average
I'm not sure how to do the "count" on an array and other than the System.out.print(list[j]) I was pretty sure the following would be how to do it:

Code:

```if (count > 0)         {             double average = sum / count;             if (int j = 0; j > average); j++)             {                 System.println.out(list[j])             }         }         else         {             System.out.println("You didn't input enough data");         }```
.....but maybe not?

I was doing okay in this class until a few weeks ago, now I'm just lost. The book they provided is HORRIBLE at explaining. :(

Please don't lose patience with me!
• 04-01-2012, 03:41 AM
Norm
Re: Greater Than the Average
With the current code, the count would be the size of the array. If you get the contents of the array from a user, then you would count the elements as they are read in from the user.

Given the number of elements in the array in the count variable, what are the steps the code must do next?
• 04-01-2012, 03:49 AM
jmscarlet9
Re: Greater Than the Average
Pull the sum from the for loop and use it to calculate the average (sum/count). Right?
• 04-01-2012, 03:52 AM
Norm
Re: Greater Than the Average
What does "pull the sum" mean?
Here is pseudo code for getting the sum:
set sum = 0
begin loop to loop for count times
add next element of array to sum
end loop

Now write the pseudo code for the rest
• 04-01-2012, 03:56 AM
jmscarlet9
Re: Greater Than the Average
set average = 0
set sum = sum (from previous loop)
begin loop to loop for count times
divide sum by count to get average
end loop

begin loop to loop through array values
compare values to average
if value > average print
if not, move to next value
end loop
• 04-01-2012, 03:59 AM
Norm
Re: Greater Than the Average
Why is the compute average in a loop? It should be computed one time when you have the two values needed to compute it.
• 04-01-2012, 04:10 AM
jmscarlet9
Re: Greater Than the Average
Ok that makes sense...should it be....

set average = 0
set sum = sum (from previous loop)
begin loop to loop for count times
end loop
divide sum by count to get average

begin loop to loop through array values
compare values to average
if value > average print
if not, move to next value
end loop
• 04-01-2012, 04:12 AM
Norm
Re: Greater Than the Average
What is the first empty loop for?
• 04-01-2012, 04:17 AM
jmscarlet9
Re: Greater Than the Average
Don't I have to cycle through to count the number of values in the array? Or can that be stored by adding something to the sum loop?
• 04-01-2012, 04:21 AM
Norm
Re: Greater Than the Average
what did this mean: set sum = sum (from previous loop)

Remove that line and make a complete list of the steps that shows what will be done. Don't refer to a previous loop, include the loop so the list shows it all
Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last