Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h> /* srand, rand */
- #include <time.h> /* time */
- #include <math.h>
- using namespace std;
- // prototypes
- int add(int, int); // no variable names, no function body
- int sub(int, int);
- int mul(int, int);
- int _div(int, int);
- int pow(int, int);
- int soe(int);
- int main() {
- srand(time(NULL));
- // local variables
- int a = rand() % 10; // this is assignment to variable `a`
- int b = rand() % 10;
- soe(8);
- // cout << a << " ^ " << b << " = " << pow(a, b);
- }
- int add(int x, int y) {
- return x + y;
- }
- int sub(int x, int y) {
- return x - y;
- }
- int mul(int x, int y) {
- return x * y;
- }
- int _div(int x, int y) {
- if (y == 0) { // equivalence is not the same as assignment
- cout << "Cannot divide by zero!" << endl;
- }
- return x / y;
- }
- int pow(int base, int power) { // 2^(1/2) == sqrt(2)
- // we handle no decimals
- if (power < 0) {
- cout << "This function does not support returing negative values." << endl;
- return -1;
- }
- // local variable to store the result
- int soln = 1; // intialized to zero.
- // do the looping
- for (int i = 0; i < power; i++) {
- soln *= base;
- }
- return soln;
- }
- int soe(int n) {
- // Input: an integer n > 1.
- if (n < 1) {
- cout << "Negative values aren't considered." << endl;
- }
- //Let A be an array of Boolean values, indexed by integers 2 to n,
- //initially all set to true.
- int* arr = new int[n - 1];
- for (int i = 0; i < n; i++) {
- arr[i] = 1;
- cout << arr[i] << " ";
- }
- // for i = 2, 3, 4, ..., not exceeding √n:
- // if A[i] is true:
- for (int i = 2; i < sqrt(n); i++) {
- if (arr) {
- for (int j = i; j < n; j++) {
- arr[j] = 0;
- }
- }
- }
- // Output: all i such that A[i] is true.
- for (int i = 0; i < n; i++) {
- if (arr[i]) {
- cout << i << " ";
- }
- }
- return 0;
- }
- // ------ Advanced
- // sin
- // cos
- // tan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement