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

687474703a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f372f37302f5465747261686564726f6e2e676966

 

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: , , ,