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



Jasbir21Asked:
Who is Participating?
 
CEHJCommented:
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);
0
 
CEHJCommented:
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>
0
 
CEHJCommented:
How you implement it depends somewhat on the type of the key though. What is it?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
objectsCommented:
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 :)
0
 
hoomanvCommented:
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
0
 
hoomanvCommented:
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.
0
 
CEHJCommented:
>>This seems like a homework and it most likely is.

I didn't think so, as jasbir has been around for some time AFAICR
0
 
VenabiliCommented:
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 :)
0
 
Jasbir21Author 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);
}
}
}
0
 
Jasbir21Author Commented:
0
 
Jasbir21Author 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>();
maps.add(map);

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..

 
0
 
objectsCommented:
what did u want  to do with the sorted map?
0
 
CEHJCommented:
>>How to use sorted map  here ..Pls help..

You would need:

Map<Double,Integer> map = new TreeMap<Double,Integer>();
0
 
Jasbir21Author Commented:
just saw this...
0
 
Jasbir21Author 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...
0
 
Jasbir21Author 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>();

maps.add(map);


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

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

java.math.BigDecimal
0
 
Jasbir21Author 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
0
 
Jasbir21Author 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






0
 
CEHJCommented:
>>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
0
 
CEHJCommented:
btw jasbir - *is* this homework?
0
 
Jasbir21Author Commented:
Thanks a lot...I learnt a lot.. I understand what is being done now.. you get the values and then your floor them.. :-)
0
 
CEHJCommented:
:-)
0
 
Jasbir21Author 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..
0
 
Jasbir21Author Commented:
i am new to use maps, treemaps, jfreegraphs  ..

i am very sorry for troubling a lot..but thanks for helping a lot..
0
 
CEHJCommented:
No - i didn't think it was. Hope it wasn't made too much harder by the removal of my code
0
 
Jasbir21Author 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..
0
 
Jasbir21Author Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.