Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6843619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:16:00+00:00 2026-05-27T00:16:00+00:00

what did I do wrong in this functions. I am pretty sure the problem

  • 0

what did I do wrong in this functions. I am pretty sure the problem is at base = exp(base, pwr /= 2) * exp(base, pwr /= 2); but I cannot think of a logical reason. is there a possible way to write a parameter like that? thanks in advance. (p.s my output of this function is a 2 which is wrong)

#include <iostream>
using namespace std;

unsigned long& exp(unsigned long& base, unsigned long& pwr)
{
    if(pwr == 0)
      base = 1;
    else if(pwr == 1)
      base = base;
    else
      base = exp(base, pwr /= 2) * exp(base, pwr /= 2);
    return base;
}

int main()
{
    unsigned long n=2, m = 4;
    cout << exp(n,m) << endl;
    return 0;
}    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:16:01+00:00Added an answer on May 27, 2026 at 12:16 am

    Here are five things to note about this line:

      base = exp(base, pwr /= 2) * exp(base, pwr /= 2);
    
    1. As noted in a comment above, base is passed by reference, not value, so there is only one copy of it and you’re changing its value. This is a bad idea.
    2. pwr is also passed by reference and you’re changing its value when you use /= instead of just /. There are two /= statements in this line, so after this line runs, pwr now has one fourth its original value.
    3. The exp function will get run twice each time you run this line. It would make more sense to store the value and then square it.
    4. /2 is integer division, so it will round down. So if you give it a number like 3 as an exponent, it will not work correctly because 3/2 is 1. If you correct the other mistakes and the call it with an exponent of 7, it will end up only doing exp(2,7) = exp(2,3)*exp(2,3) = exp(2,1)*exp(2,1)*exp(2,1)*exp(2,1) = 16 when obviously the correct answer is 128. This function, as designed, will only work correctly when the exponent is a power of 2.
    5. Good thing that number 4 is true because if you did get exp(2,1.5) you’d never terminate since it wouldn’t match your base cases. You should probably rethink your algorithm generally.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies if this has been covered before - I did my searching but possibly
I made this simple algorithm, but Chrome is acting weird, almost like functions called
What did I do wrong? Here is an excerpt from my code: public void
I'm solving this K&R exercise: Write versions of the library functions strncpy , strncat
I have 2 overloaded C# functions like this: private void _Insert(Hashtable hash, string tablename,
Not sure what is wrong with parameter 3 or the setup? error C2440: 'type
Note: This might seem like a Super User question at first, but please read
OK, I'm losing my mind over this. I did read here at SO and
I think that the wrapper framework in valgrind is pretty nice. I am trying
I have a Javascript functions which throw exceptions if something goes wrong. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.