minor problem in a code

This is a discussion on minor problem in a code within the C Programming forums, part of the General Programming Boards category; hello guys im sorry for asking so much some codes in K&R are complicated Code: #include <stdio.h> /* test power ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    minor problem in a code

    hello guys im sorry for asking so much some codes in K&R are complicated
    Code:
    #include <stdio.h>
    /* test power function */
    int power(int m, int n);
    /* power: raise base to n-th power; n >= 0 */
    int power(int base, int n)
    {
        int i, p;
        p = 1;
        for (i = 1; i <= n; ++i)/*here i=0 i<=n i++
                                 *like lets say we do 1 for int n it will run from 1 till num*/
            p *= base;//p=p * base;
        return p;
    }
    int main(void)
    {
        int i;
        for (i = 0; i < 10; ++i)
            printf("%d %d %d\n", i, power(2,i), power(-3,i));
        getchar();
        return 0;
    }
    here we passed i then power(2,i) and power(-3,i));
    in our orginal function power he set p=1 so if we passed base that means p=1 *2
    shouldnt this alawys 1*num=num ? or what coz it gets diff results like first
    set is 1 ,1 shoudlnt it be 2,-3 ??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The first value should be 0, 1, 1, then 1, 2, -3, then 2, 4, 9, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    but n is the 2nd parameter base is the first so it will be alawys 2 or -3 ?
    and since he intilized the p to 1 it will alawys p=p*2; or p=p*-3; so
    1=1*2; or 1=1*-3 since he didnt make * n right ?

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    but n is the 2nd parameter base is the first so it will be alawys 2 or -3 ?
    and since he intilized the p to 1 it will alawys p=p*2; or p=p*-3; so
    1=1*2; or 1=1*-3 since he didnt make * n right ?
    but look p is changing in the loop.for eg i=5, then base =2,n=5 so the loop in the power function will change p=p*base 5 times,i mean first p=1 then p=2,then p=4 and so on.and yes you're right base never changes,but p.i think u got me.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    how p changes we didnt write any code to make it change ? we just wrote it to be =1 we didnt after that make in the for loop p=i ?so how does it = to i in this program ? its so confusing

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    how p changes we didnt write any code to make it change ? we just wrote it to be =1 we didnt after that make in the for loop p=i ?so how does it = to i in this program ? its so confusing
    just see as i told you let n=5,then in the for loop inside power function when i=1 then p=p*base makes p=1*2(for the case base=2),which is =2 so new value of p=2,then i increments to 2,then p=p*base makes p=2*2=4(new value of p),again i increments to 3,then p=p*base gives p=4*2=8(new value of p) and so on.this way p changes continuosly until i becomes 6.am i understood?

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i understand now thanks

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    like each time it will get a new value from the loop lets say we made i=2 and and power by 2 it will run 2 times whish will get the value 4 lolz ........ stupid me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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