# C - Count Prime Numbers in Array - Beginner

Page 1 of 1

## 6 Replies - 433 Views - Last Post: 07 November 2019 - 06:10 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417759&amp;s=688f0848e911441e4bcb1a94358dd5b5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mikelul

Reputation: 0
• Posts: 9
• Joined: 06-November 19

# C - Count Prime Numbers in Array - Beginner

Posted 06 November 2019 - 01:18 PM

I want to count how many prime numbers are in my array. But i am getting unrealistic numbers.

Could anyone help me with this?

I think I dont got the return or/and the input of the array right.
But I am not able to figure it out on my own.
Any help would be more than appreciated.

```#include <stdio.h>

int countPrimes(int element_list[], int elements)
{
int primeIndex=0;
int i, n;
for (n=0; n < elements; n++)//I am taking every element from the array
{
for (i=2;i <= n ;i++) //I check vor every number between 2 and the array number if modul = 0 or not.
{
if (element_list[n] % i != 0)
{

primeIndex++; //if it is a PrimeNumber, I add 1 to the Index
}
}
}
return primeIndex; //I return the calculated PrimeIndex back to the main function
}

int main ()
{
int test[] = {1,2,3,11,12,7}; //My Input Array
int  test_length = sizeof(test) / sizeof(test[0]); //How many elements are in the array
printf("there are %d Primenumbers in the Array.\n", countPrimes(test,test_length)); //print the counted Prime numbers

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: C - Count Prime Numbers in Array - Beginner

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 2397
• Posts: 4,530
• Joined: 30-May 10

## Re: C - Count Prime Numbers in Array - Beginner

Posted 06 November 2019 - 02:29 PM

You should move the inner loop of your function into another function.

```int countPrimes(int element_list[], int elements)
{
int primeIndex=0;
int n;
for (n=0; n < elements; n++)//I am taking every element from the array
{
if ( isPrime(element_list[n]) )
{
primeIndex++;
}
}
return primeIndex; //I return the calculated PrimeIndex back to the main function
}

```

Now you can focus on writing an isPrime() function which does exactly one thing - and that's check whether a number is prime or not.

### #3 mikelul

Reputation: 0
• Posts: 9
• Joined: 06-November 19

## Re: C - Count Prime Numbers in Array - Beginner

Posted 06 November 2019 - 03:12 PM

Thanks 10000 Times.
That was an very effective Tipp.
Its heaps easier to check the code properly

Still got a few Question.

My compiler doesnt run the
i<=sqrt(n), even with <math.h> libray.

Is there a more nicer way to take out number 0 and 1 from the primes?

I wrote it now like this, and it works

```#include <stdio.h>

int isPrime(int n)
{
int i,flag,isprime;
//I have to check n/2 numbers, to identify if it ist a prime number
for(i = 2; i <= n/2; ++i)
{
//modulu from all numbers smaler than 1/2*the number
if(n%i == 0)
{
flag = 1;
break;
}
}
// take out 1 and 0 from the prime numbers
if (n == 1 || n== 0)
{
isprime=5;
}
else
{
if (flag == 0)
isprime=1;
else
isprime=0;
}
return isprime;
}

int countPrimes(int element_list[], int elements)
{
int primeIndex=0;
int n;
for (n=0; n < elements; n++)//I am taking every element from the array
{
if ( isPrime(element_list[n]) )
{
primeIndex++;
}
}
return primeIndex; //I return the calculated PrimeIndex back to the main function
}

int main ()
{
int test[] = {1,2,3,11,12,7}; //My Input Array
int  test_length = sizeof(test) / sizeof(test[0]); //How many elements are in the array
printf("there are %d Primenumbers in the Array.\n", countPrimes(test,test_length)); //print the counted Prime numbers

return 0;
}

```

### #4 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• Joined: 05-May 12

## Re: C - Count Prime Numbers in Array - Beginner

Posted 06 November 2019 - 04:03 PM

mikelul, on 06 November 2019 - 05:12 PM, said:

My compiler doesnt run the
i<=sqrt(n), even with <math.h> libray.

What do you mean by "compiler doesn't run"? A compiler compiles code. It doesn't run your code. You don't have to download anything, but you may have to explicitly tell the compiler to link in the math library.

"math.h" is not a library. It is just a header file that describes the contents of the library.

What error and/or warnings are you getting? Please copy and paste the errors verbatim. Don't paraphrase or summarize as there will be important clues in the messages. Please copy and paste the text, don't use a screenshot.

### #5 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• Joined: 05-May 12

## Re: C - Count Prime Numbers in Array - Beginner

Posted 06 November 2019 - 04:14 PM

mikelul, on 06 November 2019 - 05:12 PM, said:

Is there a more nicer way to take out number 0 and 1 from the primes?

Yes there are. The first step is figure out whether you want to use flag or isPrime. Currently you are only using isPrime to just return a value.

Some of it depends on whether your teacher is an old-school C programmer who believes that there should only ever be a single entry point and single exit point for a C function or not. It also depends on whether negative values for n are allowed or not.

If your teacher is not an only-one-way-in-one-way-out, then do the check for n being less than 2 and bail out early. If you are forced to have only one exit point, then initialize flag depending on n being less than 2 or not.

Also your last inner if's can be cleaned up/collapsed. Whenever you find yourself in a situation where you have:
```if (someCondition)
variable = true;
else
variable = false;

```

This should be simply replaced with:
```variable = someCondition;

```

### #6 mikelul

Reputation: 0
• Posts: 9
• Joined: 06-November 19

## Re: C - Count Prime Numbers in Array - Beginner

Posted 06 November 2019 - 09:34 PM

Skydiver, on 06 November 2019 - 04:03 PM, said:

mikelul, on 06 November 2019 - 05:12 PM, said:

My compiler doesnt run the
i<=sqrt(n), even with <math.h> libray.

What do you mean by "compiler doesn't run"? A compiler compiles code. It doesn't run your code. You don't have to download anything, but you may have to explicitly tell the compiler to link in the math library.

"math.h" is not a library. It is just a header file that describes the contents of the library.

What error and/or warnings are you getting? Please copy and paste the errors verbatim. Don't paraphrase or summarize as there will be important clues in the messages. Please copy and paste the text, don't use a screenshot.

I wrote this code and tried to compile it.

```#include <stdio.h>
#include <math.h>

int main()
{
float a;
printf("Insert a Number\n");
scanf("%f", &a);

double b = sqrt(a);
printf("the Root of %.2lf = %.2lf",a,B)/>/>/>;
}

```

I did clang -v. This came up:

clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.4.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
clang: error: no such file or directory: 'test'

When i complie it online via:
GDB Online
it works just fine.

So what do I need on the VM ?

### #7 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• Joined: 05-May 12

## Re: C - Count Prime Numbers in Array - Beginner

Posted 07 November 2019 - 06:10 AM

How are you currently compiling it on your VM? What errors and warnings are you getting when you compile in in the VM? As I said, please copy and paste those errors and warnings. What you did above was copy and paste the results of getting the version information.