Results 1 to 5 of 5
Thread: Need help to find average
- 11-04-2010, 03:48 AM #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);
}
}
- 11-04-2010, 05:11 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Do you know what an average is?
- 11-04-2010, 01:47 PM #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-
- 11-04-2010, 01:50 PM #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?
- 11-04-2010, 01:54 PM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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
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.Java Code:public static double average(int[] elements)
Similar Threads
-
average
By anjigadu in forum New To JavaReplies: 4Last Post: 09-19-2010, 09:52 PM -
Need help getting average
By soccer_kid_6 in forum New To JavaReplies: 15Last Post: 09-12-2010, 11:59 PM -
Calculating average
By clocksaysits9 in forum New To JavaReplies: 4Last Post: 04-06-2010, 05:03 AM -
Need help in calculating average value on this format ....
By motress in forum New To JavaReplies: 1Last Post: 03-11-2010, 04:21 AM -
Calculate Average
By sthack99 in forum New To JavaReplies: 4Last Post: 06-13-2008, 11:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks