Array

When i run the following program i get a pop-up telling me it has stopped working and to close the program.
After changing various line's i still cant find the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstddef>
#include <array>

int main()
{
    int InArr [10];

    for (auto i : InArr)
    {
        InArr [i] = i;
    }
    for (auto a : InArr)
    {
    std::cout << '\n' << a << " = " << InArr[a];
    }

    return 0;


}
Last edited on
I understand not expect me to do the program.
I see, you should check out the for loop
http://www.cplusplus.com/doc/tutorial/control/
Trying to get to grip's with Array's, But thing's are really playing up now. I wrote the following program with surprising result's:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>
int main()
{
    int VecI[10] = {10,20,30,40,50,60,70,80,90,100};

    for (auto i : VecI)
    {
        std::cout << "\nVariable = " << i << " Contents = " << VecI[i];
    }
}


The output was:

Variable = 10 Contents = 10
Variable = 20 Contents = 2686868
Variable = 30 Contents = 2686812
Variable = 40 Contents = 2130567168
Variable = 50 Contents = 2130567168
Variable = 60 Contents = 0
Variable = 70 Contents = 2130567168
Variable = 80 Contents = 1
Variable = 90 Contents = 380
Variable = 100 Contents = 68

I was expecting to see:

Variable = 1 Contents = 10
Variable = 2 Contents = 20
Variable = 3 Contents = 30
Variable = 4 Contents = 40
Variable = 5 Contents = 50
Variable = 6 Contents = 60
Variable = 7 Contents = 70
Variable = 8 Contents = 80
Variable = 9 Contents = 90
Variable = 10 Contents = 100
for (auto i : VecI)
This is how you iterate through all elements in the array. i is the value of the element (not the index).
std::cout << "\nContents = " << i;
Thank's Peter87.
Topic archived. No new replies allowed.