Thread: Delete element of array

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    15

    Delete element of array

    Hello!
    I have an assignment in school to find prime numbers.
    In my function i'm supposed to find the prime numbers and delete the numbers that are not a prime.
    The application is not complete so don't worry about my return value etc.

    I have looked up how other people have done their deletion of an element but I cannot seem to get it to work.

    Any help would be nice, thank you!

    Code:
    int checkForPrime(int* a, int num){ 
       int c = 0, j = 0;
    
    
        for(int i = 0; i < num; i++){
                for (j = 2; j <= (*a+i)/2; j++){
                    if(((*a+i) % j) == 0)
                        break;
                }
                if(*(a+i) > 1 && !(j<= (*a+i)/2)){
                    c++;
                    //printf("yes");
                }
                else{
                    //I want to delete the number that comes here,
                    //Will always know that *(a+i) is the correct number to delete.
                    }
                    printf("%d ", *(a+i));
        }
    
    
        return c;
    }
    
    
    int main()
    {
        int number[100];
        int counter;
        int result;
    
    
        printf("Numbers to check: ");
        scanf("%d", &counter);
       // number = (int*)malloc(num*sizeof(int));
    
    
    
    
        for(int i = 0; i < counter; i++){
            printf("number %d: ",i+1);
            scanf("%d", number+i);
    
    
        }
    
    
        result = checkForPrime(number, counter);
    
    
        //printf("%d", result);
    
    
        return 0;
    }

  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
    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. Delete an element in a dynamic array?
    By beta3designs in forum C Programming
    Replies: 34
    Last Post: 08-22-2011, 03:34 AM
  2. how delete 1th element of a sorted array
    By vicky_4040 in forum C Programming
    Replies: 4
    Last Post: 10-11-2009, 06:12 AM
  3. delete same array element in C
    By tasosa in forum C Programming
    Replies: 12
    Last Post: 04-09-2009, 09:36 AM
  4. Delete Samllest Array Element
    By ptysell in forum C Programming
    Replies: 5
    Last Post: 11-22-2004, 07:27 PM
  5. Cannot delete last element in array
    By Simon in forum C Programming
    Replies: 10
    Last Post: 09-12-2002, 08:29 PM