Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Comp science hw help

  1. Sep 6, 2005 #1
    comp science hw 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.
  2. jcsd
  3. Sep 6, 2005 #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 thats probably because factorial was used at the example.
  4. Sep 6, 2005 #3
    could i use the factorial method to do this one and is it a easier way to go??like i said im really new to this and yes i do know how to program in C++.
  5. Sep 6, 2005 #4


    User Avatar
    Science Advisor

    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

  6. Sep 6, 2005 #5
    so would i do this ??
    else return n * compute_exponent(n - 1); // which would return and use the function again?.
  7. Sep 7, 2005 #6


    User Avatar
    Science Advisor

    Yeah, I think that should work.
  8. Sep 7, 2005 #7

    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 dont 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;

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook