0

Hello I'm trying to create a program that reads a number(which is the amount of pairs) then you input the pairs and then for each pair it prints the prime numbers between the 2 numbers of the pair.Here's the code:

#include <iostream>
using namespace std;

class Prime {
	int x,y;
	public:
	void count(int,int);
};


  void Prime::count(int k,int l)
   {
	   int f=k%2;
	   int g=k%3;
	   int h=k%5;
	   do
	   {
		   if  (f==0)
           break;
           else if (g==0)
           break;
           else if (h==0)
           cout  <<  k;
           k++;
	   } while (k<=l);
}	 
int main(void)
{
	int * primes;
	int n,i;
	int a,b;
	cout << "Give  the amount of numbers you wish to try:"<< endl;
    cin  >> n;
    primes = new int[n];
    for (i=0;i<=(n-1);i++)
    {
    cout << "Give the 2 numbers:" << endl;
    cin >> a >> b;
    Prime primes[i];
    primes[i].count(a,b);
}
}

unfortunately after inputting the pairs the program stops.Any advice?Thanks in advance!

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Zjarek
0

Your primality tester is flawed, for example 49 according to this primitive test is prime (but for every number <49 it is correct). Also it will not finish if k>0, because only manipulation you do with k is incrementation in line 24, and in the following line you test it if it is less then 2.
PS. Good indention for Prime::count:

void Prime::count(int k,int l)
   {
	   int f=k%2;
	   int g=k%3;
	   int h=k%5;
	   do
	   {
	   if  (f==0)
                break;
           else if (g==0)
                break;
           else if (h==0)
                cout  <<  k;
           k++;
	   } while (k<=l);
}

Edited by Zjarek: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.