Question

In: Computer Science

Write a function intLog of type Integer -> Integer that returns the exponent of the largest...

Write a function intLog of type Integer -> Integer that returns the exponent of the largest power of 2 less than its integer argument.

This needs to be a haskell function

Solutions

Expert Solution

#include <bits/stdc++.h>

using namespace std;

// Function to return the lowest power

// of 2 close to given positive number

int powOfPositive(int n)

{

    // Floor function is used to determine

    // the value close to the number

    int pos = floor(log2(n));

    return pow(2, pos);

}

// Function to return the lowest power

// of 2 close to given negative number

int powOfNegative(int n)

{

    // Ceil function is used for negative numbers

    // as -1 > -4. It would be opposite

    // to positive numbers where 1 < 4

    int pos = ceil(log2(n));

    return (-1 * pow(2, pos));

}

// Function to find the highest power of 2

void highestPowerOf2(int n)

{

    // To check if the given number

    // is positive or negative

    if (n > 0) {

        cout << powOfPositive(n);

    }

    else {

        // If the number is negative,

        // then the ceil of the positive number

        // is calculated and

        // negative sign is added

        n = -n;

        cout << powOfNegative(n);

    }

}

// Driver code

int main()

{

    int n;

cin>>n;

    highestPowerOf2(n);

    return 0;

}


Related Solutions

Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
Write a function called integerPower(base, exponent) that returns the value of baseexponent For example, integerPower(3,4) =...
Write a function called integerPower(base, exponent) that returns the value of baseexponent For example, integerPower(3,4) = 3*3*3*3 Assume that exponent is a non-zero integer, and base is an integer. Function integerPower should use for loop to control the calculation. Do NOT use any math library function. Inputs for this program, which are base and exponent, have to be entered by the user An example of input/output dialog is shown below: Enter Integer Base: 5 Enter Integer Exponent: 3 5 raised...
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
A function the largest integer among three integers.
Write a function, which accept three integer values as arguments find the largest of three and then return the largest value to main program. Write a main program which will call the function by passing three integer values and print the value returned by the function.?
Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns the sum of all the odd numbers that are less than or equal to the input number. The function should return 0 if the number is not positive. For example, if 15 is passed as argument to the function, the function should return the result of 1+3+5+7+9+11+13+15. If -10 is passes, it shall return 0. You shall run the program test the result at...
Write a Scheme function that takes a positive integer n and returns the list of all...
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order: (decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1) Please explain every step
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT