# trouble with showing the average with functions and loops

• 01-29-2011, 07:14 PM
anonymous445
trouble with showing the average with functions and loops
Hello, i won't lie this is a homework. We had to write a program that would read the names of cities and then their population. For this part it works great, the sentinal works too. Its just the function that is supposed to find the average of the population. When i try to divide (in the average function) it show the last population number????

Like this:

Enter the name of a city? sdfds dsfds .
Enter the population: 2342 23432

City Population
----- ----------
sdfds 2342
dsfds 23432
The AVERAGE is 23432.0

What is weird is that if i try to show the sum, it show the sum of all the populations. It just doesn't let me divide it by anything. Based on this code, any idea on how to solve this?? Any tips would be appreciated.

Code:

``` import java.lang.*; import java.util.Scanner; public class test128 { static int counter; static Scanner keyboard = new Scanner(System.in); static float average2; static int sum; static int pop; static int x;     static int readarray(String word, int[] population, String[] arraycity)     {   for (counter=0; word.compareTo(".")!=0; counter++)         {         arraycity[counter] = word;         word=keyboard.next();         } System.out.print("Enter the population: ");       for (x=0; x< counter; x++)         {           pop=keyboard.nextInt();           population[x] = pop;         }   return counter;     } static float average(int counter, int[]population)     {   sum=0;     int i;           for (i=0; i<counter; i++)         {             sum=sum+pop;         }     average2=(sum/counter);     return average2; }     static void showarray(int counter, int [] population, String []arraycity)     {         System.out.println("City"+"    "+"Population");         System.out.println("-----"+"    "+"----------");         for (int x=0; x<counter;x++)         {             System.out.println(arraycity[x]                     +                     "    " + population[x]);         }     }     public static void main(String[] args)     {         String [] arraycity;         arraycity=new String[50];         int [] population;         population=new int[50];       System.out.print("Enter the name of a city? ");       String word=keyboard.next();     readarray(word, population, arraycity);     average(counter, population);     System.out.print("\n\n");     showarray(counter, population, arraycity);  System.out.print("The AVERAGE is "+average2);     } }```
• 01-29-2011, 09:06 PM
pbrockway2
Quote:

When i try to divide (in the average function) it show the last population number?

So, how big is counter when the average() method is called? I ask because if counter were 1 the for loop would only look at the first value and the division would be by one, leading to the behaviour you describe.

You can check with

Code:

```static float average(int counter, int[]population) {   System.out.println("At average(), counter=" + counter);   sum=0;     int i;   for (i=0; i<counter; i++)   {       sum=sum+pop;   }   average2=(sum/counter);   return average2; }```

If counter does not have the value you expect you need to go back to where you thought you gave it the correct value and figure out why that didn't happen.
• 01-29-2011, 09:18 PM
pbrockway2
Also what is pop in that average() method supposed to be? You add it together counter times and then return the result divided by counter. Ie the average() method will always return the value of pop.

Perhaps you should be adding something else to sum each time around the for loop.
• 01-29-2011, 09:33 PM
anonymous445
Quote:

Originally Posted by pbrockway2
So, how big is counter when the average() method is called? I ask because if counter were 1 the for loop would only look at the first value and the division would be by one, leading to the behaviour you describe.

You can check with

Code:

```static float average(int counter, int[]population) {   System.out.println("At average(), counter=" + counter);   sum=0;     int i;   for (i=0; i<counter; i++)   {       sum=sum+pop;   }   average2=(sum/counter);   return average2; }```

If counter does not have the value you expect you need to go back to where you thought you gave it the correct value and figure out why that didn't happen.

thx for the code but i already tried that.(tried your code and did before a writeline with the counter at the end of the main and it returns the same thing). That is the problem with my code. i know the counter does have the right value. If you enter 2 cities for example, it says the counter = 2.
• 01-29-2011, 09:41 PM
pbrockway2
Quote:

i know the counter does have the right value.
I agree. (I checked after I posted and realised that what I was suggesting would lead to the first population value being displayed not the last...)

Hence my second post.
• 01-29-2011, 09:46 PM
anonymous445
Quote:

Originally Posted by pbrockway2
Also what is pop in that average() method supposed to be? You add it together counter times and then return the result divided by counter. Ie the average() method will always return the value of pop.

Perhaps you should be adding something else to sum each time around the for loop.

pop is supposed to variable in wich i put the population number. But i may be wrong but i don't think there is anything wrong with the sum because when i do this
System.out.print("The sum is "+sum);
it shows the proper sum. ex:

run:
Enter the name of a city? dsfds dsfds .
Enter the population: 40 40

