البرنامج عقدني و لف راسي
ابي احد يصححه لي و يشوف ايش الخطأ فيه
كود البرنامج:
#include<iostream>
#include<cmath> //for square root function
#include<iomanip> //included in case you use formatting for output
using namespace std;
void makeSieve ( bool sieve[], const int sieveSize);
void workSieve ( bool sieve[], const int sieveSize);
void findBoundries ( int &lower, int &upper, const int sieveSize );
void showTwins (const bool sieve[], const int lower, const int upper);
int main()
{
const int SIZE = 120001;
bool sieve ;
int low, high;
makeSieve ( sieve, SIZE );
workSieve ( sieve, SIZE );
findBoundries ( low, high, SIZE );
showTwins ( sieve, low, high );
return 0;
}
////////////////////////////////////////////////////////////////////////
void makeSieve ( bool sieve[], const int sieveSize)
{
sieve [0]=0;
sieve [1]=0;
for ( int x=2; x<sieveSize; x++)
{
sieve[x]=1;
}
}
//////////////////////////////////////////////////////////////////////////////////////
void workSieve ( bool sieve [], const int sieveSize)
{
for (int y=2; y <= sqrt ( sieveSize ); y++ )
{
if (sieve [y]==1)
{
for(int z=y*2; z<sieveSize ;z=z+B)
{
sieve[z]=0;
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////
void findBoundries ( int &lower, int &upper, const int sieveSize )
{
cout<<"Please enter an upper boundary and a lower boundary and I will print all of the\nprime twin pairs between those boundaries.\n"<<endl;
cout<<"Please enter the upper boundary (between 1 and 120000): ";
cin>>upper;
while (upper<=0 ||upper>=120001)
{
cout<<"Please enter the upper boundary (between 1 and 120000): ";
cin>>upper;
}
cout<<"Please enter the lower boundary (between 1 and 120000): ";
cin>>lower;
while (lower<=0 ||lower>=120001)
{
cout<<"Please enter the lower boundary (between 1 and 120000): ";
cin>>lower;
}
if (lower>upper)
{
do
{
cout<<"\nYour upper boundary cannot be smaller than your lower boundary"<<endl;
cout<<"Please enter the upper boundary (between 1 and 120000): ";
cin>>upper;
while (upper<=0 ||upper>=120001)
{
cout<<"Please enter the upper boundary (between 1 and 120000): ";
cin>>upper;
}
cout<<"Please enter the lower boundary (between 1 and 120000): ";
cin>>lower;
while (lower<=0 ||lower>=120001)
{
cout<<"Please enter the lower boundary (between 1 and 120000): ";
cin>>lower;
}
}
while(lower>upper);
}
cout<<"\nHere are all of the twin prime pairs in the range "<<lower<<" to "<<upper<<", one pair per line:\n"<<endl;
}
//////////////////////////////////////////////////////////////////////
void showTwins (const bool sieve[], const int lower, const int upper)
{
int count =0;
for ( int c=lower; c<=upper-2 ; c++ )
{
if ( sieve[c]==1 && sieve[c+2]==1 )
{
cout<<c<<" and "<<c+2<<endl;
count++;
}
}
cout<<"\n\nThere were "<<count<<" twin prime pairs displayed between "<<lower<<" and "<<upper<<endl;
}
///////////////////////////////////////////////////////////////////////////////////