Posted: Thu Feb 11, 2010 10:48 pm Post subject: problem with prime numbers
code:
#include <iostream>
using namespace std;
int main()
{
int max=999; // set the size of the array
bool prime [max];
int num[max];
for (int i=-1;i<max;i++)
{
num[i]=i+2;//seting the numbers in the arry
}
for (int j=-1;j<max;j++)
{
prime[j]=true;//seting the numbers in the arry
}
prime[0]=false;
/////////////////////////////////////////////
//what this part is supose to do is go througe and see witch numbers are considered prime and removing
//all multiples of that number but it dose not work can any one tell me why
for (int k=0;k<max;k++)
{
if (prime[k])/
cout <<num[k];
for (int i=0;i<max;i=i+num[k])
{
//cout<<(num[i]%num[k]==0);
prime[i]=false;
}
//////////////////////////////////////////////////
}
/* for (int i=-1;i<max;i++) // for each element in the array
{
if (prime[i])
cout<<num[i]<<endl;
}
*/
return 0;
}
This is probably something obvious but i dont understand whats wrong with it can some one explain it for me.
thx in advance
Sponsor Sponsor
DemonWasp
Posted: Fri Feb 12, 2010 4:03 am Post subject: RE:problem with prime numbers
The if (prime[k]) line is missing its block ( curly braces - { and } ) and therefore only affects the line after it, which is an output line. Based on the indenting, you want a block around the contained for(){} loop.
Barbarrosa
Posted: Fri Mar 05, 2010 10:20 pm Post subject: Re: problem with prime numbers
Based on my current understanding, this block of code alters the area one space before each array. I don't believe that's what you intended to do, so you should probably start those loops at or after zero.
c++:
int main() { int max=999; // set the size of the array bool prime [max];
int num[max];
for(int i=-1;i<max;i++) {
num[i]=i+2;//seting the numbers in the arry } for(int j=-1;j<max;j++) {
prime[j]=true;//seting the numbers in the arry }
prime[0]=false;