addittion in an array

this is my first time with cplusplus.com

i need help to write a program to ADD ALL THE PRIME NUMBERS IN AN ARRAY
could some one provide a suitable program using #include<iostream.h>
and a program which probably works in dosbox
I can help you, but I won't do this program.

You won't learn anything if you just ask and got what you need.

Find an algorithm that found prime number in order.
Then put them one by one in an array.
Last edited on
@Mark2 i am new to c++ which is probably why i am not very clear at it...so i asked for help....i tried my self but the program didnt work...thats why i asked for help...
Since you said you tried, and of course I do believe you, why don't you post the code you wrote? Sometimes, seeing the correction of your mistakes is more useful and helpful than just reading someone else's code.
I've come up with something that works.

Because I don't want to give you all the work for free. Here's all the variables I used:

1
2
3
int maximum, counter;
bool canBeDivided(false);
vector<int> primeNumbers;


I can help you with the rest. Try something and paste the code here. I'll take a look at it. :)
Hi,

With a prime number algorithm, try this simple idea:

- Take a list of all the numbers up to limit,
- Starting at 2, remove all multiples of it,
- look at the next number in list and remove all multiples of it,
edit
- repeat the previous step when you have reached sqrt(limit), stop.

The list of numbers is quickly reduced.

You can have an array of bool, and use the subscript of the array as the number, set it to true if it is prime. If you are not comfortable using bool, you can use an array of int, set the values to or 1 to achieve the same thing. For example, if n is prime, then PrimeNumbers[n] = true; or PrimeNumbers[n] = 1;

There are lots of ways this can be improved, but we need something simple but effective at this stage. The method of seeing if every number has divisors is very inefficient, and suitable for rather small numbers only.

Google and wiki are your best friends. Have a look at the wiki page for prime numbers, near the bottom there is Euler's algorithm.

Notice the algorithm I have given is pseudo code, that is a powerful way to organise your thoughts and make it easier to write code. You can start out very general, then go back and refine and put in more detail, until you are happy to convert the comments to code. The comments can remain in the code, they could serve as documentation. Pseudo code also helps identify what code should be in a function or in a loop.

Hope all goes well :+)
Last edited on
Topic archived. No new replies allowed.