Value of array index not increasing. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Value of array index not increasing.

for(int i=1; i<=num; i++){ if(i%2!=0){ prime [x] = i; count++; } } Here if the condition occurs the value of i store in the array prime. But the index of the array is not increasing, all the values are storing at [0] index and destroy the previous value. Try all the other methods but didn't find anything. Please help.

3rd Nov 2018, 1:41 PM
Prabhjot Singh Sahni
Prabhjot Singh Sahni - avatar
3 Answers
+ 4
Where is the declaration of <x> and <count>? assuming <x> initialized to zero, you can do prime[x++] = i; that is if you use <x> for array index, not sure what <count> here is for.
3rd Nov 2018, 2:09 PM
Ipang
+ 3
Alright, good to know you have it solved. Good job : )
3rd Nov 2018, 2:59 PM
Ipang
+ 1
count is here for how many times my for loop worked, and yeah I tried prime[x++] and prime[x+1], but they both didn't work for me. I have another solution then I'll be very thankful to yours.
3rd Nov 2018, 2:42 PM
Prabhjot Singh Sahni
Prabhjot Singh Sahni - avatar