# Given an array of integers randomly selected from 7 to 13 (included), build a function that remove the primes in the array

I am trying to solve the following exercise:

"Consider a function ​ removePrimes() be a C function that takes as input an array of integers, removes all numbers that are prime, and returns the compacted version of the array. Create an array of 200 integers, with random values between 7 and 13, and call ​ removePrimes()​ on the just filled array."

I used the following code in order to solve the task

  #include <stdio.h>
#include <stdlib.h>

void print_ar(int* ar, int len){
for (int i = 0; i < len; i++){
printf("%d ", ar[i]);
}
printf("\n");

}

int is_prime(int n ){
if (n==1){
return 1;
}
if (n == 2){
return 1;
}

for (int i=2; i<n;i++){
if (n % i == 0){
return 0;
}
}

return 1;
}

int* removePrimes(int* ar, int len, int* new_len){
int  k = 0;
int* temp = calloc(0, sizeof(int));
for (int i =0;i < len; i++){
if ( is_prime(ar[i]) == 1) {
continue;
}
else{
k=k+1;
temp = realloc(temp, sizeof(int) * k);
temp[k-1] = ar[i];
}
}
free(ar); // is this the correct use of free()? //
*new_len = k;
return temp;

}

int main() {

int n=0;
while (n<200){
printf("Insert an integer greater than 200: ");
scanf("%d", &n);
}
int* arr = malloc( n * sizeof(int));
for (int i =0;i<n;i++){
int k =rand()%7 +7;
arr[i] = k;
}
int new_len = 0;
print_ar(arr,n);
printf("\n");
arr = removePrimes(arr, n, &new_len);
print_ar(arr,new_len);

}


I am not sure about the usage of the free() function on the previous array. Is the free function used correctly, inside the removePrimes() function? Or should I call the free() outside, inside the main?

• user252524: if (n==1){ return 1; } --> Usually 1 is not considered a prime. Dec 4, 2021 at 18:36

Given that all the values are between 7 and 13, we can implement is_prime() much more efficiently:

bool is_prime(int n)
{
switch(n) {
case 7: case 11: case 13: return true;
default: return false;
}
}


In removePrimes(), we allocate and reallocate memory without checking for failure. Always test the return value from allocation functions is non-null before using it.

      temp = realloc(temp, sizeof(int) * k);

If realloc() returns a null pointer, then we have overwritten our only pointer to the memory that was allocated, so we can never free it - that's a memory leak.
• For the realloc() case, we need an extra variable, so that we only overwrite temp if the realloc() was successful. If not, we'll want to free temp at some point (perhaps immediately, if we're to return an error indication). Dec 5, 2021 at 7:51