'Why does my array only print once and not twice here? [closed]

int nums [] {1,2,3,4,5,6,7,8,9,10};


int main(){
int size {10};

for (int i;i<size;i++)
    cout << nums[i] << " ";
    
for (int i;i<size;i++)
    cout << nums[i] << " ";

return 0;
}

This prints my array once but shouldn't I get my array printed twice not once?

c++


Solution 1:[1]

You need to initialize the i variable to the start index, like in for (i = 0; i < size; i++). Otherwise i can start in any value and this is undefined behavior.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kyku