# Range Value

Hi,
Hi,

I have array d[i][j] that stores values. By using map, i am able to get the number of times the values occur.
For example:
Values    How many times occur
3.6667 =2
3.52   =3
3.51 =1

Is there a way if I want to find the occurances in range. For example,

how many times i get the value between and including 3.5 and 3.6 . Is there any other function or method to use...

For example, for the example above,
3.5 &&3.6 is 4 times

###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You need to coalesce your keys using another map. Start with them in a SortedMap (<Double,Integer>) and then coalese them into SortedMap<BigDecimal, Integer> or  SortedMap<String, Integer>
Commented:
How you implement it depends somewhat on the type of the key though. What is it?
Java DeveloperCommented:
use a loop to go thru your values checking whether each is within the range
if it is the incremnent a counter for the number in the range

you can improve on this if you already have a map with the number of times each appears
in this case you can go thru the keys in your map (the values) and if in the range you inclement your counter by the number of times that value appeared.

let me know if u have any questions :)
Commented:
This can be done by sorting all the values, preprocessing and then doing binary search
on the both end values of the interval and then you have the number of values that fall in between
Commented:
another way is to use binary-search-tree
do a simple recursive traversal of the tree to find the starting value (call it S)
from the right child of S do an inroder traversal to obtain the data in ascending order
count the nodes you visit, until you reach the end of interval
this will eliminate the sort overhead (in a 1 dimensional array) but would also cause another overhead (traversal).

my first comment is good where you insert values only once but get range occurances many times
it costs only two binarysearch that is O(Log n)
then you only need to subtract the start index from the end index.
Commented:
>>This seems like a homework and it most likely is.

I didn't think so, as jasbir has been around for some time AFAICR
Commented:
I saw this but he also never made even an attempt to do anything here. :) Give him an idea so he can start implementing and then help on this :)
Author Commented:
This program takes the number of years and iterations to be runned. It then calculates the scores and stores them in the 2 dimensional score array based on the number of iterations and years.

For example,if the program is runned five times(iterations=3 and years=2), this could be the output.

score[year][iteration]

Iterations 1   Iteratons 2   Iterations 3
Years   1  2.666667       2.66667         2.66667
Years  2   3.2224           3.2221          3.4

so what i need to do is to find the percentage for the range based on years
For example, for year 1 , how many times got value between 2.6 to 2.7

For example, for year 2,how many times, got value between 3.2 to 3.3, 3.3 to 3.4

I just realized that the map below takes all the value in and does not consider it based on years..Is there a way to make the map think in years.

I am posting a new question for map first coz without getting the map correct..i cant sorrt..PLEASE look AT THIS..THANKS A LOT.

Map <Double,Integer> map=new HashMap<Double,Integer>();

for(int i=0;years+1;i++)
{
for(int j=0;j<iterations;j++){
Integer val=map.get(score[i][j]);
if(val==null)
map.put(score[i][j],1);

else
{
int newVal=val+1;
map.put(score[i][j],newVal);
}
}
}
Author Commented:
Author Commented:
Thanks for helping on the maps question...

Now my map is like this:

ArrayList<Map<Double,Integer>> maps=new ArrayList<Map<Double,Integer>>(years+1);

for(int i=0;i<years+1;i++)
{
Map<Double,Integer> map = new HashMap<Double,Integer>();

for(int j=0;j<iterations;j++)
{
Integer val = map.get(score[i][j]);
if(val==null)
map.put(score[i][j],1);

else
{
int newVal=val+1;
map.put(score[i][j],newVal);
}
}
}

How to use sorted map  here ..Pls help..

Java DeveloperCommented:
what did u want  to do with the sorted map?
Commented:
>>How to use sorted map  here ..Pls help..

You would need:

Map<Double,Integer> map = new TreeMap<Double,Integer>();
Commented:
But to normalise your keys to get the ranges, you should use

Map<BigDecimal, Integer> map = new TreeMap<BigDecimal, Integer>();
map.put(BigDecimal.valueOf(score[i][j]).setScale(1, RoundingMode.FLOOR), newVal);

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
just saw this...
Author Commented:
>>>what did u want  to do with the sorted map?

actually, now i have my map's scores based on years..
meaning map 1 would refer to scores for year 1,

map 2 would refer to scores for year 2..

however, for the scores in each map, i need find range

for example: between 3.2 and 3.3 ,how many times...
Author Commented:
ArrayList<Map<BigDecimal,Integer>> maps =new ArrayList<BigDecimal,Integer>>(years+1);

for (int i=0;i<years+1;i++)
{

Map<BigDecimal,Integer>map =new TreeMap<BigDecimal,Integer>();

for(int j=0;j<iterations;j++)
{
Integer val = map.get(score[i][j]);

int newVal=val+1;
map.put(BigDecimal.valueOf(score[i][j]).setScale(1, RoundingMode.FLOOR), newVal);

}
}

I get error: cannot find symbol Floor

Author Commented:
sorry..its i got errror : cannot find symbol BigDecimal, i hope it is ok if the arraylist of map is also BigDecimal
Commented:
You need to import

java.math.BigDecimal
Author Commented:
when i compile , i dont get any errors but when i run, i get exceptions

exception in thread "AWT-eventqueque-1" java lang.ClassCastException :java.math.BigDecimal..

pls help...thanks
Author Commented:
sorry..i increase the points...

i hope , my explaination is ok...

actually, i am finding range values which is needs to be based on years which is kept in the arraylist

maps 1 keeps the scores in map for year 1
maps 2 keeps the scores in map for year 2

my problem is that for each maps, i need to find the range..For example,
maps 1 for year 1
the scores stored in map would be { 2.66667=2 , 2.6661=1, 3.5=1}

so, now i find range of sorted map like this

2.6=3 (something like if value between 2.6 and 2.7, you add)
3.5=1

Data

score[year][iteration]

Iterations 1   Iteratons 2   Iterations 3 Iteration 4
Years   1  2.666667       2.6661         2.66667       3.5
Years  2   3.2224           3.2221          3.4

Commented:
>>exception in thread "AWT-eventqueque-1" java lang.ClassCastException :java.math.BigDecimal..

Are you putting in instances of BigDecimal? If you aren't and treating the Map as if it does have them, then you're going to get that kind of exception, although since you're using >= 1.5, you should ensure that you genericize the Map so this error is obvious at *compile* time

>>actually, i am finding range values which is needs to be based on years which is kept in the arraylist

Yes. What i'm suggesting needs to be done in a loop
Commented:
btw jasbir - *is* this homework?
Author Commented:
Thanks a lot...I learnt a lot.. I understand what is being done now.. you get the values and then your floor them.. :-)
Commented:
:-)
Author Commented:
hi,

nope this is not homework..actually, this was done earlier in array and was being used ..then, found out that it cant be done in array..it is the same graph project i am working on..
Author Commented:
i am new to use maps, treemaps, jfreegraphs  ..

i am very sorry for troubling a lot..but thanks for helping a lot..
Commented:
No - i didn't think it was. Hope it wasn't made too much harder by the removal of my code
Author Commented:
thanks again for everything..i have more question on maps ..i am going to post now..could you pls look at it as well..
Author Commented:
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.