Java Programming Help
I am currently going to class for programming, and our challenge this week was all about classless objects. Not that it matters, but I decided to do mine on Linux Distro\\\\\\\\\\\\\\\'s; and a portion of the project was to let the user select a range, and then divide that range into 10 equal increments. Then display a histogram ( bar graph ) that displays those that fall into each of the increments.
I have gotten my code to work, no problem there. The problem is I believe there is a more efficient way to do it, and I just cannot see it. It is just bugging the hell out of me, and I just can\\\\\\\\\\\\\\\'t look at this one section of code without thinking that there has to be a better way of doing it without utilizing a 11 branch if statement. Was just hoping that someone else could see what I can\\\\\\\\\\\\\\\'t.
//START CODE```markup for( int ix=0; ix<length; ix++ ){
if( distro[ix] ._sizeMB < lowB ){
}else if( distro[ix] ._sizeMB < (lowB + increment) )
occur[0]++;
else if( distro[ix] ._sizeMB < (lowB + (increment*2)) )
occur[1]++;
else if( distro[ix] ._sizeMB < (lowB + (increment*3)) )
occur[2]++;
else if( distro[ix] ._sizeMB < (lowB + (increment*4)) )
occur[3]++;
else if( distro[ix] ._sizeMB < (lowB + (increment*5)) )
occur[4]++;
else if( distro[ix] ._sizeMB < (lowB + (increment*6)) )
occur[5]++;
else if( distro[ix] ._sizeMB < (lowB + (increment*7)) )
occur[6]++;
else if( distro[ix] ._sizeMB < (lowB + (increment*8)) )
occur[7]++;
else if( distro[ix] ._sizeMB < (lowB + (increment*9)) )
occur[8]++;
else if( distro[ix] ._sizeMB < highB )
occur[9]++;
}
What the function is doing is loading up an array with the portions of data that fall into that realm. Increment is equal to (highB - lowB)/10, the length of the distro[] array can be any number, lowB and highB and user input, and occur[] is just an array I created to store the occurances of each of the increments, and distro[] .sizeMB is equal to the size of the Linux Distro in MB.
I love efficient code, and I know there has to be a way to execute what this does with a lot less code involved. I keep thinking it has to do with changing out the (increment*#) and the occur[#] with an equation from ix, but since the length is undefined I just can\\\\\\\\\\\\\\\'t see the equation...
This is in Java by the way. Probably should have mentioned that earlier..
Alright, I figured it out. I ended up just nesting a for loop inside the original for loop:
for( int ix=0; ix<length; ix++ ){
for(int ixx=0; ixx<10; ixx++){
if(distro[ix] ._sizeMB >= lowB &&
distro[ix] ._sizeMB < (lowB + increment ) ){
occur[0]++;
break;
}else if(distro[ix] ._sizeMB >= lowB &&
distro[ix] ._sizeMB < (lowB + (increment * (ixx+1) ) ) ){
occur[ixx]++;
break;
}
}
}
In my mind, this is much more elegant then how I orginially had it; hate having more then 3 or 4 if statements, it just looks ugly. Sad that I spent so much time on what ended up being quite simple in a way.
It was still bugging me because technically it would still take the same amount of processor time to run the improvement that I wrote above as a regular 11 branch if statement. Think I found the most optimized way to write the code.
for( int ix=0; ix<length; ix++ ){
if(distro[ix] ._sizeMB > lowB && distro[ix] ._sizeMB < highB){
indexD = (distro[ix] ._sizeMB) - lowB;
index = (int) (indexD / increment);
occur[index]++;
}
}