Hi, I need to figure out this programming problem. Program isprime.cpp: a. Write
ID: 3757971 • Letter: H
Question
Hi, I need to figure out this programming problem.
Program isprime.cpp: a. Write a function read_range() which reads in the minimum and maximum values. The function has two parameters listed in the following order: i. the minimum value of the range; ii. the maximum value of the range. Both parameters should have integer type. Both parameters should be passed by reference. The function does not return any value, but it modifies the two parameters. The function prompts for the minimum and maximum values. If the minimum or maximum values are less than 2, then the program prints an error message and prompts again for the two values. If the minimum value is greater than the maximum value, then the program prints an error message and prompts again for the two values. The program prints error messages and prompts for the minimum and maximum values until it gets two values greater than or equal to 2 and such that the minimum is less than or equal to the maximum. b. Write a function is_prime() which determines if a number is prime. The function has one integer parameter. The function returns true if the input parameter is prime and false otherwise. A number a is prime if and only if (a mod b ? 0) for b = 2, 3, ..., (a ? 1).
Do not modify anything in the main function in the template (below).
TEMPLATE:
#include <iostream>
#include <cmath>
using namespace std;
// FUNCTION PROTOTYPE FOR read_range
// FUNCTION PROTOTYPE FOR is_prime
// 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 j = imin; j <= imax; j++) {
if (is_prime(j))
{
cout << " " << j;
}
}
cout << endl;
return 0;
}
// DEFINE FUNCTION read_range() HERE:
// DEFINE FUNCTION is_prime() HERE:
The output should exactly match the following sample solution:
Thanks for the help.
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
// FUNCTION PROTOTYPE FOR read_range
void read_range(int *,int *);
// FUNCTION PROTOTYPE FOR is_prime
int is_prime(int);
// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
int imin=0, imax=0;
// Read in range
read_range(&imin, &imax); // here I have passed parameters as a reference.
// Print prime numbers
cout << "Primes:";
for (int j = imin; j <= imax; j++) {
if (is_prime(j))
{
cout << " " << j;
}
}
cout << endl;
return 0;
}
// DEFINE FUNCTION read_range() HERE:
void read_range(int *p,int *q)
{
cout<<"Enter Minimum and Maximum Range"<<endl;
cin>>*p>>*q;
while(*p<2 || *q<2 || *p>*q )
{
if(*p<2 || *q< 2)
{
cout<<"Value can not be less than 2"<<endl;
cout<<"Enter Again"<<endl;
cin>>*p>>*q;
}
if(*p>*q)
{
cout<<"minimum value can not be greater than maximum"<<endl;
cout<<"Enter Again"<<endl;
cin>>*p>>*q;
}}}
// DEFINE FUNCTION is_prime() HERE:
int is_prime(int p)
{
int i,count=0;
for(i=2;i<p;i++)
{
if(p%i==0)
{
return 0;
break;
}
else
{
count++;
}
}
if(count==(p-2))
return 1;
else
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.