Structural Programming

Exercise 6 - Functions


1. Reminder from lectures

1.1. Function definition in C++

type name(lists_of_arguments) {
  function body
}

1.2. Function Call

name(lista_na_argumenti);

1.3. Example of user defined function

Write a program in which a cube n3 is calculated for a natural number n.

    #include <iostream>
    using namespace std;
    
    double cube(int x) {
        return x * x * x;
    }
    
    int main() {
        int n;
        cout << "Enter a natural number: ";
        cin >> n;
        double res = cube(n);
    
        cout << "Cube of " << n << " is " << res << endl;
        return 0;
    }

2. Functions from cmath library

3. Example functions

Function Explanation
sqrt(x) square root of x
exp(x) exponential function e^x
log(x) natural logarithm of x (base e)
log10(x) logarithm of x with base 10
fabs(x) absolute value of x
ceil(x) rounds x to the smallest integer not less than x
floor(x) rounds x to the largest integer not greater than x
pow(x, y) x raised to the power y
fmod(x, y) remainder of x/y as a floating-point number
sin(x) sine of x (in radians)
cos(x) cosine of x (in radians)
tan(x) tangent of x (in radians)

1.3. Example of using a function from cmath

Write a program in which a separate function calculates the cube n3 of a natural number n.

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main() {
        int n;
        cout << "Enter a natural number: ";
        cin >> n;
        double res = pow(n, 3);
    
        cout << "Cube of " << n << " is " << res << endl;
        return 0;
    }

4. Exercise

4.1. Exercise 1

Write appropriate functions for calculating a diameter, perimeter, and square of a circle whose radius is entered as an argument. Then write a program in which these functions will be called for R (entered from the keyboard) to calculate the diameter, perimeter, and surface of the corresponding circle.

    #include <iostream>
    #define PI 3.14

    double diameter(double radius);
    double perimeter(double radius);
    double area(double radius);

    int main() {
        double radius, D, L, P;
        std::cout << "Enter the radius of the circle: ";
        std::cin >> radius;

        D = diameter(radius);
        L = perimeter(radius);
        P = area(radius);

        std::cout << "Diameter of the circle = " << D << std::endl;
        std::cout << "Perimeter of the circle = " << L << std::endl;
        std::cout << "Area of the circle = " << P << std::endl;
        return 0;
    }

    double diameter(double radius) {
        return 2 * radius;
    }

    double perimeter(double radius) {
        return 2 * radius * PI;
    }

    double area(double radius) {
        return radius * radius * PI;
    }

4.2. Exercise 2

Write a program that will print all four-digit natural numbers that are divided by the sum of the two numbers composed of the first two digits and the last two digits of the four-digit number. It should eventually print out how many such numbers have been found.

Example:

    3417 is divisible with 34 + 17
    5265 is divisible with 52 + 65
    6578 is divisible with 65 + 78
    #include <iostream>
    using namespace std;
    
    int sum(int n) {
        return n % 100 + n / 100;
    }
    
    int main() {
        int i;
        int count = 0;
        for(i = 1000; i <= 9999; ++i) {
            if(i % sum(i) == 0) {
                cout << i << endl;
                ++count;
            }
        }
        cout << "Total:  " << count;
        return 0;
    }

4.3. Exercise 3

Write a program that calculates for a given natural number the difference between the nearest prime number and that number itself.

Example: For input of 573, The program should output577 – 573 = 4

    #include <iostream>
    using namespace std;
    
    int prime(int n) {
        int i;
        for(i = 2; i * i <= n; ++i) {
            if(n % i == 0) {
                return 0;
            }
        }
        return 1;
    }
   
    int firstLargerPrime(int n) {
        do{
            ++n;
        }while(!prime(n));
        return n;
    }
    
    int main() {
        int n;
        cin >> n;
        int largerPrime = firstLargerPrime(n);
        cout << largerPrime << " " << largerPrime - n;
        return 0;
    }

4.4. Exercise 4

Write a program that will print all prime numbers less than 10000, whose sum of digits is also a prime number. At the end, print how many such numbers are found.

    #include <iostream>
    using namespace std;

    int is_prime(int n) {
        if(n < 4) return 1;
        else {
            if(n % 2 == 0) return 0;
            else {
                int i;
                for(i = 3; i * i <= n; i += 2) {
                    if(n % i == 0) {
                        return 0;
                    }
                }
            }
        }
        return 1;
    }
    
    int sum_digits(int n) {
        int sum = 0;
        while(n != 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }
    
    int main() {
        int i, count = 0;
        for(i = 2; i <= 9999; ++i) {
            if(is_prime(i) && is_prime(sum_digits(i))) {
                cout << i << endl;
                ++count;
            }
        }
        cout << "Total: " << count << endl;
        return 0;
    }

4.5. Exercise 5

Write a program that will print all pairs of prime numbers less than 1000 that differ between them by 2. Finally, print the number of such pairs.

    #include <iostream>
    using namespace std;
    
    int prime(int n) {
        int i;
        for(i = 2; i * i <= n; ++i) {
            if(n % i == 0) {
                return 0;
            }
        }
        return 1;
    }
    
    int main() {
        int i, count = 0;
        for(i = 1; i < 998; ++i) {
            if(prime(i) && prime(i + 2)) {
                cout << "(" << i << ", " << i+2 <<")" << endl;
                ++count;
            }
        }
        cout << "Total: " << count << endl;
        return 0;
    }

4.6. Exercise 6

Calculate the following sum:

1! + (1 + 2)! + (1 + 2 + 3)! + …​ + (1 + 2 + …​ + n)!

NOTE: Use a function to calculate the sum of the first k natural numbers. Use a factorial calculation function to one natural number k

    #include <iostream>
    using namespace std;

    int sum(int n) {
        int i;
        int s = 0;
        for(i = 1; i <= n; ++i) {
            s += i;
        }
        return s;
    }
    
    int factorial(int n) {
        int result = 1;
        int i;
        for(i = 1; i <= n; ++i) {
            result *= i;
        }
        return result;
    }
    
    int main() {
        int n;
        cin >> n;
        if(n > 0) {
            int i;
            int result = 0;
            int s;
            for(i = 1; i < n; ++i) {
                s = sum(i);
                result += faktoriel(s);
                cout << s << "! + ";
            }
            s = sum(n);
            result += factorial(s);
            cout << s << "! = " << result;
        } else {
            cout << "Invalid number" << endl;
        }
        return 0;
    }