Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following skeleton program with a function prototype and its defini

ID: 3727701 • Letter: C

Question

Consider the following skeleton program with a function prototype and its definition:

#include <iostream>
#include <cmath>

using namespace std;

// function is_prime(k) returns true if k is prime and false otherwise.
bool is_prime(int k);


int main()
{
return 0;
}

// function implementation
// is_prime(k) returns true if k is prime and false otherwise.
bool is_prime(int k)
{
if (k < 2) return false;
for (int i = 2; i <= static_cast<int> (sqrt(k)); i++)
if (k % i == 0)
return false;
return true;
}

Write a complete C++ program that finds and stores all primes numbers strictly less than 1000 in a vector and compiles a report in which it displays the number of primes found in the following ranges 0 to 250, 250 to 500, 500 to 750, and 750 to 1000. Make sure that you test your program and validate the results of your program. Your program must use the function _is_prime(k) given below and C++ vector class.

(I've posted this question before but someone answered it with flags and my professor didnt approve of that. so can someone answer this WITHOUT the use of flags in the most simplest form possible, c++ language)

thank you.

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

// function is_prime(k) returns true if k is prime and false otherwise.
bool is_prime(int k);


int main()
{
vector<int> primes;
for(int i = 1; i <= 1000; i++)
{
if(is_prime(i))
primes.push_back(i);
}
  
int count250 = 0, count500 = 0, count750 = 0 , count1000 = 0;
  
for(int i = 0; i < primes.size(); i++)
{
if(primes[i] <= 250)
count250++;
else if(primes[i] <= 500)
count500++;
else if(primes[i] <= 750)
count750++;
else
count1000++;
}
  
cout << "The following is the statistics of prime numbers" << endl;
cout << "Primes in the range 0-250: " << count250 << endl;
cout << "Primes in the range 251-500: " << count500 << endl;
cout << "Primes in the range 501-750: " << count750 << endl;
cout << "Primes in the range 751-1000: " << count1000 << endl;

return 0;
}

// function implementation
// is_prime(k) returns true if k is prime and false otherwise.
bool is_prime(int k)
{
if (k < 2) return false;
for (int i = 2; i <= static_cast<int> (sqrt(k)); i++)
if (k % i == 0)
return false;
return true;
}
output
------

The following is the statistics of prime numbers
Primes in the range 0-250: 53
Primes in the range 251-500: 42
Primes in the range 501-750: 37
Primes in the range 751-1000: 36