could someone explain me why it's not working ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

could someone explain me why it's not working ?

I tried do some random array with rand() srand () and time(0) i write somthing like that. Code: #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main () { int a[10]; srand(time(0)); for (int x = 1; x <= 11; x++) { cout << endl << 1 + (rand() % 6) << endl; a[x] =(rand() % 5); cout << endl << x-1 << ':'<< a[x] ; } for (int i=1; i<=11; i++) { cout << endl << a[i] << ' '; } } and i get that output: (strange to me) 4 0:1 6 1:4 4 2:4 4 3:0 1 4:2 6 5:0 4 6:2 2 7:1 4 8:1 6 9:2 4 -1:258111004 4 0:1 6 1:3 4 2:4 2 3:3 5 4:3 3 5:4 3 6:2 5 7:4 3 8:2 2 9:3 6 -1:258111004 5 0:0 1 1:3 2 2:1 4 3:3 5 4:3 4 5:4 2 6:1 3 7:2 6 8:0 2 9:3 3 -1:258111004 1 0:3 5 1:2 4 2:0 1 3:4 1 4:4 4 5:0 2 6:1 2 7:2 4 8:3 1 9:4 2 2:0 5 3:3 4 4:0 3 5:2 4 6:3 2 7:2 3 8:2 2 9:4 3 -1:258111004 4 0:3 5 1:0 4 2:2 4 3:2 2 4:2 3 5:0 2 6:2 3 7:2 5 8:4 2 9:4 1 1:0 2 2:3 4 3:4 3 4:2 4 5:2 5 6:0 4 7:2 3 8:4 5 9:0 6 1:0 5 2:1 5 3:0 2 4:2 2 5:2 1 6:3 5 7:3 1 8:3 6 9:1 4

21st Apr 2017, 2:23 PM
MKey
MKey - avatar
2 Answers
+ 4
thx i find mistake :)
21st Apr 2017, 2:34 PM
MKey
MKey - avatar
+ 3
you are accessing memory blocks that are not part of the array for loop iterates from 1 to 11 a[10] means you have ten elements in your array ranging from a[0] to a[9]. but you are accessing a[10] and a[11] when you use a[x] and a[i]
21st Apr 2017, 2:33 PM
seamiki
seamiki - avatar