a method to increment array based on input from another array.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: a method to increment array based on input from another array.

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Posts
    4

    a method to increment array based on input from another array.

    I have inputed 20 numbers in the range 0 to 100 in one array by name num. Then i have another array by name output with 10 elements all initialized to zero; I have to go through the list of intput numbers and increment one of the elements in output based on what range the number resides.

    Given any input number in the range [0, 99], i need an expression or a method that determines which elemnt in output to increment? For example, if number is 22, it should increment output[2]. Here is my code with first part working, second part is to display a histogram that shows how many numbers are in the range 0 to 9, 10 to 19 etc.,

    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 [] output = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};


    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)
    {
    largestSmallest();
    sumAverage();

    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);



    }

    }
    Being newly introduced to arrays, I am having problems not knowing how to increment the second array based on the input from first array.
    Any Help appreciated..
    Kt

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    If the numbers are in the range 0-99 then you simply need to divide it by 10 to get the right index to increment (assuming the numbers are ints). For example..

    22/10 = 2
    33/10 = 3

  3. #3
    Join Date
    Apr 2005
    Posts
    4
    Quote Originally Posted by mikeBarr81
    If the numbers are in the range 0-99 then you simply need to divide it by 10 to get the right index to increment (assuming the numbers are ints). For example..

    22/10 = 2
    33/10 = 3
    Thanx for your reply Mike.

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

    This is the method i came up with the above solution and i call this method in paint()

    public void drawHist()
    {
    for( int i = 0; i <num.length; i++)
    {
    hist[num[i] / 10] += 1;
    }

    }

    public void paint (Graphics g)
    {
    int x = 20;
    int y = 20;
    int height = 40;


    for(int i = 0; i < hist.length; i++)
    {
    drawHist();
    int width = hist[i];
    g.fillRect(x, y, width * 10, height);
    x = x + width + 10;
    }

    }

    }

    But i see a solid black rectangle that stretches when the applet window is dragged. It doesn't look like histogram. I don't know what am i doing wrong.
    Any help appreciated,
    Kt

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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