[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Range Value

Posted on 2006-04-06
30
Medium Priority
?
277 Views
Last Modified: 2010-03-31
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



0
Comment
Question by:Jasbir21
  • 14
  • 10
  • 2
  • +2
29 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16398322
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16398456
How you implement it depends somewhat on the type of the key though. What is it?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 16398721
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:hoomanv
ID: 16398728
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
 
LVL 14

Expert Comment

by:hoomanv
ID: 16398843
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16399624
>>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
 
LVL 20

Expert Comment

by:Venabili
ID: 16399835
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
 

Author Comment

by:Jasbir21
ID: 16406005
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
 

Author Comment

by:Jasbir21
ID: 16406012
0
 

Author Comment

by:Jasbir21
ID: 16406175
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
 
LVL 92

Expert Comment

by:objects
ID: 16406179
what did u want  to do with the sorted map?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16406578
>>How to use sorted map  here ..Pls help..

You would need:

Map<Double,Integer> map = new TreeMap<Double,Integer>();
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 16406601
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
 

Author Comment

by:Jasbir21
ID: 16407483
just saw this...
0
 

Author Comment

by:Jasbir21
ID: 16407509
>>>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
 

Author Comment

by:Jasbir21
ID: 16407584
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
 

Author Comment

by:Jasbir21
ID: 16407650
sorry..its i got errror : cannot find symbol BigDecimal, i hope it is ok if the arraylist of map is also BigDecimal
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16408041
You need to import

java.math.BigDecimal
0
 

Author Comment

by:Jasbir21
ID: 16409929
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
 

Author Comment

by:Jasbir21
ID: 16409967
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16410511
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16410954
btw jasbir - *is* this homework?
0
 

Author Comment

by:Jasbir21
ID: 16411065
Thanks a lot...I learnt a lot.. I understand what is being done now.. you get the values and then your floor them.. :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16411071
:-)
0
 

Author Comment

by:Jasbir21
ID: 16411072
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
 

Author Comment

by:Jasbir21
ID: 16411075
i am new to use maps, treemaps, jfreegraphs  ..

i am very sorry for troubling a lot..but thanks for helping a lot..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16411076
No - i didn't think it was. Hope it wasn't made too much harder by the removal of my code
0
 

Author Comment

by:Jasbir21
ID: 16411081
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question