CODE IN C AND PLEASE MAKE THE CODE COPY AND PASTABLE Work wi

CODE IN C++ AND PLEASE MAKE THE CODE COPY AND PASTABLE

Work with vectors and functions Write two functions. The first function will compute all the prime numbers less than a user-entered value, n. Implement this using a nested loop. The outer loop will iterate from 2 to n - 1. The inner loop will then iterate through the values from 2 to floor( squareroot (n)), the largest maximum factor of n, and determines when a value is a prime number using the modulus operator. If the value is prime, add that to a vector of prime values. At the end, write the vector of primes to the screen by implementing a second function whose only job is to display to the screen. Your assignment must Have two functions: one for calculating the primes and one for displaying Use the modulus operator Include a nested loop Use the vector class For maximum efficiency, use the min function to determine the maximum value the inner for loop will iterate to. The min function returns the lowest of two arguments. Ex: min(10, 2) = 2 vector primes(int num); void dispPrimes(vector v); Sample output This program prompts the user for a number. It then grabs all the primes below that number Enter a number: 24 2 3 5 7 11 13 17 19 23

Solution

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
using namespace std;

//declaration of two functions
vector<int> primes(int num);
void dispPrimes(vector<int> v);

int main()
{
int value;
cout<<\"This program prompt the user for a number. It then grab all the primes below that number\";
cout << \"\ \ Enter a number:- \";
cin>>value;
vector<int> a=primes(value);
dispPrimes(a);
}
//definition of \'primes\' function usinf nested loops
vector<int> primes(int n){
   vector<int> myvector;
for (int i=2; i<n; i++)
for (int j=2; j<i; j++)
{
if (i % j == 0)
               break;
else if (i == j+1)
               myvector.push_back(i);
}   
  
   return myvector;
}
//definition of \'dispPrimes\' function only displaying the values in vector \'v\'
void dispPrimes(vector<int> v){
   for (std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i)
std::cout << *i << \' \';
}

CODE IN C++ AND PLEASE MAKE THE CODE COPY AND PASTABLE Work with vectors and functions Write two functions. The first function will compute all the prime number

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site