Results 1 to 6 of 6
  1. #1
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Calculating Average using Arrays....help!

    I've been stuck on this for many hours now. I'm trying to allow the user to input 10 numbers. Then the program is to add up the numbers and output an average. I'm baffled by what I should be doing for the average part. Below is my code, followed by my output. I actually only have to write out the logic for this code in pseudocode, but I find it easier to grasp what is needed by actually coding it in java first.

    package handson7;

    /**
    *
    * @author Matt
    */
    import java.util.Arrays;
    import java.util.Scanner;
    public class HandsOn7 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    int index = 0;
    int number;

    int[] anArray;
    int element;

    anArray = new int[10];

    number = 0;
    element = 1;

    while(number < 10){
    System.out.println("Enter a number for " + element);
    Scanner elementNumber = new Scanner(System.in);
    anArray[index] = elementNumber.nextInt();
    index = index + 1;
    element = element + 1;
    number = number + 1;
    }
    System.out.println("Here are the numbers you entered: ");
    System.out.println(Arrays.toString(anArray));


    int sum = 0;
    for(int r = 0; r <anArray.length ; r++){
    sum = sum + anArray[r];
    int average = sum / anArray[r];
    System.out.println("Here is the average " + average);
    }


    }



    }


    ****Now the output that I have from this code*********

    run:
    Enter a number for 1
    1
    Enter a number for 2
    1
    Enter a number for 3
    1
    Enter a number for 4
    1
    Enter a number for 5
    1
    Enter a number for 6
    1
    Enter a number for 7
    1
    Enter a number for 8
    1
    Enter a number for 9
    1
    Enter a number for 10
    1
    Here are the numbers you entered:
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    Here is the average 1
    Here is the average 2
    Here is the average 3
    Here is the average 4
    Here is the average 5
    Here is the average 6
    Here is the average 7
    Here is the average 8
    Here is the average 9
    Here is the average 10
    BUILD SUCCESSFUL (total time: 11 seconds)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Calculating Average using Arrays....help!

    That's not how you calculate averages -- the denominator is the number of items. And you'll want to do your calculation after you've calculated the sum, not while you're calculating it.

  3. #3
    Herah is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Calculating Average using Arrays....help!

    int numElements = 0;
    sum = 0;
    //while loop code to enter numbers{
    sum += number
    numElements++
    }

    double average = sum / numElements
    println(average)

  4. #4
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Calculating Average using Arrays....help!

    Thanks for all the help. This did the trick.

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Calculating Average using Arrays....help!

    Quote Originally Posted by Herah View Post
    int numElements = 0;
    sum = 0;
    //while loop code to enter numbers{
    sum += number
    numElements++
    }

    double average = sum / numElements
    println(average)
    Wow, now I can guarantee you that the OP has no idea what your code does and why his doesn't work. Thank you for spoonfeeding someone so now they will have difficulty throughout their entire Java endeavor.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Calculating Average using Arrays....help!

    Actually, I do understand this and was able to come up with someone similar in a for loop.

Similar Threads

  1. Inputting arrays to find the average
    By DuncanHollows in forum New To Java
    Replies: 19
    Last Post: 07-28-2011, 07:55 PM
  2. Displaying and calculating 2D arrays
    By JSTBO in forum Java 2D
    Replies: 5
    Last Post: 03-07-2011, 02:07 AM
  3. finding average in 2d arrays
    By the beginner in forum New To Java
    Replies: 18
    Last Post: 02-24-2011, 05:32 AM
  4. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 06:03 AM
  5. Replies: 1
    Last Post: 03-11-2010, 05:21 AM

Posting Permissions

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