# Thread: Need help to find average

1. Member
Join Date
Nov 2010
Posts
2
Rep Power
0

## 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 BufferedReader stdIn=new BufferedReader(new InputStreamReader (System.in));
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);
a[x]=Integer.parseInt (stdIn.readLine());
}
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];
arr=grades(arr);
screen (arr);
}
}

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Do you know what an average is?

3. Member
Join Date
Nov 2010
Posts
2
Rep Power
0
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-

4. 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?

5. Senior Member
Join Date
Feb 2009
Posts
304
Rep Power
6
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
Java 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.

#### Posting Permissions

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