Arrays

Finding the average of the values in an array

Page 1 of 1

3 Replies - 10553 Views - Last Post: 03 December 2007 - 09:14 AM Rate Topic: -----

#1 harmonica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • 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