City Population
----- ----------
dsfds 40
dsfds 40
The sum is 80The sum is 80
• 01-29-2011, 09:48 PM
anonymous445
yea, it should work like. If the sum is working and the counter has the right value, if you divide the sum by the counter, it should show the average. Weird.
• 01-29-2011, 10:02 PM
pbrockway2
You got "lucky" by entering 40 twice. Here's what I see:

Code:

```Enter the name of a city? Asd Dsa . Enter the population: 40 50 sum=100 City    Population -----    ---------- Asd    40 Dsa    50 The AVERAGE is 50.```

This is using the code

Code:

```static float average(int counter, int[] population) {     sum = 0;     int i;     for (i = 0; i < counter; i++) {         sum = sum + pop;     }     System.out.println("sum=" + sum);     average2 = (sum / counter);     return average2; }```

The important thing is that pop never changes its value within the for loop. You keep adding the same value over and over again. In my case that value was 50 - ie the population of the last city to be entered.
• 01-29-2011, 10:15 PM
anonymous445
Quote:

Originally Posted by pbrockway2
You got "lucky" by entering 40 twice. Here's what I see:

Code:

```Enter the name of a city? Asd Dsa . Enter the population: 40 50 sum=100 City    Population -----    ---------- Asd    40 Dsa    50 The AVERAGE is 50.```

This is using the code

Code:

```static float average(int counter, int[] population) {     sum = 0;     int i;     for (i = 0; i < counter; i++) {         sum = sum + pop;     }     System.out.println("sum=" + sum);     average2 = (sum / counter);     return average2; }```

The important thing is that pop never changes its value within the for loop. You keep adding the same value over and over again. In my case that value was 50 - ie the population of the last city to be entered.

hmm that might be the problem. But i can't figure out another way to find the sum. Like is there a way of figuring out the sum of the numbers in my table population.
• 01-29-2011, 10:18 PM
anonymous445
Quote:

Originally Posted by anonymous445
hmm that might be the problem. But i can't figure out another way to find the sum. Like is there a way of figuring out the sum of the numbers in my table population.

ive sent the array population to the function average. I also sent the counter. Like i don't think java will let me divide the array population by the counter.
• 01-29-2011, 10:41 PM
pbrockway2
Quote:

ive sent the array population to the function average. I also sent the counter.
Yes. And it would be most straightforward if you used these values and not the static class ones to determine the average. (Actually the counter you use is the one you pass - it just happens to have the same value as the static class counter. Don't worry too much about this.)

Quote:

i don't think java will let me divide the array population by the counter

Java doesn't let you directly do arithmetic with arrays (sum or average them). But you can use a for loop to work on them one element at a time.

The use of pop is wrong in the for loop. It is (and stays being) the value of the last city to be entered. There is no reason to keep adding this to the sum. Basically you need to think of something to replace it. Something (as you're begining to think about) to do with that population array.

Code:

```static float average(int counter, int[] population) {     sum = 0;     int i;     for (i = 0; i < counter; i++) {         sum = sum + ...; // population[???]     }     average2 = (sum / counter);     return average2; }```
• 01-29-2011, 11:09 PM
anonymous445
Quote:

Originally Posted by pbrockway2
Yes. And it would be most straightforward if you used these values and not the static class ones to determine the average. (Actually the counter you use is the one you pass - it just happens to have the same value as the static class counter. Don't worry too much about this.)

Java doesn't let you directly do arithmetic with arrays (sum or average them). But you can use a for loop to work on them one element at a time.

The use of pop is wrong in the for loop. It is (and stays being) the value of the last city to be entered. There is no reason to keep adding this to the sum. Basically you need to think of something to replace it. Something (as you're begining to think about) to do with that population array.

Code:

```static float average(int counter, int[] population) {     sum = 0;     int i;     for (i = 0; i < counter; i++) {         sum = sum + ...; // population[???]     }     average2 = (sum / counter);     return average2; }```

I think i got it thanks :)

Enter the name of a city? dsfdsf sdfds .
Enter the population: 67000 45000

City Population
----- ----------
dsfdsf 67000
sdfds 45000
The average is 56000.0
• 01-29-2011, 11:30 PM
pbrockway2
Quote:

I think i got it thanks

You're welcome. One thing you could think about is removing those static variables. pop got you into trouble and that could be taken as a sign that something's wrong. In general terms it is better to pass values (like the population array) rather than rely on static values (like pop). Likewise since readarray() returns the number of entries read, is there any need for the static counter variable (rather than just having it as a local variable of the main() method and passing it as a value when other methods need it)?