I have declared an array and using the function subset(uint start, uint end) I am trying to get the subset of that array. But Solidity throws an error "The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance."
Here's my Code..
pragma solidity >=0.8.0 <0.8.17;
contract Array{
uint256[] public numbers;
uint public length;
function addNumToArr(uint num)public
{
numbers.push(num);
length=numbers.length;
}
//function to get the subset of an arry
function subsets(uint start, uint end)public view returns(uint[] memory){
uint[] memory arr;
uint count=0;
for(uint i=start;i<end;i++)
{
arr[count]=numbers[i];
count++;
}
return arr;
}
}
input: numbers=[2,4,6,8,10,12]
Operation: subset(1,4)
Expected output: arr=[4,6,8]