-6

I'm trying to understand C++ as I'm new. The below code crashes every time I run it. As far as I know, it should output 0 to 9. Stating that i = num[i] I know is pointless however adding this extra code makes it crash. Why?

    int num[10];

    for(int i = 0; i < 10; i++){
        i = num[i];
        cout << num[i];
    }

Edit - Thanks, guess I didn't see that little error. Seems obvious now...

kepsek
  • 23
  • 3
  • why should it print 0 to 9? You never assigned anything to the array elements – 463035818_is_not_an_ai Dec 13 '17 at 14:52
  • 2
    try `num[i] = i;`. – user1810087 Dec 13 '17 at 14:52
  • Why are you using an array at all? – Scott Hunter Dec 13 '17 at 14:53
  • Just experimenting, thanks user1810087 – kepsek Dec 13 '17 at 14:55
  • Your code is incomplete; in particular, it seems to be missing a `main()` function and the initialisation of `num[]`. Please [edit] your code so it's a [mcve] of your problem, then we can try to reproduce and solve it. You should also read [ask]. – Toby Speight Dec 13 '17 at 14:56
  • @TobySpeight The code itself was easy enough to understand regardless of the main function. Question, why would I need to initialise the num array? And thanks Ill look into the link. – kepsek Dec 13 '17 at 15:01
  • "_why would I need to initialise the num array_" if `num` was in global scope - you wouldn't, since it would be default-initialized to `0`. If it is in local scope (i.e. in a function, like `main`), it would contain indeterminate values. Such an answer would be answered, in detail, in any [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Dec 13 '17 at 15:08
  • If you don't initialise the contents of `num[]` and it was declared in your function, then the reads from it will be *undefined* - you might get negative values or nasal demons. Running your program under Valgrind will help you catch errors such as that; also make sure you've enabled as many compiler warnings as you can bear. – Toby Speight Dec 13 '17 at 15:08
  • It's possible that you meant `num[i] = i;` - is that the case? – Toby Speight Dec 13 '17 at 15:10
  • @TobySpeight There was no error in what I wrote, but what I intended to have happen was solved by doing that. I usually make dumb mistakes. Thanks for your comments – kepsek Dec 13 '17 at 15:14

2 Answers2

1

You're assigning num[i] which is undefined to i. Then you're incrementing i, and accessing element i (undefined) of num in the next iteration of the loop, which is only 10 items long.

In effect you're trying to access memory at position num[1978253], which you have no control over.

xyious
  • 1,065
  • 4
  • 13
  • 29
0
int num[10];

    for(int i = 0; i < 10; i++){
        i = num[i];
        cout << num[i];
    }

In the above piece of code you haven't stored anything to the array you declared. So it would print some random values out.

The following would work as you think :

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

    for(int i = 0; i <= 10; i++){

        cout << num[i];
    }
Mathews Sunny
  • 1,796
  • 7
  • 21
  • 31