displaying a histogrm for an array of numbers

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: displaying a histogrm for an array of numbers

  1. #1
    Join Date
    Apr 2005

    displaying a histogrm for an array of numbers

    Hello folks,
    I am currently working on a problem that inputs 20 numbers into an array and the numbers being in the range of 0 to 100. I had to calculate smallest, largest, mean and average of all the numbers in this array which i was able to do without any problem. But the second half of the problem is to display a histogram to show how many numbers are in the ranges of 0 to 9, 10 to 19 etc., So i just need a little help and not expecting anyone to code for me. This is how far i have gotten with the code.
    import java.awt.*;
    import java.applet.*;

    public class ArrayTest extends Applet

    private int [] num = {5, 7, 8, 19, 25, 30, 37, 41, 43, 50, 52, 55,
    61, 63, 70, 76, 84, 91, 95, 98};

    private int max, min, sum;
    private float average;

    public void largestSmallest()

    max = -1;
    min = 101;

    for (int i = 0; i < num.length; i++)
    if (max < num[i])
    max = num[i];
    if (min > num[i])
    min = num[i];


    public void sumAverage()
    sum = 0;
    average = 0.0f;

    for (int i = 0; i < num.length; i++)
    sum = sum + num[i];
    average = (float)(sum) / num.length;


    public void paint (Graphics g)

    g.drawString("The largest number in this array is: " + max, 30, 50);
    g.drawString("The smallest number in this array is: " + min, 30, 80);
    g.drawString("The sum of the numbers in this array is: " + sum, 30, 110);
    g.drawString("The mean of the numbers in this array is: " + average, 30, 140);



    I am lost in figuring out how to do the histogram for this problem. Any help appreciated for letting me know how to approach this portion of the problem,

  2. #2
    Join Date
    May 2006

    Smile ur question

    if u wanna calculate the number of elements b/w 0-9
    10-19 etc

    then u shud check the number by the ranges u hav taken the number like
    if the number is greater than equal to 0 and less than equal to 9.
    if number is graeter than equal to 10 and less than equal to 19.
    and goes on make these loops like in C ((num>=0)||(num<=9))

    or general loop wud be : ((num>=starting range)||(num<=ending range))

    in each loop u set a counter whose initial value is 0.increment it if any respective loop condition is satisfied.
    u hav ur histogram

    now if u wana printy the numbers then store them into arrays and then print them at the end of prog.

    hopw this wud help.
    enjoy programming.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center