Thread: Why array doesn't print correct numbers?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    2

    Why array doesn't print correct numbers?

    Following code:

    Code:
    #include <stdio.h>
    
    int izbaciSveProste(int n, int x[], int y[])
    {
        int i;
        int flag=0;
    
        for(i=2; i<n/2; i++)
        {
            if(n%i ==0)
            {
                flag =1;
                break;
            }
        }
        if(flag==1)
            return 0;
        else
            return 1;
    }
    
    int main()
    {
        int i,j,n,x[100],y[100],dest=0;
    
        printf("Koliko elemenata zelite u polju?\n");
        scanf("%d", &n);
    
        printf("Unesite elemente:\n");
        for(i=0;i<n;i++)
        {
        scanf("%d",&x[i]);
        }
    
        int len = sizeof(x)/sizeof(x[0]);
    
        for(i=0; i<len; i++)
        {
        if(izbaciSveProste(n,x[i],y[i]))
        {
        y[dest++]=x[i];
        for(j=i; j<len-1; j++)
        {
        x[j] = x[j+1];
        y[dest++]=x[j];
        }
        i--;
        len--;
        }
        }
        printf("Elementi nakon brisanja su:\n");
        for(i=0; i<dest; i++)
        printf("%d\n",y[i]);;
    
        printf("\n");
    
        return 0;
    }
    Purpose of this program should be to delete all prime numbers from array x[] with n elements, remaining elements should be rewritten in array y[] and show count of elements in array y[] in the end.I believe that function is okay and error is in main() specifically in storing y[].

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first step would be learning how to indent code.
    Code:
    #include <stdio.h>
    
    int izbaciSveProste(int n, int x[], int y[])
    {
      int i;
      int flag = 0;
    
      for (i = 2; i < n / 2; i++) {
        if (n % i == 0) {
          flag = 1;
          break;
        }
      }
      if (flag == 1)
        return 0;
      else
        return 1;
    }
    
    int main()
    {
      int i, j, n, x[100], y[100], dest = 0;
    
      printf("Koliko elemenata zelite u polju?\n");
      scanf("%d", &n);
    
      printf("Unesite elemente:\n");
      for (i = 0; i < n; i++) {
        scanf("%d", &x[i]);
      }
    
      int len = sizeof(x) / sizeof(x[0]);
    
      for (i = 0; i < len; i++) {
        if (izbaciSveProste(n, x[i], y[i])) {
          y[dest++] = x[i];
          for (j = i; j < len - 1; j++) {
            x[j] = x[j + 1];
            y[dest++] = x[j];
          }
          i--;
          len--;
        }
      }
      printf("Elementi nakon brisanja su:\n");
      for (i = 0; i < dest; i++)
        printf("%d\n", y[i]);;
    
      printf("\n");
    
      return 0;
    }
    > I believe that function is okay and error is in main() specifically in storing y[].
    That's a good guess - go with it.

    Now is a good time to investigate debuggers and follow how your y array changes over time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array doesn't show correct value
    By vajra11 in forum C Programming
    Replies: 7
    Last Post: 08-05-2018, 04:51 AM
  2. How not to print duplicate numbers in an array?
    By ahmedbatty in forum C Programming
    Replies: 4
    Last Post: 11-16-2011, 01:22 PM
  3. Replies: 1
    Last Post: 11-04-2011, 01:16 PM
  4. Print numbers within a certain range of an Array
    By omegasli in forum C Programming
    Replies: 4
    Last Post: 04-10-2011, 01:08 AM
  5. What is wrong here, just won't print the correct numbers
    By Unregistered in forum C++ Programming
    Replies: 16
    Last Post: 05-10-2002, 03:31 PM

Tags for this Thread