Skip to content

For Loop To Generate 1-100 #3303

Answered by KMean
uthmanabayomi asked this question in Q&A
Jan 20, 2025 · 2 comments · 3 replies
Discussion options

You must be logged in to vote

@uthmanabayomi would be helpful if you share the entire code as it's a bit difficult to understand what you're trying to do. I can see you're using the pure modifier (you can't access or modify any state variables) so I guess you want to do everything in memory. Because you can't initialize dynamic arrays in memory you will have to define how much memory your variable will take.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract Test {
    function returnNumbers() public pure returns (uint[100] memory) {
        uint[100] memory numbers; // initialize the array with fixed size

        for (uint i = 0; i < 100; i++) {
            numbers[i] = i + 1; 
        }
        return

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@EngrPips
Comment options

@uthmanabayomi
Comment options

@EngrPips
Comment options

Answer selected by uthmanabayomi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants