Then it prits out numbers divided by k, not the index![]()
Then it prits out numbers divided by k, not the index![]()
prints only number that are divided by the k
3 should be here right output
yes because u changed your if statement again
the if statement needs to divide the index (i) by % k
the printf needs the myarray[i] to print the element
the if statement decides weather the index is devisable by k the print f prints the info to the screen
for(i=0;i<n;i++){
if(i%k == 0)
{
indikator = 1;
printf("%d ",brojevi[i]);
yes dont fotget your closing } bracket for the if statement i susspect it was a typo though
id you want to count the number of times its run u need to change indikator = 1; to indikator = indikator +1;
it's good now...one more question..how i can print only positive numbers at the end...you were so helpful...thank you.
imnot sure whar youmean but bed for me now im afraid i have things o do tomorrow
thank you so much for your help...
array -5 -3 -2 1 7 -8 9
k=2
no negative, only positive result...here it would be:7
Almost there, but there are another problems:
1- In your description you said all elements which index is divisible by k will be printed, but index 0 is always divisible by any k (except when k=0, which isn't allowed). So brojevi[0] will always be printed... Seems to me your sequence starts at index 1, instead of 0, isn't it? If that's so, with k=3, the sequence:
Will result in "3 6 9".Code:1 2 3 4 5 6 7 8 9 10
If this is the case, it is simple to modify the code. Instead of ( (i % k) == 0 ) you could do ( ( ( i + 1 ) % k ) == 0 ).
2- Your description says the array size (number of elements) is provided by the user. The user can choose more than 100 elements! You can print something as "Choose k (between 1 and 100)"...
PS: In problem #1 I am suggesting you use parentesis around operations because I think you aren't very familiar with operators precedence. Your code is not wrong at this matter, but sometimes an expression isn't doing things as you think it should. For example: i % k == 0: What is compared to zero? i % k or just k? In this case % operator has precedence over ==, and i % k is evaluated first (so, i % k == 0 do what you expect!). But consider this:
It is intuitive, but == has precedence over ^. This expression is equivalent to x = a ^ ( b == 0 ).Code:x = ( a ^ b == 0 );
So, use parentesis when you are not sure...
(i+1)% k will cause the array to go ot of ranged if i +1 happends to be devisable by k because the next thing he does iis a printf to print the element at the index if the issue is he doesnt want the first index divisable by what ever k is start the loop from 1 raher than 0
coop