The general policy here is that we aren't going to just do your homework from scratch, YOU have to make an attempt first, and then we can help you from there. (But I'll give a template for you to expand)
If you don't understand C++ at all, I would suggest starting with the tutorials:
http://www.cplusplus.com/doc/tutorial/
Seems like you might want to read up on how to make and manipulate arrays, as well:
http://www.cplusplus.com/doc/tutorial/arrays/
← in your pseudocode means assignment, so p ← 2 means that a variable called p is being assigned the value 2.
in C++, that would be written as
1 2
|
int p; // variable declaration
p = 2; // variable assignment
|
"for p ← 2 to n" means you want to write a for loop that goes from p = 2 to p = n. You can read about for loops here:
http://www.cplusplus.com/doc/tutorial/control/ (loops section)
You're going to need 2x arrays -- the A array and the L array. The A array seems to be every number from 2 to N, inclusive.
Here's something to get you started
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <cmath> // std::sqrt(), ex std::sqrt(16) == 4.0
int main()
{
const int n = 50; // change this to what you desire
int A[n+1]; // +1 needed because the upper bound is inclusive for your problem, it seems.
int L[n+1]; // not all elements of L will be assigned in the end, but this gives a constant upper-bound array size
for (int p = 2; p <= n; p++)
{
A[p] = p;
}
}
|
Another hint:
while j ≤ n do
A[j ] ← 0 //mark element as eliminated
J ← j+p
|
would translate into
1 2 3 4 5
|
while (j <= n)
{
A[j] = 0; // mark element as eliminated
j = j + p;
}
|