Thread: Index Array simpe task. Need help.

  1. #31
    Registered User
    Join Date
    May 2019
    Posts
    21
    Then it prits out numbers divided by k, not the indexIndex Array simpe task. Need help.-11ed-png

  2. #32
    Registered User
    Join Date
    May 2019
    Posts
    21
    prints only number that are divided by the k

  3. #33
    Registered User
    Join Date
    May 2019
    Posts
    21
    3 should be here right output

  4. #34
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    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

  5. #35
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    the if statement decides weather the index is devisable by k the print f prints the info to the screen

  6. #36
    Registered User
    Join Date
    May 2019
    Posts
    21
    for(i=0;i<n;i++){
    if(i%k == 0)
    {
    indikator = 1;
    printf("%d ",brojevi[i]);

  7. #37
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    yes dont fotget your closing } bracket for the if statement i susspect it was a typo though

  8. #38
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    id you want to count the number of times its run u need to change indikator = 1; to indikator = indikator +1;

  9. #39
    Registered User
    Join Date
    May 2019
    Posts
    21
    it's good now...one more question..how i can print only positive numbers at the end...you were so helpful...thank you.

  10. #40
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    imnot sure whar youmean but bed for me now im afraid i have things o do tomorrow

  11. #41
    Registered User
    Join Date
    May 2019
    Posts
    21
    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

  12. #42
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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:
    Code:
    1 2 3 4 5 6 7 8 9 10
    Will result in "3 6 9".

    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:

    Code:
    x = ( a ^ b == 0 );
    It is intuitive, but == has precedence over ^. This expression is equivalent to x = a ^ ( b == 0 ).

    So, use parentesis when you are not sure...

  13. #43
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    (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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-13-2013, 12:01 AM
  2. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  3. Replies: 2
    Last Post: 12-31-2007, 11:40 AM
  4. Simpe question about graphic
    By kennny2004 in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2006, 02:28 PM
  5. Simpe graphics in C
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-07-2001, 08:27 PM

Tags for this Thread