Results 1 to 5 of 5
  1. #1
    kevinsoto is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default 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. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Do you know what an average is?

  3. #3
    kevinsoto is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    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. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    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. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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.

Similar Threads

  1. average
    By anjigadu in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 09:52 PM
  2. Need help getting average
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 09-12-2010, 11:59 PM
  3. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 05:03 AM
  4. Replies: 1
    Last Post: 03-11-2010, 04:21 AM
  5. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 11:09 AM

Tags for this Thread

Posting Permissions

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