Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining an arrays value depending on another array

Tags:

java

I'm currently working on one of my assignments, and am looking for some help with the logic for one of my functions.

First off I have a array of numbers to be categorized, then a number interval, this number determines in which position each of the numbers being plotted goes into array2.

ie.

int interval = 2;

for(int i = 0; i < array1.length; i++) {
    if((array1[i] > 0) && (array1[i] < interval)) {
        array2[0]++;
    }
}

However, the number from array1 is 3. I would then need another if statement like so:

...
}else if((array1[i] > 2) && (array1[i] < interval * 2)) {
    array2[1]++;
}else if((array1[i] > 

As you can start to see the problem with this is that I would need to continue for an infinite range of numbers. So my question is what is an easier way of achieving this goal? Or is there already a library which I can utilize to do so?

I'm sorry if I didn't make this clear enough, also I would prefer if code wasn't given to me. I would appreciate if someone would be able to tell me a more effective way about going about this, thanks in advance!

EDIT:

Assuming that the interval is set to 2, and the numbers from array1 are between 0 and 10, I would need to create a code that would do such:

2 < numFromArray1 > 0 == array2[0]++
4 < numFromArray1 > 2 == array2[1]++
6 < numFromArray1 > 4 == array2[2]++
8 < numFromArray1 > 6 == array2[3]++
10 < numFromArray1 > 8 == array2[4]++

However, the numbers from array1 can be positive or negative, whole or decimal.

like image 682
Matthew Brzezinski Avatar asked May 03 '26 16:05

Matthew Brzezinski


2 Answers

Use a nested loop. Obviously it's not infinitely many possibilities for interval because array2 has a fixed size. So if you loop through all the cells in array2, and then do some math to figure out what your conditions need to be... I won't give complete code (you asked me not to, but it would look something like:

for ( ... ) {
    for ( ... ) { 
        if (array1[i] > /* do some math here */ && ... ) {
            array2[/* figure out what this should be too */]++;
        }
    }
 }

Hopefully you can figure it out from this.

By the way, if you aren't required to use an array for array2, consider learning about LinkedList<?>for a data structure that can grow in size as you need it to.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/LinkedList.html

http://www.dreamincode.net/forums/topic/143089-linked-list-tutorial/

like image 146
durron597 Avatar answered May 06 '26 04:05

durron597


Assuming I understood the question correct, and the interval would be 3, than occurrences of 0, 1 and 2 would increase array2[0], occurences of 3, 4 and 5 would increase array2[1] and so on, this would be a solution:

EDIT sorry, you did not want to see code. I can repost it, if you want. Think about a real easy way to determine which category a number will be in. I'll try to give a hint.

Interval = 3;
0,1,2 -> category 0
3,4,5 -> category 1
6,7,8 -> category 2

Once you know the category, it is easy to increment the desired number in array2.

It would look something like that:

for(int i = 0; i < array1.length; i++) {
    int category = // determine category here
    // increase correct position of array2
}

After some dicussion, here is my code:

for(int i = 0; i < array1.length; i++) {
    int category = array1[i] / interval;
    array2[category]++;
}

My solution won't work for negative numbers. Also it is not specified how to handle them

like image 29
jlordo Avatar answered May 06 '26 05:05

jlordo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!