Posts: 13 Location: USA Joined: 05.04.12 Rank: Newbie
Posted on 30-09-12 00:22
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.
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..
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.