[c++] array with prime numbers

11/25/2012 11:08 Hikarim#1
Hi, i'd need some help with a function...
well, i need to write a function with an array parameter and an int parameter.

that array has to be filled with first 10 prime numbers that are exact or higher than the int parameter...
and then i need an average value of those 10 prime numbers...

The problem is im not really sure how i should do the part to fill the array with prime numbers that are higher than that int?? So any code samples that does that would be really nice... (the average part shouldnt do much problems i think, so i only need for that array fill...)

Code:
int avgprimearray (int higharray[], int somenumber){

}
11/25/2012 15:59 snow#2
For the prime numbers array:
Do some for- / while-loop and check every number above your passed "somenumber" if it's a prime number. If it's a prime number, add it to the array.

This is how it could look (not sure if there are any syntax errors)

PHP Code:
for (int i somenumber500i++) {

 if (
primenumber) { // insert your prime number check here
  
  
*higharray++ = i;
 }

If you want to do this for the first 10 prime numbers, take a look at a while-loop where you count up an integer every time you've found a prime number.

To get the average:

int average = 0;
int counter = 0;
while (*higharray++ != null) {

average+= *higharray;
++counter;
}

average = (int)(average / counter);

-> just add every prime number you've found and divide it by the total amount of found prime numbers. If there are only 10 prime numbers, you don't need the counter variable, just divide by 10. :)
11/25/2012 16:44 Hikarim#3
Solved it alrdy, but thanks anyway :)