dear people i have an assignment due in one hour and its not working.

i have to write a program with four functions that finds out and print the twin prime numbers. i followed every step in the assignment and its not giving any error but i dont have any output so far even in one of my functions i have cout too.
the last function to print the twin prims is not done yet here is my code

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void makeSieve ( bool sieve[], const int sieveSize );
void workSieve ( bool sieve[], const int sieveSize );
void findBoundaries (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 [SIZE];
 int low, high;
 makeSieve (sieve, SIZE);
 workSieve (sieve, SIZE);
 findBoundaries (low, high, SIZE );
 showTwins (sieve, low, high );
 return 0;
}

 
void makeSieve ( bool sieve[], const int sieveSize )
{
 for ( int i=2; i < sieveSize; i++ )
 {
  sieve[i]= true;
 }
 sieve[1]=false;
 sieve[0]=false;
}
//==============================================================
void workSieve ( bool sieve[], const int sieveSize )
{
 for ( int A=2; A<=sqrt(120000); A++)
 {
  for (int B=3; B <= 120000; B++)
  {
   if(sieve[B]%2==0)sieve[B]=false;
  }
  if (sieve[A]%3==0)sieve[A]=false;
 }
}
//=============================================================
void findBoundaries (int &lower, int &upper, const int sieveSize )
{
 do
 {
  while(upper>0 && upper<=120000)
  {
   cout << "Please Enter the upper boundary (between 1 and 120000): " << endl;
   cin >> upper;
  }
  while (lower>0 && lower<=120000)
  {
   cout << "Please enter the lower boundary (between 1 and 120000): " << endl;
   cin >> lower;
  }
 }while (lower>upper);
 return;
}
void showTwins (const bool sieve[], const int lower, const int upper )
{
}

>>its not giving any error

Wow. What compiler are you using? I'm surprised it compiles at all. You use a variable in workSieve that isn't declared. Remember that b isn't the same as B in C/C++.

My friend, I think you are having trouble how functions works :confused:
I am not an expert in C++ but I can see a couple things wrong
with how you make use of functions.

void makeSieve ( bool sieve[], const int sieveSize )
{
 for ( int i=2; i < sieveSize; i++ )
 {
  sieve[i]= true;
 }
 sieve[1]=false;
 sieve[0]=false;
}

What is this function doing?. A lot of nothingness. The sieve[] is a copy
of the one in main but that one is never changed.

void workSieve ( bool sieve[], const int sieveSize )
{
 for ( int A=2; A<=sqrt(120000); A++)
 {
  for (int B=3; B <= 120000; B++)
  {
   if(sieve[b]%2==0)sieve[b]=false;
  }
  if (sieve[A]%3==0)sieve[A]=false;
 }
}

This one the same that above.

void showTwins (const bool sieve[], const int lower, const int upper )
{
}

This one above doesn't even do any work at all.

Luckly, this one will display something in screen. Please enter input.

void findBoundaries (int &lower, int &upper, const int sieveSize )
{
 do
 {
  while(upper>0 && upper<=120000)
  {
   cout << "Please Enter the upper boundary (between 1 and 120000): " << endl;
   cin >> upper;
  }
  while (lower>0 && lower<=120000)
  {
   cout << "Please enter the lower boundary (between 1 and 120000): " << endl;
   cin >> lower;
  }
 }while (lower>upper);
 return;
}

Pretty much these functions do the same that this:

#include <stdio.h>

int main(void)
{
     return 0;
}

>What is this function doing?. A lot of nothingness. The sieve[] is a copy of the one in main but that one is never
>changed.
Are you so sure about that? Try this and see.

#include <iostream>
using namespace std;

void fillArray(int myArray[], int arraySize) {
    
    for (int i=0; i < arraySize; i++) {
        
        myArray[i] = 5;
    }
}

