Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @file */
- /*
- FUNDAMENTALS OF COMPUTER PROGRAMMING
- Lecture 03: functions
- */
- #define debug(x) std::cerr << "(" << __LINE__ << ") " << #x << " == " << (x) << std::endl
- #include <iostream>
- #include <vector>
- #include <random>
- #include <chrono>
- /** Just a simple function for adding numbers (commmented in doxygen style).
- * @param augend the first summand
- * @param addens the second summand
- * @return sum of parameters
- * @author it's me :-)
- * @date 2019-10-18
- */
- int add (int augend, const int addend) // parameters are copied!
- {
- int sum = augend + addend;
- augend = 10000; // modification of a copy, original not changed!
- return sum;
- }
- /** Just a simple function for subtracting numbers.
- * @param minuend
- * @param substrahend
- * @return difference of parameters
- * @author it's me :-)
- * @date 2019-10-19
- */
- auto subtract (const int minuend, const int substrahend) -> int
- {
- return minuend - substrahend;
- }
- /** Function for multiplication of two numbers.
- * @param multiplier left hand factor
- * @param multiplicand right hand factor
- */
- auto multiply (const int multiplier, const int multiplicand)
- {
- return multiplier * multiplicand;
- }
- /** The function increases the difference between both parameters by incrementing of the first parameter,
- and decrementing of the second one.
- @param [in,out] a parameter to increment (parameter passed by reference)
- @param [in,out] b parameter to decrement (parameter passed by reference)
- */
- void increaseDifference (int & a, int & b)
- {
- a++; // modification of parameter passed by reference is visible outside the function
- b--; // the same :-)
- }
- /** The inline function for division of numbers.
- @param a dividend
- @param b divisor
- @return quotient
- */
- inline double divide (const double a, const double b)
- {
- return a / b;
- }
- /** @return The function returns a random value from the interval [0, 99]. */
- int random_value ()
- {
- std::default_random_engine engine;
- engine.seed (std::chrono::system_clock::now().time_since_epoch().count());
- std::uniform_int_distribution<int> distro (0, 99);
- return distro(engine);
- }
- /** @return Always returns 12. */
- const int twelve ()
- {
- return 12;
- }
- /** @return Always returns 12. */
- constexpr int constexpr_twelve ()
- {
- // return random_value(); // not legal: all expressions inside a constexpr function must be constexpr
- return 12;
- }
- /** @return a factorial of a number
- A constexpr keyword denotes a function the result of which can be used as a constant value. A function must be simple enough so that it is possible to evalue it in compilation time.
- */
- constexpr
- unsigned long long int factorial (unsigned int i)
- {
- return i < 2 ? 1 : i * factorial(i - 1);
- /*
- if (i < 2)
- return 1;
- else
- return i * factorial(i - 1);
- */
- }
- const int foo(int i)
- {
- return i;
- }
- /** Function for printing elements of an array.
- @param t an array to print (const to prevent any modifications of array's content)
- @param N size of an array
- @return Function returns nothing, thus "void" return type.
- */
- void print (const double t[], const int N)
- {
- for (int i = 0; i < N; i++)
- std::cout << t[i] << " ";
- std::cout << std::endl;
- }
- /** Function fills an array with some values. Arrays are handled in a special way. We can modify arrays passed to functions without references (&). An array passed to a function is not copied.
- @param t an array to fill
- @param N size of an array
- @return Function returns nothing.
- @todo function not implemented yet
- */
- void fill (double t[], const int N)
- {
- std::default_random_engine engine;
- engine.seed(std::chrono::system_clock::now().time_since_epoch().count());
- std::normal_distribution<double> distro (170, 20); // Gaussian distribution
- for (int i = 0; i < N; i++)
- t[i] = distro(engine);
- }
- /** @param count number of parameters
- @param params array of char * strings (C-style strings)
- */
- int main (int count, char * params[])
- {
- // mathematical approach
- double number = 1.5;
- double value = sin (number);
- debug(number);
- debug(value);
- // return 0;
- int p1 = 5;
- int p2 = 4;
- debug(p1);
- std::cout << add (p1, p2) << std::endl;
- debug(p1);
- // return 0;
- //
- std::cout << subtract (5, 4) << std::endl;
- std::cout << multiply (5, 4) << std::endl;
- // return 0;
- int first = 5;
- int second = 9;
- int result = add(first, second);
- debug(result);
- debug(first);
- debug(second);
- // return 0;
- // passing of parameters
- int left = 10;
- int right = -10;
- debug(left);
- debug(right);
- increaseDifference (left, right);
- debug(left);
- debug(right);
- // return 0;
- const int const_LENGTH = random_value();
- // const_LENGTH++;
- // int array2[const_LENGTH]; // not legal!
- // debug(const_LENGTH);
- // return 0;
- const int LENGTH = twelve();
- // int array[LENGTH]; // not legal
- // constexpr
- const int LENGTH_cexpr = constexpr_twelve();
- int array[LENGTH_cexpr];
- // return 0;
- int variable = 4;
- // constexpr
- int arr_ints [factorial(4)];
- // int arr_ints [factorial(variable)];
- // return 0;
- // inline expansion
- double quotient = divide (45, 55); // expanded to: 45 / 55 ;
- debug(quotient);
- // return 0;
- // passing arrays to functions
- const int SIZE = 5;
- double arr[SIZE];
- // return 0;
- // print an array
- std::cout << "uninitialised array" << std::endl;
- print (arr, SIZE);
- fill (arr, SIZE);
- print(arr, SIZE);
- // return 0;
- debug(count);
- // main is also a function
- for (int i = 0; i < count; i++)
- std::cout << i << ": " << params[i] << std::endl;
- // return 0; // ./main ; echo $?
- // return 0;
- // VECTORS
- // STL: standard template library
- std::vector<int> vec_ints; // vector of ints
- std::vector<double> vec_doubles; // vector of doubles
- std::cout << vec_ints.size() << std::endl; // vector's length
- vec_ints.push_back(4); // push item to the back of the vector
- std::cout << vec_ints.size() << std::endl;
- vec_ints.push_back(5);
- std::cout << vec_ints.size() << std::endl;
- vec_ints.push_back(6);
- std::cout << vec_ints.size() << std::endl;
- for (int i = 0; i < 150; i++)
- {
- vec_ints.push_back(i);
- std::cout << "size: " << vec_ints.size() << ", capacity: " << vec_ints.capacity() << std::endl;
- }
- for (int i = 0; i < 10; i++)
- {
- vec_ints[i] = -i;
- std::cout << vec_ints[i] << std::endl;
- }
- vec_ints[-1] = 45; // mind the indices!!!
- return 0;
- }
- // Finis coronat opus.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement