Can Anybody help me out, in converting my code to iterative method.

This is a discussion on Can Anybody help me out, in converting my code to iterative method. within the C++ Programming forums, part of the General Programming Boards category; Hello, I am new on programming, is currently using dev c++. I was asked to write a method to find ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Can Anybody help me out, in converting my code to iterative method.

    Hello,

    I am new on programming, is currently using dev c++. I was asked to write a method to find x^y, where y is a power of x. Using recursive and iterative solution. I already have the recursive one, but I don't know how to make it to iterative. Here is my recursive solution.
    Code:
    #include<stdio.h>
    
    
    int power (int, int);
    
    int main ()
    
    {
    	int base, exp, result;
    
    	
    	printf("Please enter a base number\n");
    	scanf("%d",&base);
    	printf("Please enter a exponent number\n");
    	scanf("%d",&exp);
    
    	result = power(base, exp);
    
    
    	printf("\n The result is:%d\n",result);
    	
    	system("pause");
        return 0;
    }
    
    
    
    int power (int base, int exp)
    {
       if(exp >= 1)
           return base * (power(base,exp - 1));
    
       else
           return 1;
    }
    Help would highly be appreciated. Thanks in advance..
    Last edited by Salem; 03-14-2011 at 09:47 AM. Reason: Added [code][/code] tags

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Use a loop to keep multiplying the base exp number of times

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Yeah, I tried to used loop and got this code....

    #include<stdio.h>

    int main()

    {
    int base, exp, result;


    printf("Please enter a base number\n");
    scanf("%d",&base);
    printf("Please enter a exponent number\n");
    scanf("%d",&exp);

    int i = 1;
    for (i=1; i < exp; i++)
    result=result*base;
    printf("\n The result is:%d\n",result);

    system("pause");
    return 0;
    }


    The problem is I can't get the exact value when inputting a base of 4 and exponent of 4.. it gives a result of 128, it should be 256.... can somebody help me please... Thanks

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Take a look at the loop condition. How many times does it run?

    Also, I don't see an initial value for result. You're lucky the result isn't more off than it is.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Also, take at look at your first post (as edited by me), and see how much better the code looks.
    Now read the intro threads, and make your latest effort just as presentable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Now, I got it... Thanks a lot... clap clap clap...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 03:28 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Converting C++ code to C
    By alixi in forum C Programming
    Replies: 3
    Last Post: 09-21-2004, 02:23 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread


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