int main() {
    const int arraySize = 5;
    int array[arraySize];
    
    fillArray(array, arraySize);
    cout << "Contents of array:\n";
    
    for (int i=0; i<arraySize; i++) {
        cout << array[i] << endl;
    }
    
    return 0;
}

When you pass an array, you in fact pass the address of the first element -- thus eliminating the need for a pointer.

Are you so sure about that? Try this and see.

When you pass an array, you in fact pass the address of the first element -- thus eliminating the need for a pointer.

From time to time I need to put my foot in my mouth so I can
taste the leather of my boot. :)

Additionally the variables low and high are never initialized. Thus when they are tested in your findBoundaries function you have the possibility that they will be outside of the test range and thus the outputs will never be displayed. To correct this I would recommend initializing them to a value that will pass the test in the while loops and allow the while loops to run, or use do-while loops in place of the while loops.
Hope that made sense...

OK OK OK guys i finally figured it out. i could not focus as i had only one hour left to submit this program. but i called the teacher and asked for more time from him so here is the totaly working code that i wrote to find out the prime numbers of 1 to 120,000 and print out its twin prime numbers of a number range that user want (between 1 to 120,000)

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void makeSieve ( bool sieve[], const int sieveSize);
void workSieve ( bool sieve[], const int sieveSize);
void findBoundaries ( int &lower, int &upper, const int sieveSize);
void showTwins ( bool sieve[], int lower, int upper);

int main()
{
 const int SIZE = 120001;
 bool sieve [SIZE];
 int low, high;
 makeSieve (sieve, SIZE);
 workSieve (sieve, SIZE);
 findBoundaries (low, high, SIZE );
 showTwins(sieve, low, high );
 return 0;
}

//Function Name                 :makeSieve
//Partners                      :NONE
//Description                   :This function sets all the array elements value to true.
void makeSieve ( bool sieve[], const int sieveSize )
{
 for ( int i=2; i < sieveSize; i++ )
 {
  sieve[i]= true;
 }
 sieve[1]=false;
 sieve[0]=false;
}

//Function Name                 :workSieve
//Partners                      :NONE
//Description                   :This function find all Non prime numbers between 1 to 120000 and sets their value to false

void workSieve ( bool sieve[], const int sieveSize )
{
 for ( int A=2; A <= sqrt(120000); A++)
 {
  for (int B=A+1; B <= sieveSize; B++)
  {
   if(B%A == 0)
    sieve[B]=false;
  }
 }
}
 
//Function Name                 :findboundaries
//Partners                      :NONE
//Description                   :This function prompts user for lower and upper number for the next function

void findBoundaries (int &lower, int &upper, const int sieveSize )
{
 
 do
 { 
  cout << "Please enter the upper boundary (between 1 and 120000): ";
  cin >> upper;
 } while( upper <=0 || upper > 120000 );

 do
 {
  cout << "Please enter the lower boundary (between 1 and 120000): ";
  cin >> lower;
 } while( lower <=0 || lower >120000);

while (lower >upper)
{
cout << "\nYour upper boundary cannot be smaller than your lower boundary" << endl;
 do
 { 
  cout << "Please enter the upper boundary (between 1 and 120000): ";
  cin >> upper;
 } while( upper <=0 || upper > 120000 );

 do
 {
  cout << "Please enter the lower boundary (between 1 and 120000): ";
  cin >> lower;
 } while( lower <=0 || lower >120000);

}
 return;
}
 
//Function Name                 :showTwin
//Partners                      :NONE
//Description                   :This function displays the result

void showTwins(bool sieve[], int lower, int upper )
{
int total=0;
cout << "\nHere are all of the twin prime pairs in the range " << lower << " to " << upper << ", one pair per line:\n" << endl;
 for(int i = lower; i <= upper; i++)
 {
  if(sieve[i] && sieve[i+2])
  {
   cout << i << " and " << i + 2 << endl;
  total++;
  }
 }
cout << "\nThere were " << total  << " twin prime pairs displayed between " << lower << " and " << upper << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.