Home > Software engineering >  Finding Prime and Composite Elements in an array. Print primes in ascending order and composite in d
Finding Prime and Composite Elements in an array. Print primes in ascending order and composite in d

Time:05-25

I am successful in identifying prime and composite from an array. But my qsort function seem to not have any effect when I print the output. I need the primes to be ascending and composite to be descending. When I run the code, it does not sort the output, though it identifies primes and composites.

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

int compare_Asc(const void *a_void, const void *b_void) {
    int a = *(int *)a_void;
    int b = *(int *)b_void;
    return a - b;
}

int compare_Desc(const void *a_void, const void *b_void) {
    int a = *(int *)a_void;
    int b = *(int *)b_void;
    return b - a;
}

int main() {
    int i = 0, n, x, p, c, z, w, j = 0, k = 0, cmpst, null;
    int prm;
    int prime[50], composite[50], input[50];
    printf("How many inputs are you be working with?\nNote: 50 Maximum Inputs\n"); 
    scanf("%d", &n);
    printf("Enter the numbers.\n", n);
    
    for (i = 0; i < n; i  ) {
        scanf("%d", &input[i]);;
    }
    for (i = 0; i < n; i  ) {
        if (input[i] % 2 != 0) {
            prime[p  ] = input[i];
            prm = p;
        } else
        if (input[i] >= 2 && input[i] % 2 == 0) {
            composite[c  ] = input[i];
            cmpst = c;
        }
    }
    printf("Prime Numbers:"); 
    qsort(prime, prm, sizeof(int), compare_Asc);
    for (i = 0; i < p; i  ) {
        printf("%d", prime[p]);
    }
    printf("Composite Numbers:"); 
    qsort(composite, cmpst, sizeof(int), compare_Desc);
    for (i = 0; i < c; i  ) {
        printf("%d", composite[c]);
    }
    return 0;
}

CodePudding user response:

Single letter variables names are to be avoided... except for i, j and k used in for() loops only.

You're not updating the index of the arrays c and p as the numbers are being printed out. The arrays are being sorted fine.

In the code below I also remove redundant variables, and rename n to input_count, c to compo_count and p to prime_count.

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

int compare_Asc(const void *a_void, const void *b_void)
{
    int a = *(int *) a_void;
    int b = *(int *) b_void;
    return a - b;
}

int compare_Desc(const void *a_void, const void *b_void)
{
  int a = *(int *) a_void;
  int b = *(int *) b_void;
  return b - a;
}

int main ()
{
    int i = 0;
    int input_count = 0;
    int prime_count = 0;
    int compo_count = 0;
    int prime[50];
    int composite[50];
    int input[50];

    printf("How many inputs are you be working with?\nNote: 50 Maximum Inputs\n");
    scanf("%d", &input_count);
    printf("Enter the %d numbers.\n", input_count);

    for (i = 0; i < input_count; i  )
    {
        scanf("%d", &input[i]);
    }

    for (i = 0; i < input_count; i  )
    {
        if (input[i] % 2 != 0)
        {
            prime[prime_count] = input[i];
            prime_count  = 1;
        }
        else if (input[i] >= 2 && input[i] % 2 == 0)
        {
            composite[compo_count] = input[i];
            compo_count  = 1;
        }
    }

    printf("Prime Numbers:");
    qsort(prime, prime_count, sizeof(int), compare_Asc);
    for (i = 0; i < prime_count; i  )
    {
        printf("%d ", prime[i]);  // <<-- HERE, not [p]
    }
    printf( "\n" );

    printf ("Composite Numbers:");
    qsort(composite, compo_count, sizeof(int), compare_Desc);
    for (i = 0; i < compo_count; i  )
    {
        printf("%d", composite[i]);  // <<-- HERE, not [c]
    }
    printf( "\n" );
    return 0;
}

CodePudding user response:

There are some major issues, in the posted code, worth mentioning.

Variables

Declaring all the variables at the beginning of the scope, instead of just before where they are used, can hide bugs.

Uninitialized variables, are an even worse source of errors, because their values are indeterminated.

int i=0, n, x, p, c, z, w, j=0, k=0, cmpst, null;
//             ^  ^                         ^^^^ ?

// ... Later, in the code:
           prime[p  ] = input[i];
//               ^^^               What value is incremented?
//                                 Where is [p  ]? Is it inside the prime array?

A correct initialization would prevent undefined behavior.

int p = 0, c = 0;
int composite[50], input[50];
for(int i = 0; i < n ;   i) {
    if ( is_prime(input[i]) ) {  // <-- More on this, later.
        prime[p  ] = input[i];
    }
    else {
        composite[c  ] = input[i];
    }
}

Loops

This happens a couple of times, just because the code itself is duplicated (another code smell):

for(i=0;i<p;i  ){
//  ^^^^^^^^^^^             We want to iterate over [0, p).
     printf("%d",prime[p]);
//                     ^    But this always prints the element one past the end
}

Even if it's just a simple loop, it could be a good idea to write a (testable and reusable) function

void print_arr(size_t n, int arr[n])
{
    for (size_t i = 0; i < n;   i) {
        printf("%d ", arr[i]);
    } //                  ^
    putchar('\n');
}

// ... Later, in main:
    print_arr(p, prime);
    print_arr(c, composite);

Primes or composite

I am successful in identifying prime and composite from an array

Well, no. Not with this code, I'm sorry.

if (input[i]%2 != 0) {                   // Those are ALL the ODD numbers!
     prime[p  ]=input[i];
}
else if(input[i]>=2 && input[i]%2==0){   // Those are the EVEN numbers greater than 0
     composite[c  ]=input[i];
}
// What about 0 and the even numbers less than 0?

Not all the odd numbers are prime number (it's a little more complicated than that) and 2 itself is a prime, not a composite.

It's unclear to me if this is a terminology issue or if the snippet is only a placeholder for a proper algorithm. In any case, there are multiple examples of primality test functions in SE sites (I'm quite confident some are posted almost every day).

Overflow risk

See chux - Reinstate Monica's comment:

return a-b; risks overflow when a, b are large int values.
Consider return (a > b) - (a < b); for a full range solution.

  •  Tags:  
  • c
  • Related