Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h> /* srand, rand */
  3. #include <time.h> /* time */
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. // prototypes
  9. int add(int, int); // no variable names, no function body
  10. int sub(int, int);
  11. int mul(int, int);
  12. int _div(int, int);
  13. int pow(int, int);
  14. int soe(int);
  15.  
  16. int main() {
  17. srand(time(NULL));
  18.  
  19. // local variables
  20. int a = rand() % 10; // this is assignment to variable `a`
  21. int b = rand() % 10;
  22. soe(8);
  23. // cout << a << " ^ " << b << " = " << pow(a, b);
  24. }
  25.  
  26. int add(int x, int y) {
  27. return x + y;
  28. }
  29.  
  30. int sub(int x, int y) {
  31. return x - y;
  32. }
  33.  
  34. int mul(int x, int y) {
  35. return x * y;
  36. }
  37.  
  38. int _div(int x, int y) {
  39. if (y == 0) { // equivalence is not the same as assignment
  40. cout << "Cannot divide by zero!" << endl;
  41. }
  42. return x / y;
  43. }
  44.  
  45. int pow(int base, int power) { // 2^(1/2) == sqrt(2)
  46. // we handle no decimals
  47. if (power < 0) {
  48. cout << "This function does not support returing negative values." << endl;
  49. return -1;
  50. }
  51.  
  52. // local variable to store the result
  53. int soln = 1; // intialized to zero.
  54.  
  55. // do the looping
  56. for (int i = 0; i < power; i++) {
  57. soln *= base;
  58. }
  59.  
  60. return soln;
  61. }
  62.  
  63. int soe(int n) {
  64. // Input: an integer n > 1.
  65. if (n < 1) {
  66. cout << "Negative values aren't considered." << endl;
  67. }
  68.  
  69. //Let A be an array of Boolean values, indexed by integers 2 to n,
  70. //initially all set to true.
  71. int* arr = new int[n - 1];
  72.  
  73. for (int i = 0; i < n; i++) {
  74. arr[i] = 1;
  75. cout << arr[i] << " ";
  76. }
  77.  
  78. // for i = 2, 3, 4, ..., not exceeding √n:
  79. // if A[i] is true:
  80. for (int i = 2; i < sqrt(n); i++) {
  81. if (arr) {
  82. for (int j = i; j < n; j++) {
  83. arr[j] = 0;
  84. }
  85. }
  86. }
  87.  
  88. // Output: all i such that A[i] is true.
  89. for (int i = 0; i < n; i++) {
  90. if (arr[i]) {
  91. cout << i << " ";
  92. }
  93. }
  94. return 0;
  95. }
  96.  
  97. // ------ Advanced
  98. // sin
  99. // cos
  100. // tan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement