3

I have made a struct which has an address called playersAddress and a uint called numbers.

I then have a fixed sized array which has in it the playersAddress and the numbers.

When someone calls the function pickWinner, the highest number in the numbers variable compared with the other numbers will transfer a certain amount of ether to the address in the playersAddress.

Also the numbers in the numbers variable are always changing value. I would like to know in this situation would the only way to find the highest number in an array would be to use a loop? I have tried to write out a for loop in the pickWinner function, but it keeps coming up with an error.

function getPickWinner() public {

    var test = numbers;
    var largest = 0;
    var i;

    for (i = 0; i < test.length; i++) {
        if (test[i] > largest) {
            largest = test[i];
    }
}

The error comes up in the var i; part and it says assignment necessary for type detection.

I would like to know what this means and how I could fix it? At the moment I am not worrying about gas or anything I just want to experiment a bit first. If gas is a really big issue would it be possible in this scenario to do the loop offchain but keep everything else the way it is ? How could I possibly go about doing it offchain? thank you

6 Answers 6

1

This code will do what you ask.

Note the variable declarations. var is deprecated.

pragma solidity ^0.4.7;

contract test3 {

    uint256[5] public numbers = [1, 2, 100, 4, 5];

    function getPickWinner() public view returns(uint256){
        uint256 largest = 0; 
        uint256 i;

        for(i = 0; i < numbers.length; i++){
            if(numbers[i] > largest) {
                largest = numbers[i]; 
            } 
        }
        return largest;
    }
}

I have set the variable numbers just for testing purposes. Hope this help

0

Use utilities to solve such kinda problems. No builtin functions available.

Considering Off-chain calculations, yes it would be great if we calculate/transfer assets among users in our system. And assign only their true assets by submitting on main chain finally when they leave our system.

PS: Like Plasma MVP

0

var is deprecated. Give your variables types. In particular, use uint256 i to silence this, but go ahead and give all the variables types:

uint256 test = numbers;
uint256 largest = 0;
uint256 i;
0

Note: If you are using var for a declaration of the variable i then you need to assign value to variable at the time of declaration like var i=0. I suggest you to use uint instead of var for i and largest variable.

0

Try my utilities library https://www.npmjs.com/package/libsol to get the greatest number from an Array of uint256s

thanks

0
0

You can use local struct or data to keep largest and return. You can also return index, Easy to use more useful.

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
type UFixed256x18 is uint256;

contract Maths {


    function max(uint256[] calldata numbers)
        public
        pure
        returns (uint256 largest)
    {
        uint256 len = numbers.length;
        require(len > 0, "Empty numbers");
        largest = numbers[0];
        if (len == 1) {
            return largest;
        }
        for (uint256 i = 1; i < len; i++) {
            uint256 curr = numbers[i];
            if (curr > largest) {
                largest = curr;
            }
        }
    }

    function max_i(uint256[] calldata numbers)
        public
        pure
        returns (uint256 largest, uint256 index)
    {
        uint256 len = numbers.length;
        require(len > 0, "Empty numbers");
        largest = numbers[0];
        if (len == 1) {
            return (largest, 0);
        }
        for (uint256 i = 1; i < len; i++) {
            uint256 curr = numbers[i];
            if (curr > largest) {
                largest = curr;
                index = i;
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.