
November 25th, 2012, 04:17 AM
#1
[HELP, C++] array with prime numbers
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){
}

November 25th, 2012, 05:25 AM
#2
Re: [HELP, C++] array with prime numbers
Originally Posted by Fromar123
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...)
How would you solve this problem if you didn't have a computer? What are the exact steps you'd take?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it.  P. D. Ouspensky

November 25th, 2012, 05:33 AM
#3
Re: [HELP, C++] array with prime numbers
well, i already have a IsPrime function that checks if numbers are prime (but that is for input),... so i'd guess i have to use that to get those primes, but the main problem is with the array part... should i set the length of array when i declare the function like this: int somefunc (int array[10], int somenum){...}
or later on in the function?
also i have problems with filling those 10 spots in the array, and to generate 10 prime numbers, i know how to check for IsPrime if i use cin, but im a little lost when it sohuld automatically get 10 in a certain range...

November 25th, 2012, 06:59 AM
#4
Re: [HELP, C++] array with prime numbers
It shouldn't be a problem to modify your IsPrime() function so that it takes the number it shall check for primeness as an int parameter rather than from cin. Then you call that from somefunc() in a loop to check successive numbers, the first one of course being the one you got passen in as the somenum parameter.
As I understand the assignment, you can assume the count of exactly 10 primes to find and store into the array to be part of the specification, so you can simply assume you got passed an array of the approprite size and hardcode the prime count of 10. If the caller passes an array of different size, then that's their problem. If the array is too long, it simply won't get filled completely, and if it's too short...
I'd also suggest to consider changing the declaration of the array parameter in the somefunc() function header from int array[10] to int array[] to reflect the fact that the array, as passed into the function, does not carry any size information. The 10 in the definition may give a false sense of safety in that the compiler somehow takes care that your function or the calling code passes a correctly sized array, which definitely is not the case. Probably better yet is to declare the parameter as int *array to reflect the fact that plain arrays in C++ are always passed to funtions as pointers. All the three parameter declarations are effectively equivalent, in particular you can syntactically handle the parameter as an array inside somefunc() and pass an ordinary array from the outside in all three cases.
On the side note, array isnt't really a good choice as a parameter or variable name. This is generally true for some reasons I'm not going to discuss here for brevity, but in particular, starting with C++11, standard library defines an std::array template with which your variable name may conflict.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

November 25th, 2012, 08:07 AM
#5
Re: [HELP, C++] array with prime numbers
Originally Posted by Fromar123
well, i already have a IsPrime function that checks if numbers are prime (but that is for input),... so i'd guess i have to use that to get those primes
This is why you should separate I/O from your algorithms. If you had implemented the IsPrime function as
Code:
int GetNumberFromUser();
bool IsPrime(int n);
int main()
{
int n = GetNumberFromUser();
if (IsPrime(n))
// etc
}
Then you could now reuse the IsPrime function as is.
Originally Posted by Fromar123
also i have problems with filling those 10 spots in the array, and to generate 10 prime numbers, i know how to check for IsPrime if i use cin, but im a little lost when it sohuld automatically get 10 in a certain range...
You could check whether each subsequent number is a prime or not and stop when you have found 10 prime numbers.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it.  P. D. Ouspensky
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
