VBA – Sorting VBA array – Functions in VBA

In the current article, I will show how to resolve with VBA an entry exam for programming in HackBulgaria. The problems could be resolved in any language and I will use VBA for the first two in this article.

The idea is to calculate the volume of a tetrahedron (the thing rolling below). The amount of water should be in liters. After this, you should calculate how many tetrahyrdons can you fill up with the the given liters. The detailed description of the problem is here.

Problem 1 – 1-Fill-tetrahedron-with-water

Problem 2 – 2-Tetrahedron-filled-with-water



So, what I have done? I have generated 2 custom functions and 1 sub routine. The two custom functions calculate how much liters of water fill a tetrahyrdon and how many tetrahydron can we fill. The sub routine is needed, in order to sort the array of tetrahydrons – thus, when we start filling them one by one we can see how many we have filled once the water is over. This is a small drawback of VBA – if I haev used another language solve this, then the sorting of an array (or list or anthing that contains systemized info) would have come automatic. Anyway, VBA is still a nice language.

Here is how the formula looks like in Excel:

And this is the code.

Enjoy it 🙂

Tagged with: , , ,