Question

#include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be...

#include <iostream>

#include <chrono>

using namespace std;

double improvedPow(double x, int y)

{

// To be implemented by you

}

int main()

{

cout << "To calculate x^y ..." << endl;

double x;

int y;

cout << "Please enter x: ";

cin >> x;

cout << "Please enter y: ";

cin >> y;

if(x == 0) {

if (y > 0)

cout << 0 << endl;

else

cout << "x^y is not defined" <<endl;

}

else {

cout << improvedPow(x,y) << endl;

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <chrono>

using namespace std;

double improvedPow(double x, int y) {
    if (y == 0) {
        return 1;
    } else {
        if (y > 0) {
            double res = improvedPow(x, y / 2);
            if (y % 2 == 0) {
                return res * res;
            } else {
                return res * res * x;
            }
        } else {
            return 1.0 / improvedPow(x, -y);
        }
    }
}

int main() {
    cout << "To calculate x^y ..." << endl;
    double x;
    int y;
    cout << "Please enter x: ";
    cin >> x;
    cout << "Please enter y: ";
    cin >> y;
    if (x == 0) {
        if (y > 0)
            cout << 0 << endl;
        else
            cout << "x^y is not defined" << endl;
    } else {
        cout << improvedPow(x, y) << endl;
        return 0;
    }
}
Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout...

    Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout << "Enter an integer cin >> number; if (number > B) cout << You entered a positive integer: " << number << endl; else if (number (8) cout<<"You entered a negative integer: " << number << endl; cout << "You entered e." << endl; cout << "This line is always printed." return 0;

  • #include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0;...

    #include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • #include <iostream> #include <cmath> using namespace std; int main() { int x; int y; int z;...

    #include <iostream> #include <cmath> using namespace std; int main() { int x; int y; int z; int r1; int r2; cin >> x; cin >> y; cin >> z; r1 = 4* pow(x,3)-5*pow(y,2)+3*z; r2 = r1+7*pow(x,3)-2*pow(y,2)+11*z; cout << "r1="; cout << 4* pow(x,3)-5*pow(y,2)+3*z; cout << endl; cout << "r2="; cout << r1+7*pow(x,3)-2*pow(y,2)+11*z; cout << endl; cout << "r3="; cout << r2-9*pow(x,3)+22*pow(y,2)-6*z; cout << endl; return 0; } 1-115 Your output r2-395 13-186 5:Compare output Output differs. See highlights below. -163...

  • #include<iostream> using namespace std; double hey(int x){ if(x==2) return 1; int i=0; if (x%2==0){ while(i<=2){ cout<<(x/2)<<endl;...

    #include<iostream> using namespace std; double hey(int x){ if(x==2) return 1; int i=0; if (x%2==0){ while(i<=2){ cout<<(x/2)<<endl; i++; break; } } if (x%2==1){ while(i<=2){ i=3*x+1; cout<<i<<endl; } return 1; } } int main() { int n; cout<<"insert a number to check how many steps to reduce down to 2"<<endl; cin>>n; cout<<hey(n)<<endl; return 0; } I couldn't

  • What is the difference between these two programs? #include <iostream> using namespace std; int DontPanic(int &...

    What is the difference between these two programs? #include <iostream> using namespace std; int DontPanic(int & x); int z = 10; void main() { char x = 'y'; int y = 5; int z = 100; y = DontPanic(z); cout << x << " " << y << " " << z << endl; } int DontPanic(int & x) { int * p; p = & z; x = (*p)++ + 1; cout << x << " " << *p...

  • Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string...

    Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string first, last, job;    double hours, wages, net, gross, tax, taxrate = .40;    double oPay, oHours;    int deductions;    // input section    cout << "Enter First Name: ";    cin >> first;    cout << "Enter Last Name: ";    cin >> last;    cin.ignore();    cout << "Enter Job Title: ";    getline(cin, job);    cout << "Enter Hours Worked:...

  • Watermelon Problem: The answer should not include any whitespace. #include<iostream> using namespace std; int main() {...

    Watermelon Problem: The answer should not include any whitespace. #include<iostream> using namespace std; int main() {     double distance, gravity =9.8, height;     int time, t=0;     cout<<"Please input the time of fall in seconds:"<<endl;     [ A ] // Get the user input of the time of fall in seconds (Use cin>> method)     cout<<endl;     cout<<"Please input the height of the bridge in meters:"<<endl;     [ B ] // Get the user input of the height of the bridge in meters (Use cin>>...

  • Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length,...

    Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length, int width = 0); int main() { int length, width; // for rectangle use both arguments cout << "Enter length and width of a rectangle" << endl; cin >> length >> width; cout << "The area is " << area(length, width) << endl; // for square, only need first argument cout << "Enter side of a square" << endl; cin >> length; cout <<...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT