# How to calculate average of some numbers in the secret contract of Enigma?

I want to calculate the average of the Millionaires based on this article. https://blog.enigma.co/getting-started-with-the-enigma-protocol-a-complete-developers-guide-170b7dfa5c0f

And this is my code.

``````// CALLABLE FUNCTION to calculate the average assets
function computeAverage(address[] _addresses, uint[] _netWorths)
public
pure
returns (uint)
{
uint average;
uint sum = 0;
for (uint i = 0; i < _netWorths.length; i++) {
sum += _netWorths[i];
}
average = sum / _netWorths.length;
return average;
}

// CALLBACK FUNCTION to change contract state
function averageAssets(uint _uint) public onlyEnigma() {
averageAssetsResult = _uint;
emit CallbackFinished();
}
``````

This average becomes some big number like 52576064 (when the actual number is 125), 52573696 (when the actual number is 100).

I think this is natural because in secret contract we need to hide the actual number. Is there some built-in average function?

• Do you have an example of the parameters used for a failed call to `computeAverage`? I see nothing obviously wrong.
– Ismael
Sep 18, 2018 at 11:45
• Are the input parameters, `_addresses` and `_netWorths`, encrypted? On the page that you cited they use the function `getEncryptedValue(value)` to encrypt any unencrypted input. Sep 18, 2018 at 20:14
• @Ismael For example, I calculated the average with 100 and 150, the result should be 125, but actually the result is 52576064. Sep 19, 2018 at 5:41
• @sfmiller940 Yes, input parameters are encrypted. And these parameters are decrypted in the CALLABLE FUNCTION and calculated the result. Sep 19, 2018 at 5:43
• @hidehiro-nagaoka When you read the result, are you using the same format as `handleSubmit() ` in the example? Did you try the original example and did it return the `richestAddress` correctly? Sep 20, 2018 at 16:54

## 2 Answers

The original contents of this post are on the dev forums here, which I see you've joined!!

Super glad you posted this initially, actually helped point out a little bug that has now been resolved, so thank you :)

There were two issues at play here:

1.) string vs. uint: In the developer guide pushed out, there was a bug where we were actually passing in an encrypted value of a string type instead of a number type for the net worth. To fix this, I made two modifications, which you can see in the guide.

The first is in the AddMillionaireDialog.js section (line 126) - I specify that the input field is of type number.

The second is in the MillionairesProblemWrapper.js section (line 58) - I pass in a BigNumber instance of the net worth.

2.) Even if you had properly passed in an int, it wasn’t being properly handled. This has now been resolved in the internal services and you can ensure you are using the latest correct code by running docker-compose build --no-cache within your network tab (the one that holds the enigma-docker-network) and relaunching with ./launch.bash -s -t

Hopefully with these two you should be good to go, but please reach out if something isn’t working still :)

Excited to see you working on your own contracts! I really appreciate you reporting this and pointing out a slight bug that will be resolved very shortly. I will update you extremely soon on the matter :)

P.S. I tried commenting on your post but due to reputation limitations since I'm new to stackexchange, I had to post this answer...but the real answer will be coming out very soon!

Also, I encourage you to post your questions on our dev forms (https://forum.enigma.co/c/secret-contracts-dapps) for quicker responses from the team/community!

Adi

• Thank you very much!!! I will post it to the Enigma dev forum! Oct 12, 2018 at 3:01