I have to create a program (using user defined functions) that reads in a user d
ID: 3653385 • Letter: I
Question
I have to create a program (using user defined functions) that reads in a user defined range, then prints all the prime numbers between the minimum and maximum. There are also two error messages, one if the minimum is greater than the range; the other if either input is less than two. My code is posted below. I'm having trouble with it and have tried numerous things. Please put comments in your answer so I can understand what's going on /* File: Synopsis: This program reads in a minimum and maximum integer greater than 1 and returns all primes between the minimum and maximum. */ #include #include using namespace std; // FUNCTION PROTOTYPE FOR read_range void read_range(int & imin, int & imax); // FUNCTION PROTOTYPE FOR is_prime int is_prime(int num); // DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY int main() { int imin(0), imax(0); // Read in range read_range(imin, imax); // Print prime numbers cout << "Primes:"; for (int k = imin; k <= imax; k++) { if (is_prime(k)) { cout << " " << k; } } cout << endl; return 0; } // DEFINE FUNCTION read_range() HERE: void read_range( int & imin, int & imax) { if(int & imin < 2 || int & imax < 2) { cout << "Error. Minimum and maximum value must be at least 2."; cout << "Enter minimum and maximum: "; } if(int & imin > int & imax) { cout << "Error. Minimum must be less than maximum."; cout << "Enter minimum and maximum: "; } } // DEFINE FUNCTION is_prime() HERE: int is_prime(int num) { for(int k = int & imin; k <= int & imax; k++) { for(int j=k-1;j >= 2; j++) { if((k%j)==0) { is_prime = false; } else { is_prime=true; } } if(is_prime==false) { is_prime=true; } } }Explanation / Answer
please rate - thanks
/* File: Synopsis: This program reads in a minimum and maximum integer greater than 1 and returns all primes between the minimum and maximum. */
#include <iostream>
using namespace std;
// FUNCTION PROTOTYPE FOR read_range
void read_range(int & imin, int & imax);
// FUNCTION PROTOTYPE FOR is_prime
int is_prime(int num); // DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{ int imin(0), imax(0);
// Read in range
read_range(imin, imax);
// Print prime numbers
cout << "Primes:";
for (int k = imin; k <= imax; k++)
{ if (is_prime(k))
{ cout << " " << k;
}
}
cout << endl;
return 0;
}
// DEFINE FUNCTION read_range() HERE:
void read_range( int & imin, int & imax)
{int error;
do
{ error=0;
cout<<"enter minimum: ";
cin>>imin;
cout<<"enter maximum: ";
cin>>imax;
if(imin < 2 ||imax < 2)
{ cout << "Error. Minimum and maximum value must be at least 2. ";
error=1;
}
if(imin > imax)
{ cout << "Error. Minimum must be less than maximum. ";
error=1;
}
}while(error==1);
}
// DEFINE FUNCTION is_prime() HERE:
int is_prime(int num)
{ for(int j=2;j<num; j++)
{ if((num%j)==0)
return false;
}
return true;
}