Page 1 of 3 123 LastLast
Results 1 to 20 of 45
Like Tree1Likes

Thread: Greater Than the Average

  1. #1
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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:

    Java 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default Re: Greater Than the Average

    What does the code print out when it executes?

    I tried to define the array with values of 5, 10, 15, 20, 25
    where did you do that?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Greater Than the Average

    I had changed the code to look like this (before I attempted to add my code to it):

    Java 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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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??

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default Re: Greater Than the Average

    No, the 6 was an example. Use the array's length
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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"

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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:

    Java 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!

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Greater Than the Average

    Pull the sum from the for loop and use it to calculate the average (sum/count). Right?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default Re: Greater Than the Average

    What is the first empty loop for?
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 9
    Last Post: 06-17-2012, 04:13 AM
  2. Help: 1 is greater than 3?
    By atac57 in forum New To Java
    Replies: 3
    Last Post: 01-08-2012, 04:00 AM
  3. Need help getting average
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 09-12-2010, 11:59 PM
  4. Problem while writing to file, size greater than 15MB ..?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 05:28 PM
  5. Replies: 1
    Last Post: 06-30-2008, 07:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •