Thread: Index Array simpe task. Need help.

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    21

    Index Array simpe task. Need help.

    Array is defined by the user (size & members)
    1 2 3 4 5 6 7 8 9 10

    input k

    if index (not the number) can be divided with the k, then that number (not the index is printed).

    For example

    1 2 3 4 5 6 7 8 9 10 array

    k=3

    Solution is: 4 7 10

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Where is YOUR code?

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    21

    Here it is

    Quote Originally Posted by flp1969 View Post
    Where is YOUR code?
    Code:
    #include <stdio.h>
    #define MAX 100
    int main()
    {
    int brojevi[MAX];
    int n, i, k, indikator;
    
    
    
    
    printf("Array input: ");
    scanf("%d", &n);
    if(n<1 || n>MAX)
    {
    printf("Error !\n");
    return -1;
     }
    
    
    printf("Insert array elements: ");
     for(i=0;i<n;i++)
    scanf("%d", &brojevi[i]);
    
    
    
    
    /printf("Insert number k: ");
    scanf("%d", &k);
    if(k == 0)
    {
    printf("Error!\n");
    return -1;
    }
    
    
    
    
     indikator = 0;
    
    
    
    
    for(i=0;i<n;i++){
     if(brojevi[i]%k == 0)
    {
     indikator = 1;
    printf("%d ",i);
    
    
    
    
     }
    }
    
    
    
    
    
    
    if(indikator == 0){
     printf("there is no index that can be divided with %d!\n",k);
    }
    
    
     return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    you want the program to return the number if the arrays index is devisable by k ie if k was 2 it would return every other element or do you want the index of the integers in the array that are devisable by k

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    21
    "you want the program to return the number if the arrays index is devisable by k" yes, this is what i want

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    have a look at the for loop then

  7. #7
    Registered User
    Join Date
    May 2019
    Posts
    21
    Quote Originally Posted by cooper1200 View Post
    have a look at the for loop then
    I'm looking at it, but don't have the solution yet

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    well as i said it depends which of the above you wanted to do you have it set for the second option not the first

  9. #9
    Registered User
    Join Date
    May 2019
    Posts
    21
    ok... i agree...help me to set it up for the first option..thanks.

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    here is a hint
    Code:
    for(i =0; i <8; i++)
    {
             printf("%d", myarray[i])
    }
    will cycle through the elements of my array and print out to screen the value in each element of my array

    Code:
    for (i=0;i<7;i++)
    {
        printf{"%d",i);
    }
    will print out the value of i
    coop

  11. #11
    Registered User
    Join Date
    May 2019
    Posts
    21
    Quote Originally Posted by cooper1200 View Post
    here is a hint
    Code:
    for(i =0; i <8; i++)
    {
             printf("%d", myarray[i])
    }
    will cycle through the elements of my array and print out to screen the value in each element of my array

    Code:
    for (i=0;i<7;i++)
    {
        printf{"%d",i);
    }
    will print out the value of i
    coop
    I already have this in my code... But thanks, anyway...

  12. #12
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    the point is you are dividing the elements by k not the index

  13. #13
    Registered User
    Join Date
    May 2019
    Posts
    21
    Can you please do the whole code here, so I can test on my side. Thank you.

  14. #14
    Registered User
    Join Date
    May 2019
    Posts
    21
    If the index can be divided by the k (input by the user at the beginning), then I need to printf that element, not the index...

  15. #15
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Code:
    myarray[i] % 2
    divides he element

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