The project name of this exercise is FunctionsAndPrimeNumbers.
The purpose of this assignment is to apply what you have learned about repetition structures and methods, as well as continue to use the concepts you have already learned. Additionally, you learn about how to create your own header files and implementation files.
Introduction A function is a named sequence of instructions that performs a specific task and returns the result of its computation. Once defined, it can be then called in your program wherever that particular task should be performed.
A function can receive zero or more arguments. For example, consider a function sum, which receives three arguments, here named a, b, and c, and returns their sum:
Task A int sum(int a, int b, int c) { // your code is here;
} To execute (or call) a function, you must supply its arguments. For example, if you want to compute the sum of 500, 600, and 700, you can write: sum(500, 600, 700).
A complete program example:
using namespace std;
/* Defining a function that computes the sum of three integers */ int sum(int a, int b, int c) { // your code is here;
}
int main() { // We call it with the actual arguments 1, 20, 300, // and save the result in a variable x int x = sum(1, 20, 300);
cout << x << endl; // Prints 321 }
Task B Let’s define a function that computes the maximum of two integers: /* Returns the maximum of two arguments */ int max2(int a, int b) { // your code is here } Then one can find the maximum of thee integers, for example, like this:
max2(max2(135, 8763), 500 ) // would return 8763 Execution of a function call
Task C. Is divisible? Write a program numbers.cpp that defines a function
bool isDivisibleBy(int n, int d); If n is divisible by d, the function should return true, otherwise return false.
For example: isDivisibleBy(100, 25) == true isDivisibleBy(35, 17) == false The program should also have a main function that tests your code. For example, it can ask the user to input two integer numbers and print Yes if the first number is divisible by the second, otherwise print No.
Task D. Is a prime? A prime number is an integer greater or equal to 2 that is only divisible by 1 and by itself. The first few primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 …
N is a prime if and only if it is not divisible evenly by any of the numbers from 2 to N−1. Let’s implement this decision as a function.
In the same program numbers.cpp, add a function
bool isPrime(int n); The function should return true if n is a prime, otherwise return false. Change the
main function to test your new code.
Task F. Next prime Add a function
int nextPrime(int n); that returns the smallest prime greater than n.
For example: nextPrime(14) == 17 nextPrime(17) == 19 Change the main function to test the new code.
Task G. Count primes in range Add a function
int countPrimes(int a, int b); that returns the number of prime numbers in the interval a ≤ x ≤ b. Change the main function to test the new code.
Task H. Is a twin prime? A prime number N is called a twin prime if either N-2 or N+2 (or both of them) is also a prime. For example, a prime 17 is a twin prime, because 17+2 = 19 is a prime as well. The first few twin primes are: 3, 5, 7, 11, 13, 17, 19, 29, 31 …
Add a function
bool isTwinPrime(int n); that determines whether or not its argument is a twin prime. Change the main function to test the new code.
Task I. Next twin prime Add a function
int nextTwinPrime(int n); that returns the smallest twin prime greater than n. Change the main function to test the new code.
Task J. Largest twin prime in range Add a function
int largestTwinPrime(int a, int b); that returns the largest twin prime in the range a ≤ N ≤ b. If there is no twin primes in range, then return -1.
For example: largestTwinPrime(5, 18) == 17 largestTwinPrime(1, 31) == 31 largestTwinPrime(14, 16) == -1 Change the main function to test the new code.
_Hint: Try to reuse your functions you just have created in the next onces. _Warning: Do not use really big hmbers upon testing your code, remember "Prime number search is a source extencive operation."
Task A
#include <iostream>
using namespace std;
int sum(int a, int b, int c) {
return a + b + c;
}
int main () {
int x;
x = sum(1, 20, 300);
cout << x << endl;
}
Task B
#include <iostream>
using namespace std;
int max2(int a, int b) {
if (a > b) {
return a;
}
else {
return b;
}
}
int main () {
int x;
x = max2(max2(135, 8763), 500);
cout << "max number among 135, 8763, 500: " << x << endl;
}
Task C
#include <iostream>
using namespace std;
bool isDivisibleBy(int n, int d) {
if (n % d == 0) {
return true;
}
else {
return false;
}
}
int main () {
int n, d;
cout << "n = "; cin >> n;
cout << "d = "; cin >> d;
if (isDivisibleBy(n, d)) {
cout << n << " is divisible by " << d;
}
else {
cout << n << " isn't divisible by " << d;
}
}
Task D;
#include <iostream>
using namespace std;
bool checkPrimeNumber(int n) {
bool isPrime = true;
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
isPrime = false;
}
else {
for (int i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
return isPrime;
}
int main () {
int n;
cout << "Enter number: "; cin >> n;
if(checkPrimeNumber(n)) {
cout << "Entered number is prime.";
}
else {
cout << "Entered number isn't prime.";
}
}
Comments
Leave a comment