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

Using C++ with the following code already written Task 5 Add a new function, cal

ID: 3726676 • Letter: U

Question

Using C++ with the following code already written

Task 5

Add a new function, called isPrime(), that takes in a number and returns true if the number is a prime number. Just like with the others, create an output file called primes.txt that contains all of the prime numbers you find.

Determine if a number is prime can be quite time consuming. You will probably want to test your program using the short.txt input file, found here:

Here is the sample output for the full input.txt file. I have also used the unix time command, to show long long it took to run the program over the large input file.

Numbers read in: 100009

Even numbers: 49868

Odd numbers: 50141

Prime numbers: 6774

real   1m12.695s

user   1m12.292s

sys   0m0.376s

Task 6:

Finally, add in the necessary functions to display the maximum, minimum, mean, standard deviation, and sum of these numbers. You have already completed most of these functions in previous assignments.

Expected Output:

Numbers read in: 100009

Even numbers: 49868

Odd numbers: 50141

Prime numbers: 6774

Min number: 27

Max number: 9999930

Sum: 780886019

Mean: 7808.16

Standard Deviation: TBD

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

// Function to check if a number is even
bool isEven(int num){
if(num%2 == 0)
return true;
else{
return false;
}
}
// Function to check if a number is odd
bool isOdd(int num){
if(isEven(num))
return false;
else{
return true;
}
}

int main () {
  
int num, count = 0;
vector <int> integers;
std::ifstream inputFile;
std::ofstream evenFile;
std::ofstream oddFile;
inputFile.open("input.txt"); // Open the file input.txt
  
// Reads in all of the integers in the file
while (inputFile >> num)
{
integers.push_back(num); //Stores data in a vector
count++;
}
cout<<"Numbers read in: "<<integers.size();
inputFile.close();
count = 0;
int oddCount =0;
//Task 3: Write even numbers in a file
evenFile.open("evens.txt", ios::out | ios::app); // Output file object
// Task 4: Write odd numbers to a file
oddFile.open("odds.txt", ios::out | ios::app); // Output file object
  
// Task 2: Count number of evens
vector <int> :: iterator i;
for (i = integers.begin(); i != integers.end(); ++i){
if(isEven(*i)){
evenFile << *i <<endl; // Write data to even file
count++;
}else{
oddFile << *i <<endl; // Write data to odd file
oddCount++;
}
}
cout<<endl<<"Even numbers: "<<count<<endl;
cout<<"Odd numbers: "<<oddCount<<endl;
evenFile.close();
oddFile.close();
return 0;
}

Explanation / Answer

#include <bits/stdc++.h>

using namespace std;

bool isPrime(int n)

{

    // Corner cases

    if (n <= 1) return false;

    if (n <= 3) return true;

    // This is checked so that we can skip

    // middle five numbers in below loop

    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)

        if (n%i == 0 || n%(i+2) == 0)

           return false;

    return true;

}

// Driver Program to test above function

int main()

{

    isPrime(17)? cout << " true ": cout << " false ";

    isPrime(5)? cout << " true ": cout << " false ";

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote