# Thread: Greater Than the Average

1. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

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

3. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Greater Than the Average

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

4. ## Re: Greater Than the Average

int[] list = {5,10,15,20,25};

What step in the list of steps are you having trouble with?

5. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

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

7. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## 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. ## Re: Greater Than the Average

Ok see you later.

9. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## 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;

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

~Jaime

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

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

12. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

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

13. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## 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);

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

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

15. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Greater Than the Average

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

16. ## Re: Greater Than the Average

Was everything we did yesterday lost?

17. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## 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);

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

18. ## 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)?

19. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## 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 First 123 Last

#### Posting Permissions

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