Comp Sci HW Help: Compute Exponent in C++

  • Thread starter heavyc
  • Start date
  • Tags
    Science
In summary, the conversation is about a request for help with implementing a C++ program that calls a recursive function to compute a to the power of n, with a being a real number and n being a non-negative integer. The person is struggling with where to start and has some questions about recursion and how to get user input. They receive some tips and guidance on how to approach the problem and discuss the necessary components of the program."
  • #1
heavyc
16
0
comp science homework help!

I have this problem in Data Abstraction and problem solving with C++. Here is the problem. Implement a C++ program that calls a recursive function "compute_exponent". compute_exponent function computes a to the power n, where a is a real number and n is a non-negative integer.

I am new to this class and I have no idea what I am doing but i think I know how to put the function together bbut I have no idea how to get this problem started because we didnt really go over to much in this class but if anyone can help me out a little it would be a very grateful thing lol. So please anyone.
 
Physics news on Phys.org
  • #2
do you know what recursion is?
is it told how to immplement recursion on the power(exponrent refers to n not to a^n) problem?
if not do you have an inkling of how you would implement recursion on the power problem.
if you have done the factorial problem look how you could relate it to the power problem.

lastly do you know C++

I find it funny that you are asked to find recursion method for power rather than factorial. But that's probably because factorial was used at the example.
 
  • #3
neurocomp2003 said:
do you know what recursion is?
is it told how to immplement recursion on the power(exponrent refers to n not to a^n) problem?
if not do you have an inkling of how you would implement recursion on the power problem.
if you have done the factorial problem look how you could relate it to the power problem.

lastly do you know C++

I find it funny that you are asked to find recursion method for power rather than factorial. But that's probably because factorial was used at the example.
could i use the factorial method to do this one and is it a easier way to go??like i said I am really new to this and yes i do know how to program in C++.
 
  • #4
No, that's not what he meant. He just meant that finding n! is much more the "standard" recursion exercise.

"Recursion" means that you can call the function itself inside the function.

For the "factorial" problem, you would do this: 0!= 1 by definition. n!= n(n-1)! so we write a function to do exactly that:

int factorial(int n)
{
if n= 0 then return 1; //which returns the value 1 and ends the function
else return n*factorial(n-1); //which uses this function again!
}
wasn't that easy? Just for fun "walk through" that when n= 3.

For an, if n=0, a0= 1 and if n> 0 an= a*an-1. Do basically the same thing as above.

float compute_exponent(float a, int n)
{
if n= 0 return 1;
else //I'll let you fill it in here

}
 
  • #5
HallsofIvy said:
No, that's not what he meant. He just meant that finding n! is much more the "standard" recursion exercise.

"Recursion" means that you can call the function itself inside the function.

For the "factorial" problem, you would do this: 0!= 1 by definition. n!= n(n-1)! so we write a function to do exactly that:

int factorial(int n)
{
if n= 0 then return 1; //which returns the value 1 and ends the function
else return n*factorial(n-1); //which uses this function again!
}
wasn't that easy? Just for fun "walk through" that when n= 3.

For an, if n=0, a0= 1 and if n> 0 an= a*an-1. Do basically the same thing as above.

float compute_exponent(float a, int n)
{
if n= 0 return 1;
else //I'll let you fill it in here

}

so would i do this ??
else return n * compute_exponent(n - 1); // which would return and use the function again?.
 
  • #6
Yeah, I think that should work.
 
  • #7
heavyc said:
I have this problem in Data Abstraction and problem solving with C++. Here is the problem. Implement a C++ program that calls a recursive function "compute_exponent". compute_exponent function computes a to the power n, where a is a real number and n is a non-negative integer.

I am new to this class and I have no idea what I am doing but i think I know how to put the function together bbut I have no idea how to get this problem started because we didnt really go over to much in this class but if anyone can help me out a little it would be a very grateful thing lol. So please anyone.


The hardest part is starting out. Whether its this or any kind of program, write out as much as you know on paper, put it into groups, try to visualize it in a way that gives you a pattern to work with.

CPP is advanced, the form of coding is not easy to read but its simple once you get it down.

What do you know for sure?

1. The name is compute_exponent
2. The variables are A and N, in the form [txt]a^n[\txt] so var 'a' is the base, and var 'n' is the exponent. How will you get these values from the user when the program runs?

you will print a message to explain the function and ask the user for input. Or the function can be called internally.

At least you will have your function_name and 2 variables to input, then it will return what type?

3. The return type is INT (or FLOAT)
4. The variable 'a' is type REAL; So you test it first for some range
5. The variable 'n' is positive INT; So you test this for being positive and INT

Is that it? Now you can start to picture it, a function, named (name) and the variables, called a, n, types real, int; return type is int

So far you would have

INT compute_exponent (REAL a, INT n) {

// first thing is to check the input, assume the worst

IF ( 0 == a ) {

console.message(error, variable a is zero);
return 0;
}

IF ( 0 == n ) {
console.message(error, variable n is zero);
return 0;
}

//you would want to then try the switch statement to validate the input further, but don't waste time if its zero

//now declare any needed local variables



// create your method to multiply 'a' times itself 'n' times



for loop/while loop

your n count is not zero

subtotal *= a

decrement n

return subtotal;

}
 

1. How do I compute an exponent in C++?

To compute an exponent in C++, you can use the pow() function from the cmath library. This function takes two parameters: the base and the exponent. For example, to compute 2 to the power of 3, you would use pow(2, 3).

2. Can I use the ^ operator to compute an exponent in C++?

Yes, the ^ operator can be used to compute an exponent in C++. However, it is not the same as the mathematical exponent operator. In C++, ^ is the bitwise XOR operator, so it may not work as expected for computing exponents.

3. What data types can be used for computing exponents in C++?

You can use any numeric data type for computing exponents in C++, including int, float, double, etc. However, keep in mind that using larger data types like double or long double will give more accurate results for larger exponents.

4. How can I handle negative exponents in C++?

To handle negative exponents in C++, you can use the pow() function with a negative exponent. For example, to compute 2 to the power of -3, you would use pow(2, -3). Alternatively, you can also use the pow() function with a reciprocal value, such as pow(2, 1.0 / 3.0) to compute the cube root of 2.

5. Are there any other functions or libraries that can be used to compute exponents in C++?

Yes, there are other functions and libraries that can be used to compute exponents in C++. For example, the exp() function from the cmath library can be used to compute e to the power of a number. There are also third-party libraries like Boost that offer more advanced mathematical functions for computing exponents and other operations.

Similar threads

  • STEM Career Guidance
Replies
3
Views
1K
Replies
1
Views
880
  • Programming and Computer Science
Replies
11
Views
1K
Replies
8
Views
1K
  • Introductory Physics Homework Help
Replies
11
Views
759
  • Programming and Computer Science
Replies
19
Views
2K
  • Introductory Physics Homework Help
Replies
5
Views
2K
  • Introductory Physics Homework Help
Replies
5
Views
4K
  • Introductory Physics Homework Help
Replies
5
Views
1K
  • STEM Academic Advising
Replies
4
Views
1K
Back
Top