Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. /** @file */
  2.  
  3. /*
  4. FUNDAMENTALS OF COMPUTER PROGRAMMING
  5.  
  6. Lecture 03: functions
  7.  
  8. */
  9.  
  10. #define debug(x) std::cerr << "(" << __LINE__ << ") " << #x << " == " << (x) << std::endl
  11.  
  12.  
  13. #include <iostream>
  14. #include <vector>
  15. #include <random>
  16. #include <chrono>
  17.  
  18.  
  19.  
  20.  
  21. /** Just a simple function for adding numbers (commmented in doxygen style).
  22. * @param augend the first summand
  23. * @param addens the second summand
  24. * @return sum of parameters
  25. * @author it's me :-)
  26. * @date 2019-10-18
  27. */
  28. int add (int augend, const int addend) // parameters are copied!
  29. {
  30. int sum = augend + addend;
  31. augend = 10000; // modification of a copy, original not changed!
  32. return sum;
  33. }
  34.  
  35. /** Just a simple function for subtracting numbers.
  36. * @param minuend
  37. * @param substrahend
  38. * @return difference of parameters
  39. * @author it's me :-)
  40. * @date 2019-10-19
  41. */
  42. auto subtract (const int minuend, const int substrahend) -> int
  43. {
  44. return minuend - substrahend;
  45. }
  46.  
  47. /** Function for multiplication of two numbers.
  48. * @param multiplier left hand factor
  49. * @param multiplicand right hand factor
  50. */
  51. auto multiply (const int multiplier, const int multiplicand)
  52. {
  53. return multiplier * multiplicand;
  54. }
  55.  
  56. /** The function increases the difference between both parameters by incrementing of the first parameter,
  57. and decrementing of the second one.
  58. @param [in,out] a parameter to increment (parameter passed by reference)
  59. @param [in,out] b parameter to decrement (parameter passed by reference)
  60. */
  61. void increaseDifference (int & a, int & b)
  62. {
  63. a++; // modification of parameter passed by reference is visible outside the function
  64. b--; // the same :-)
  65. }
  66.  
  67. /** The inline function for division of numbers.
  68. @param a dividend
  69. @param b divisor
  70. @return quotient
  71. */
  72. inline double divide (const double a, const double b)
  73. {
  74. return a / b;
  75. }
  76.  
  77. /** @return The function returns a random value from the interval [0, 99]. */
  78. int random_value ()
  79. {
  80. std::default_random_engine engine;
  81. engine.seed (std::chrono::system_clock::now().time_since_epoch().count());
  82. std::uniform_int_distribution<int> distro (0, 99);
  83.  
  84. return distro(engine);
  85. }
  86.  
  87. /** @return Always returns 12. */
  88. const int twelve ()
  89. {
  90. return 12;
  91. }
  92.  
  93. /** @return Always returns 12. */
  94. constexpr int constexpr_twelve ()
  95. {
  96. // return random_value(); // not legal: all expressions inside a constexpr function must be constexpr
  97. return 12;
  98. }
  99.  
  100.  
  101.  
  102. /** @return a factorial of a number
  103.  
  104. 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.
  105. */
  106. constexpr
  107. unsigned long long int factorial (unsigned int i)
  108. {
  109. return i < 2 ? 1 : i * factorial(i - 1);
  110.  
  111. /*
  112. if (i < 2)
  113. return 1;
  114. else
  115. return i * factorial(i - 1);
  116. */
  117. }
  118.  
  119. const int foo(int i)
  120. {
  121. return i;
  122. }
  123.  
  124. /** Function for printing elements of an array.
  125. @param t an array to print (const to prevent any modifications of array's content)
  126. @param N size of an array
  127. @return Function returns nothing, thus "void" return type.
  128. */
  129. void print (const double t[], const int N)
  130. {
  131. for (int i = 0; i < N; i++)
  132. std::cout << t[i] << " ";
  133. std::cout << std::endl;
  134. }
  135.  
  136.  
  137. /** 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.
  138. @param t an array to fill
  139. @param N size of an array
  140. @return Function returns nothing.
  141. @todo function not implemented yet
  142. */
  143. void fill (double t[], const int N)
  144. {
  145. std::default_random_engine engine;
  146. engine.seed(std::chrono::system_clock::now().time_since_epoch().count());
  147. std::normal_distribution<double> distro (170, 20); // Gaussian distribution
  148.  
  149.  
  150. for (int i = 0; i < N; i++)
  151. t[i] = distro(engine);
  152. }
  153.  
  154. /** @param count number of parameters
  155. @param params array of char * strings (C-style strings)
  156. */
  157. int main (int count, char * params[])
  158. {
  159.  
  160. // mathematical approach
  161. double number = 1.5;
  162. double value = sin (number);
  163. debug(number);
  164. debug(value);
  165.  
  166. // return 0;
  167.  
  168.  
  169.  
  170. int p1 = 5;
  171. int p2 = 4;
  172. debug(p1);
  173. std::cout << add (p1, p2) << std::endl;
  174. debug(p1);
  175.  
  176. // return 0;
  177. //
  178. std::cout << subtract (5, 4) << std::endl;
  179. std::cout << multiply (5, 4) << std::endl;
  180. // return 0;
  181. int first = 5;
  182. int second = 9;
  183.  
  184. int result = add(first, second);
  185. debug(result);
  186. debug(first);
  187. debug(second);
  188. // return 0;
  189.  
  190. // passing of parameters
  191. int left = 10;
  192. int right = -10;
  193.  
  194. debug(left);
  195. debug(right);
  196.  
  197. increaseDifference (left, right);
  198.  
  199. debug(left);
  200. debug(right);
  201.  
  202. // return 0;
  203.  
  204. const int const_LENGTH = random_value();
  205.  
  206. // const_LENGTH++;
  207. // int array2[const_LENGTH]; // not legal!
  208.  
  209. // debug(const_LENGTH);
  210.  
  211. // return 0;
  212.  
  213.  
  214. const int LENGTH = twelve();
  215. // int array[LENGTH]; // not legal
  216.  
  217. // constexpr
  218. const int LENGTH_cexpr = constexpr_twelve();
  219. int array[LENGTH_cexpr];
  220.  
  221.  
  222.  
  223. // return 0;
  224.  
  225.  
  226. int variable = 4;
  227. // constexpr
  228. int arr_ints [factorial(4)];
  229. // int arr_ints [factorial(variable)];
  230.  
  231. // return 0;
  232.  
  233. // inline expansion
  234. double quotient = divide (45, 55); // expanded to: 45 / 55 ;
  235. debug(quotient);
  236.  
  237. // return 0;
  238.  
  239. // passing arrays to functions
  240. const int SIZE = 5;
  241. double arr[SIZE];
  242.  
  243. // return 0;
  244. // print an array
  245. std::cout << "uninitialised array" << std::endl;
  246. print (arr, SIZE);
  247.  
  248. fill (arr, SIZE);
  249.  
  250. print(arr, SIZE);
  251. // return 0;
  252.  
  253. debug(count);
  254.  
  255. // main is also a function
  256. for (int i = 0; i < count; i++)
  257. std::cout << i << ": " << params[i] << std::endl;
  258.  
  259.  
  260. // return 0; // ./main ; echo $?
  261.  
  262.  
  263. // return 0;
  264.  
  265. // VECTORS
  266. // STL: standard template library
  267. std::vector<int> vec_ints; // vector of ints
  268. std::vector<double> vec_doubles; // vector of doubles
  269.  
  270. std::cout << vec_ints.size() << std::endl; // vector's length
  271.  
  272. vec_ints.push_back(4); // push item to the back of the vector
  273. std::cout << vec_ints.size() << std::endl;
  274. vec_ints.push_back(5);
  275. std::cout << vec_ints.size() << std::endl;
  276. vec_ints.push_back(6);
  277. std::cout << vec_ints.size() << std::endl;
  278.  
  279. for (int i = 0; i < 150; i++)
  280. {
  281. vec_ints.push_back(i);
  282. std::cout << "size: " << vec_ints.size() << ", capacity: " << vec_ints.capacity() << std::endl;
  283. }
  284.  
  285. for (int i = 0; i < 10; i++)
  286. {
  287. vec_ints[i] = -i;
  288. std::cout << vec_ints[i] << std::endl;
  289. }
  290.  
  291. vec_ints[-1] = 45; // mind the indices!!!
  292.  
  293.  
  294. return 0;
  295.  
  296. }
  297.  
  298. // Finis coronat opus.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement