Results 1 to 3 of 3
  1. #1
    nevets93 is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default Can't get correct average using array

    my dat file just says this:
    231
    523
    893
    254
    927
    368
    189

    Everything else looks good but the average is obviously wrong. currently my output file says this:
    Numbers in list:
    231
    523
    893
    254
    927
    368
    189
    Number count: 7
    average: 67
    largest numbers is: 927
    2nd largest numbers is: 893
    3rd largest numbers is: 523


    Here is my code:

    // prog04.java

    import java.io.*;

    class Numlistclass
    {
    final int MAXCOUNT = 50;
    private int count;
    private int sum;
    private int average;
    private int largest_1;
    private int largest_2;
    private int largest_3;
    private int[] nums = new int[MAXCOUNT];

    public Numlistclass()
    {
    int i;

    count = 0;
    for (i = 0; i < MAXCOUNT; i++) nums[i] = 0;
    } // Numlistclass
    public void addnum(int num)
    {
    if (count == MAXCOUNT)
    System.out.println("List is full -- unable to store: "+num);
    else
    nums[count++] = num;
    } // addnum
    public void printnums(PrintWriter pw)
    {
    int i;

    pw.println("Numbers in list:");
    for (i = 0; i < count; i++) pw.println(nums[i]);
    } // printnums
    public void printcount(PrintWriter pw)
    {
    pw.println("Number count: "+count);
    } // printcount
    public void printaverage (PrintWriter pw)
    {
    for(int i = 0; i < nums.length; i++)
    sum = sum + nums[i];

    average = sum /nums.length;

    pw.println("average: "+average);
    }
    public void topthree(PrintWriter pw)
    {
    int i;

    for (i = 0; i < count; i++)
    {
    if (nums[i]>largest_3 && nums[i]<largest_2)
    {
    largest_3=nums[i];
    }
    else if(nums[i]>largest_2 && nums[i]<largest_1)
    {
    largest_3=largest_2;
    largest_2=nums[i];
    }
    else if(nums[i] > largest_1)
    {
    largest_3=largest_2;
    largest_2=largest_1;
    largest_1=nums[i];
    }
    }//for
    pw.println("largest numbers is: "+largest_1);
    pw.println("2nd largest numbers is: "+largest_2);
    pw.println("3rd largest numbers is: "+largest_3);
    } // topthree
    } // class Numlistclass

    public class prog04
    {
    public static void main(String args[]) throws IOException
    {
    int num;
    String line;
    Numlistclass numlist = new Numlistclass();
    BufferedReader br = new BufferedReader(new FileReader("prog04.dat"));
    PrintWriter pw = new PrintWriter(new FileWriter("prog04.out"));

    // store numbers in list
    while ((line = br.readLine()) != null)
    {
    num = Integer.parseInt(line);// convert line to numeric value
    numlist.addnum(num);
    }

    // write output
    numlist.printnums(pw);
    numlist.printcount(pw);
    numlist.printaverage(pw);
    numlist.topthree(pw);


    // finalize
    br.close();
    pw.close();
    }
    } // class prog04

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default

    You have only 7 numbers in your array but you're dividing the sum by the length of the array (50).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    nevets93 is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Need help to find average
    By kevinsoto in forum New To Java
    Replies: 4
    Last Post: 11-04-2010, 02:54 PM
  2. average
    By anjigadu in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 09:52 PM
  3. Need help getting average
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 09-12-2010, 11:59 PM
  4. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 05:03 AM
  5. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 11:09 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
  •