Answer to Question #330707 in C++ for Secret Agent

Question #330707

Reverse Engineering

by CodeChum Admin

Functions that return the value of a base and an exponent is very common so I want to switch it up.


This time, we’re going to try to reverse-engineer these functions and find out the exponent of a certain number based on the value of the result and the base used to get that value.


Instructions:

  1. In the code editor, you are provided with a main() function that asks the user for two integer inputs:
  2. The first integer is the base
  3. The second integer is the result
  4. Furthermore, you are provided with the getExponent() function which is partially implemented. The details of this function are the following:
  5. Return type - int
  6. Name - getExponent
  7. Parameters
  8. int - base
  9. int - result
  10. Description - this recursive function returns the exponent
  11. Your task is to add the base case of this recursive function so that it will work properly.

Input


1. The base

2. The result

Output


Enter·the·base:·2
Enter·the·result:·8
Exponent·=·3




1
Expert's answer
2022-04-21T07:02:53-0400
#include <iostream>
#include <cmath>

using namespace std;


int getExponent(int base, int result)
{
	static int exp = 1;
	static int base0 = base;
	if (base != result)
	{
		exp++;
		base *= base0;
		return getExponent(base, result);
	}
	return exp;
}

int main()
{
	int base, result;
	cout << "Please, enter two integers: \n";
	cout << "Enter the base: ";
	cin >> base;
	cout << "Enter the result: ";
	cin >> result;
	cout << "Exponent: " << getExponent(base, result);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS