Ok. sorry to bother you guys with a newb question.

This is a discussion on Ok. sorry to bother you guys with a newb question. within the C++ Programming forums, part of the General Programming Boards category; Ok ive just started learning c++. Acctualy i tried to start for like a year ago but I didnt have ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    25

    Ok. sorry to bother you guys with a newb question.

    Ok ive just started learning c++. Acctualy i tried to start for like a year ago but I didnt have enough time to learn it.
    Anyway. here is my problem.

    "Write a program that asks for a number and a power. Write a recursive function that takes the number to the power. Thus, if the number is 2 and the power is 4, the function will return 16"

    That is an exercise from my book.

    So far i have this
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    float myFunc(unsigned short int x, unsigned short int y);
    
    int main()
    {
    unsigned short int x = 0;
    unsigned short int y = 0;
    float z;
    
    std::cout<<"Write a exponent: ";
    std::cin>>x;
    std::cout<<"\nNow write a number: ";
    std::cin>>y;
    z = myFunc(x,y);
    std::cout<<"\n the answer is:" << z;
    system("pause");
    return 0;
    }
    
    float myFunc(unsigned short int x, unsigned short int y)
    {
     
    
    }

    I don't have a clue of what to put in my function. everytime i try it just ends up with some sort of a loop.
    Don't laugh, i just started reading this book a couple of hours ago. And this recursive functions is hard for me to understand.
    Last edited by arnis; 07-16-2003 at 08:30 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    25
    Ok. I tried some more. This is what i got.

    Code:
    #include <iostream>
    #include <stdlib.h>
    float sum;
    int count = 1;
    float myFunc(unsigned short int x, unsigned short int y);
    int main()
    {
    unsigned short int x = 0;
    unsigned short int y = 0;
    float z;
    
    std::cout<<"Write a exponent: ";
    std::cin>>x;
    std::cout<<"\nNow write the number: ";
    std::cin>>y;
    sum = y;
    z = myFunc(x,y);
    std::cout<<"\n the answer is:" << z;
    system("pause");
    }
    
    float myFunc(unsigned short int x, unsigned short int y)
    {
    if(count==x)
    return sum;
    else{
    count++;
    sum = sum * y;
    return(myFunc(x,y));
    }
    }
    It works. But it just seams so , i dont know, uncorrect. I seams like i use a method that is not what the author of my book intended.
    Any comments?
    Last edited by arnis; 07-16-2003 at 08:29 PM.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yes, you want to avoid using global variables when possible. The method I came up with involves passing x as two of the parameters of "myFunc" , where x1^y. The second x2 would change when the function recurses...ok maybe that doesn't make much sense so:

    Code:
    float power(float x1,float x2,int y)
       {
         if (y>1)
            power(x1,(x2*x1),(y-1));
         else
           return x2;
       }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    int power(int base, int exp)
    {
         if (exp == 0) 
            return 1; 
    
         return (power(base, exp - 1) * base);
    }

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I knew there was a better way...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    What are you guys talking about.
    Code:
    template<int x, unsigned int y>
    struct power
    {
       static const int val = x * power<x, y - 1>::val;
    };
    
    template<int x>
    struct power<x, 0>
    {
       static const int val = 1;
    };
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Zach, that is only for compile time.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This should work,
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    float myFunc(unsigned int, unsigned int);
    
    int main()
    {
          unsigned short int exponent = 0;
          unsigned short int number = 0;
          float z;
    
          std::cout<<"Write a exponent: ";
          std::cin>>exponent;
    
          std::cout<<"\nNow write a number: ";
          std::cin>>number;
    
          z = myFunc(exponent, number);
          std::cout<<"\nThe answer is : " << z << endl << endl;
    
          system("PAUSE");
          return 0;
    }
    
    float myFunc(unsigned int exp, unsigned int num)
    {
          if (exp > 0)
             return (float) (num * myFunc( --exp, num) );
    
          return 1;
    }

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why does the function return a float anyway?

    Eibro's code is still the best.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    25
    Thanks for all the replies. I will analyze this carefully now.

    " CornedBee: Why does the function return a float anyway?"

    I just thought that would be the best way. Because this numbers usualy gets very big.

    example: 5^11 = very high number.
    And im not sure how high an int can go.
    As I stated earlier in this thread, im a newbie, but thats what i think.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    int can go up to 2 billion, unsigned int to 4 billion (on 32-bit systems anyway).

    To be more specific, the highest possible value of int is
    2^31 - 1
    and of unsigned int
    2^32 - 1
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    25
    oh thanks. thats good to know

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by CornedBee
    Zach, that is only for compile time.
    Yes, I know... It was really just a joke.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newb question about interpreting and writing functions
    By crazychile in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 08:51 PM
  2. newb question on ARRAYS
    By viciousv322 in forum C++ Programming
    Replies: 18
    Last Post: 11-16-2005, 07:34 PM
  3. Need to ask you guys a Question....
    By Halo in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-03-2003, 01:38 AM
  4. A newb question about keyboard input
    By PorkyChop in forum Game Programming
    Replies: 4
    Last Post: 12-06-2002, 01:12 PM
  5. Replies: 4
    Last Post: 11-19-2002, 09:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21