6 Replies - 433 Views - Last Post: 07 November 2019 - 06:10 AM Rate Topic: -----

#1 mikelul   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2397
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 mikelul   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Do I need to download something?

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 :D

#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;
}



Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

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

Re: C - Count Prime Numbers in Array - Beginner

Posted 06 November 2019 - 04:03 PM

View Postmikelul, on 06 November 2019 - 05:12 PM, said:

My compiler doesnt run the
i<=sqrt(n), even with <math.h> libray.
Do I need to download something?

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.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

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

Re: C - Count Prime Numbers in Array - Beginner

Posted 06 November 2019 - 04:14 PM

View Postmikelul, 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;


Was This Post Helpful? 1
  • +
  • -

#6 mikelul   User is offline

  • New D.I.C Head

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

Re: C - Count Prime Numbers in Array - Beginner

Posted 06 November 2019 - 09:34 PM

View PostSkydiver, on 06 November 2019 - 04:03 PM, said:

View Postmikelul, on 06 November 2019 - 05:12 PM, said:

My compiler doesnt run the
i<=sqrt(n), even with <math.h> libray.
Do I need to download something?

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
Thread model: posix
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 ?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1