# Arrays

Page 1 of 1

## 3 Replies - 11028 Views - Last Post: 03 December 2007 - 09:14 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=38641&amp;s=76adb5169685ed2d065ede405b45a865&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 harmonica

• New D.I.C Head

Reputation: 0
• Posts: 24
• Joined: 03-December 07

# Arrays

Posted 03 December 2007 - 04:53 AM

Hi, I am new to Java and I am having problems with arrays. The problem is finding the average of the values in an array. Array: int intArray[]={2,4,17,23,45,101,32};

My attempt:

```public class array_average {

public static void main(String[] args) {
int index;
int average;
int intArray[]={2,4,17,23,45,101,32};

index=0;

//==========Find Array Average=====================
while(index<=7)
{
average = intArray[index];

index++;
//===========Output Average to the User===========

System.out.println(intArray[index]);
JOptionPane.showMessageDialog(null,"The average of the array is " + average,
"Array Average",
JOptionPane.INFORMATION_MESSAGE);
}
}
}

```

I have managed to increment the index by one each time the loop goes round. Is it possible to add the index value
each time the loop goes round to get the total. Basically i'm trying to find the average of the numbers (2,4,17,23,45,101,32). Am I using the correct approach. The above code can only display each value everytime the loop goes round. Thanks.

Is This A Good Question/Topic? 0

## Replies To: Arrays

### #2 1lacca

• code.rascal

Reputation: 44
• Posts: 3,822
• Joined: 11-August 05

## Re: Arrays

Posted 03 December 2007 - 06:50 AM

There are some problems:
- it's not a logical error, but could become bad practice: instead of writing the length of the array in (index<=7) , let Java count the elements: (index< intArray.length). This can save many headaches later.
- arrays are indexed from 0, so writing <= 7 is incorect, because 7 is an invalid index in an array that has 7 elements. So as I gave it away in the previous point, your condition should look like this: (index< intArray.length)
- calculating the average is simply done like this (pseudo code):
```sum = 0;
loop{
sum = sum + next_value
}
average = sum/number_of_values

```

Finally, I would use a for loop here, but it's your call.
Was This Post Helpful? 0

### #3 harmonica

• New D.I.C Head

Reputation: 0
• Posts: 24
• Joined: 03-December 07

## Re: Arrays

Posted 03 December 2007 - 08:36 AM

Hi, thanks for the advice. I got it to work. Do you mind looking over my code to see if it's logical or can you suggest a more efficient way to code it. Thanks again for your help.

```public static void main(String[] args) {
int index;
int sum;
int average;
int intArray[]={2,4,17,23,45,101,32};

//==========Find Array Average=====================
index=0;
sum = 0;

while (index< intArray.length)
{
sum = (sum+intArray[index]);
index++;
}
average = (sum/intArray.length);

//===========Output Average to the User===========

JOptionPane.showMessageDialog(null,"The average of the array is " + average,
"Array Average",
JOptionPane.INFORMATION_MESSAGE);
}
}

```

Was This Post Helpful? 0

### #4 1lacca

• code.rascal

Reputation: 44
• Posts: 3,822
• Joined: 11-August 05

## Re: Arrays

Posted 03 December 2007 - 09:14 AM

Without running it, it seems fine. Some might argue that the form
```		sum = 0;

for(int index = 0; index< intArray.length;index ++)
{
sum = (sum+intArray[index]);
}
average = (sum/intArray.length);

```
would look more polished, but it's a matter of taste, probably any modern compiler would generate the same bytecode anyway. (However it would restrict the scope of the index variable only to the loop where it is used, and that is a good thing)
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }