Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 45
Like Tree1Likes

Thread: Greater Than the Average

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

    Default Re: Greater Than the Average

    so....


    set sum = 0
    begin loop to loop for count times
    add next element of array to sum
    end loop

    set average = 0
    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

    (I removed the other loop in the 2nd set)

  2. #22
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,783
    Rep Power
    25

    Default Re: Greater Than the Average

    That looks reasonable, now write the code that does the steps you have listed.

    One note: both loops should loop the same number of times. Your wording is different for the two begin loop steps.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Greater Than the Average

    And this is where I'm lost. I don't understand how to write the code for arrays.

  4. #24
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,783
    Rep Power
    25

    Default Re: Greater Than the Average

    You already have done that:
    int[] list = {5,10,15,20,25};

    What step in the list of steps are you having trouble with?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Greater Than the Average

    I can't use that. I was just trying to put that in there so I could test it.

    I don't know how to cycle through the array to get the count and then use that for the average.

    The code I have to add to is this:

    Java Code:
    // declare variables
    int[] list = new int[500];
    int sum;
    
    // populate the array list (code not shown—assume it is good)
    
    // initialize sum
    sum = 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);
    
    //   ----   your code goes here
    So there is no average or count initialized and I wasn't sure if I could put that after the println or if I should put it where she initialized "sum". Plus, she has the loop for the sum already written, how do I add in the "count" so i can get the average (hence why I was thinking it would have to go in it's own loop).

    :(

  6. #26
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,783
    Rep Power
    25

    Default Re: Greater Than the Average

    There are many parts to programs and each part should be built and tested separately. If you try to test all the parts at one time it will be a mess. Leave off the part about getting the input from the user. That was not being discussed.
    Put in the array as shown in post#24, set count to the length of the array and get the code written that you have listed steps for. Compile it, execute it and check that it is working properly. When that all works, THEN go back and work on how to get the input from the user. That is a separate problem to be done later. Do one thing at a time.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Greater Than the Average

    Ok. I need to step away for tonight (been doing this for 8hrs). I will try in the morning what you said above. Thank you SO much for your help and patience.

    Jaime

  8. #28
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,783
    Rep Power
    25

    Default Re: Greater Than the Average

    Ok see you later.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Greater Than the Average

    Good morning,
    Fresh start but new errors. Ugh. :(

    I shortened my code to contain only the part I need to get to work. I'm getting a host of errors on my "if" statement I've never seen before and have NO idea how to fix.

    Here's the code:
    Java Code:
    import java.util.Scanner;
    
    // 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.
    
    public class Test2a
    {
    	public static void main(String[] args)
       {
    		// declare variables
    		int[] list = new int[500];
    		int[] list = {5,10,15,20,25};
    		int average;
    
    		// initialize average
    		average = 0;
    
    		// find average
    		double average = sum / count;
    		
    		// loop through array and print any elements > average
    		if (list.length > 0)
    		{
    			if (int j = 0; j > average); j++)
    			{
    				System.println.out(list[j]);
    			}
    		}
    		else
    		{
    			System.out.println("You didn't input enough data");
    		}
    	}
    }

    Here are the errors: (*note I'm using "int j" so that it matches with the code already created)
    error: '.class' expected
    error: illegal start of expression
    error: ';' expected
    error: not a statement
    error: ';' expected
    error: ';' expected

    I know it's probably something ridiculously simple, but please help!
    ~Jaime

  10. #30
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  11. #31
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Greater Than the Average

    Normally the compiler shows the line(s) where it discovered the error(s); why are you hiding those from us?

    kind regards,

    Jos

    ps. oh, and you're trying to declare a local variable 'list' twice: once as an array with 500 elements and next as an array with just 5 elements, all initialized to some value.
    cenosillicaphobia: the fear for an empty beer glass

  12. #32
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,783
    Rep Power
    25

    Default Re: Greater Than the Average

    You seem to have ignored the steps you listed in post #21

    1)set sum = 0
    2)begin loop to loop for count times
    3)add next element of array to sum
    4)end loop

    5)set average = 0
    6)divide sum by count to get average

    7)begin loop to loop through array values
    8)compare values to average
    9)if value > average print
    10)if not, move to next value
    11)end loop

    The purpose for making this list was to create a guide for writing the code. There should be line(s) of code for each item in the list.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Greater Than the Average

    Only because it doesn't copy & paste nicely. Here is the full list of errors:

    ----jGRASP exec: javac -g Test2a.java

    Test2a.java:23: error: '.class' expected
    if (int j = 0; j > average); j++)
    ^
    Test2a.java:23: error: illegal start of expression
    if (int j = 0; j > average); j++)
    ^
    Test2a.java:23: error: ';' expected
    if (int j = 0; j > average); j++)
    ^
    Test2a.java:23: error: not a statement
    if (int j = 0; j > average); j++)
    ^
    Test2a.java:23: error: ';' expected
    if (int j = 0; j > average); j++)
    ^
    Test2a.java:23: error: ';' expected
    if (int j = 0; j > average); j++)
    ^
    6 errors

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

    Default Re: Greater Than the Average

    UPDATED CODE with new errors :)

    Java Code:
    import java.util.Scanner;
    
    public class Test2
    {
    	public static void main(String[] args)
       {
    		// declare variables
    		int sum;
    
    		// populate the array list
    		int[] list = {5,10,15,20,25};
    
    		// initialize sum
    		sum = 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.
    		
    		if (list.length > 0)
    		{
    			for (int j = 0; j > average; j++);
    			{
    				System.println.out(list[j]);
    			}
    		}
    		else
    		{
    			System.out.println("You didn't input enough data");
    		}
    	}
    }
    ERRORS:

    Test2.java:30: error: cannot find symbol
    for (int j = 0; j > average; j++);
    ^
    symbol: variable average
    location: class Test2
    Test2.java:32: error: cannot find symbol
    System.println.out(list[j]);
    ^
    symbol: variable j
    location: class Test2
    Test2.java:32: error: cannot find symbol
    System.println.out(list[j]);
    ^
    symbol: variable println
    location: class System
    3 errors
    Last edited by jmscarlet9; 04-01-2012 at 05:39 PM. Reason: wrong code...more errors...

  15. #35
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,783
    Rep Power
    25

    Default Re: Greater Than the Average

    The ; at the end of the for statement ends the scope for the definition for the variable j.
    Remove the ; to put the block of code inside of the {}s in the same scope as the for loop.

    Did you miss my post #32 ??

    Your code is not following the pseudo code.
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Greater Than the Average

    I did miss #32. Let me try using that instead. Sorry. :(

  17. #37
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,783
    Rep Power
    25

    Default Re: Greater Than the Average

    Was everything we did yesterday lost?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Greater Than the Average

    No, I think I got it, but I'm getting an error on the j in my 2nd loop....

    I followed the pseudocode....please tell me if I didn't....
    The only change I know I made is I used list.length instead of count. I think that will do the same thing from what I was reading(?).

    Java Code:
    import java.util.Scanner;
    
    public class Test2
    {
    	public static void main(String[] args)
       {
    		// declare variables
    		int sum;
    		int average;
    
    		// populate the array list
    		int[] list = {5,10,15,20,25};
    
    		// initialize sum
    		sum = 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.
    		
    		average = 0;
    		average = sum / list.length;
    		
    		if (list[j] > average)
    		{
    			System.out.println(list[j]);
    		}
    	}
    }
    ERRORS:
    Test2.java:32: error: cannot find symbol
    if (list[j] > average)
    ^
    symbol: variable j
    location: class Test2
    Test2.java:34: error: cannot find symbol
    System.out.println(list[j]);
    ^
    symbol: variable j
    location: class Test2
    2 errors

  19. #39
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,783
    Rep Power
    25

    Default Re: Greater Than the Average

    Your code uses 500 in a for loop and not the length on line 18.
    Line 29 seems a waste of time because of line 30

    Where is the loop for step 7)?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Greater Than the Average

    It's set to 500 because the final code will use
    Java Code:
    int[] list = new int[500];
    instead of my declared array. I didn't want to change the code provided for the sum. Will list.length not work if the sum loop uses 500?

    As for the loop for step seven, I wrote

    Java Code:
    if (list[j] > average)
    		{
    			System.out.println(list[j]);
    		}
    Is this not correct?

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

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