### Trouble assigning array with loop

Hello. That's my first post. I'm 16. I like to program just for a hobby. My knowledge is very basic. I wanted to write a program that would display shared divisors for 2 selected numbers.

The idea is to first create 2 arrays for divisors of each number. Then select shared elements. The latter part is no problem. I didn't expect I would have such problem with creating an array. I don't understand it, I know that computer cannot be wrong, but I checked these 10 lines for nearly 1.5h. It's very strange.

 ``12345678910`` ``````int tempx[a], ix=0; for (l=2; l<=a; l++) { if (divisibility(a,l) == 1) { tempx[ix]=l; ix+=1; //cout << l << "dividable\n"; } }``````

Divisibility is a function that returns 1 if numbers are dividable, 0 if they aren't. tempx is just a temporary array. If I do something like this:
 ``123`` ``````if (divisibility(a,l) == 1) { cout << l << "dividable\n"; }``````

The output for number 100 will be:
 ```2dividable 4dividable 5dividable 10dividable 20dividable 25dividable 50dividable 100dividable ```

So everything seems fine. Now if I put this into array like in the first source I posted, the array for 100 will look like this:
 ```2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38, 39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67, 68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96, 97,98,99,100,32767```

Every number from 2 to 100 included. Why? Any help appreciated.
a is the number to input ix is just to increase the value of array each positive "if"
Last edited on
Now what's even stranger, I put both array assigment and cout dividable numbers. And array is what it is supposed to be.

 ``12345`` ``````if (divisibility(a,l) == 1) { tempx[ix]=l; ix+=1; cout << l << "dividable\n"; }``````

Gives
 ```2dividable 4dividable 5dividable 10dividable 20dividable 25dividable 50dividable 100dividable 2,4,5,10,20,25,50,100,1104814976```

That's very strange, isn't it? I know that last number is just because I selected unassigned element.
It's like cout << l; resolves the problem. Except it displays unwanted information. Is it an official C++ glitch or what?
Last edited on
Topic archived. No new replies allowed.