# Need help to find average

• 11-04-2010, 04:48 AM
kevinsoto
Need help to find average
hi, I need to make a program in which I enter 8 grades and it gets the average of them. I already did everything and I'm only missing the average part, I have no idea what to do next.
thanks.

This is what I've got.

import java.io.*;
public class Hw {

private static PrintWriter stdOut=new PrintWriter (System.out,true);

public static int [] grades(int a [])throws IOException
{
for(int x=1;x<a.length;x++)
{ System.out.println("Enter the first grade "+x);
}
return a;
}

public static void screen (int a[]){
for (int x=1;x<a.length;x++)
System.out.println("Grade of student "+x+" "+"= "+ a[x]);
}

public static void main(String[] args)throws IOException{
int y=9;

int arr[];
arr= new int [y];
screen (arr);
}
}
• 11-04-2010, 06:11 AM
pbrockway2
Do you know what an average is?
• 11-04-2010, 02:47 PM
kevinsoto
hummm, yes, of course I know..
the problem is that I don't know how to take the average with arrays.
Idk how to sum all the numbers I enter...

A-
• 11-04-2010, 02:50 PM
KevinWorkman
How would you do this "by hand"? If somebody gave you a stack of cards, each with a number on it, and asked you to figure out their average- what would you do?
• 11-04-2010, 02:54 PM
StormyWaters
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

Basically to obtain the values out of an array you use indexes, just like you did when you are displaying them to the screen. I would suggest making another method
Code:

`    public static double average(int[] elements)`
This method takes in the int[] are averages out the elements. Just like you did when you were displaying the elements to the screen, you would loop through the array and keep track of the sum of all the elements. After the loop just return the sum divided by the number of elements.