Thread: Assigning values to functions (Newbie)

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    30

    Assigning values to functions (Newbie)

    I have just started learning C and I already have some issues.

    Code:
    #include <stdio.h>
    
    int binom(int n, int k)
    {
    int result=1;
    int i;
    if(n == k) return 1;
    if(k == 0) return 1;
    for(i=n;i>n-k;i--)
    result=result*i;
    for(i=1;i<=k;++i)
    result=result/i;
    return result;
    }
    
    
    int main(void)
    {
    int bnm;
    int n, k;
    for(n=0;n<10;n++){
    for(k=0;k<=n;k++){
    bnm=binom(n,k);
    printf("%ld ",bnm);
    }
    printf("\n");
    }
    return 0;
    }
    I understand that variables "n" and "k" from "main" and "binom" function are different but does the line:
    Code:
    bnm=binom(n,k);
    mean it will assign "n" value from "main" function to "binom" function? (Same applies to "k")
    I know this is stupid question, and yes I googled it but I havent found a decent answer. Thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by turke92 View Post
    does the line:
    Code:
    bnm=binom(n,k);
    mean it will assign "n" value from "main" function to "binom" function? (Same applies to "k")
    What you would say in programming-speak is: n and k are submitted as arguments to binom, and the result is assigned to bnm.

    I know this is stupid question, and yes I googled it but I havent found a decent answer. Thanks.
    If you are unsure what something does, get as many hints as you can and then start playing around to see what happens. If you have a theory about what you think is happening, try to prove and disprove it.

    Finally: LEARN TO INDENT. STARTING NOW. There are a bunch of examples in C for various "indent styles" here:

    Indent style - Wikipedia, the free encyclopedia

    Pick one (variants on K&R or Allman are most popular) and use it! This is an important part of keeping code comprehensible to yourself and others.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    Thanks!
    One more question. I don't understand the logic behind this... In the "main" function we keep incrementing n and k at the same rate until n=9.
    Shouldn't that keep the ratio of n and k equal (n==k) and therefore always return 1?
    I know I got something wrong but I'm not sure what...

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by turke92 View Post
    Thanks!
    One more question. I don't understand the logic behind this... In the "main" function we keep incrementing n and k at the same rate until n=9.
    Shouldn't that keep the ratio of n and k equal (n==k) and therefore always return 1?
    I know I got something wrong but I'm not sure what...
    If you would learn to indent your code properly you would actually be able to see what is happening...

    Code:
    #include <stdio.h>
    
    int binom(int n, int k)
    {
       int result=1;
       int i;
    
       if(n == k) 
         return 1;
    
       if(k == 0) 
         return 1;
    
       for(i=n;i>n-k;i--)
         result=result*i;
       for(i=1;i<=k;++i)
         result=result/i;
       return result;
    }
    
    
    int main(void)
    {
       int bnm;
       int n, k;
    
       for(n=0;n<10;n++) 
         {
           for(k=0;k<=n;k++)
             {
               bnm=binom(n,k);
               printf("%ld ",bnm);
             }
           printf("\n");
        }
       return 0;
    }
    See it now? the k loop is inside the n loop... for each value of n you call binom with every value of k.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    So that means that after testing if n<10 is true the program solves the "binom" function and prints it, increments n, goes into a new line, and after all that it looks if k<=n and does the orders in its brackets? I'm sorry if you find it difficult to understand my stupid question regarding this simple issue.

    EDIT: "See it now? the k loop is inside the n loop... for each value of n you call binom with every value of k." I think I speak english well but this statement confused me and hence my even more confusing question.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Here try this little code snippet...
    Code:
    #include <stdio.h>
    
    int main (void)
      { int i, j;
    
        for (i = 0; i < 5; i++)
           for (j = 0; j< i; j++)
              printf( " i = %d, j = %d \t",i,j);
    
       getchar();
       return 0;
    }
    Get it now?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    So initialization of "j" (j = 0) is made every time if the statement "j < i" is false and initialization of "i" is done only once?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by turke92 View Post
    So initialization of "j" (j = 0) is made every time if the statement "j < i" is false and initialization of "i" is done only once?
    Initialization is done only at the beginning of a for() loop... any for() loop, every for() loop...

    You have two loops, one running inside the other...
    Look at the way the numbers count up...

    Now apply that to your original code...

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    I got it but I haven't said what I meant right.
    I thought that the value of j stays saved if we get back to the first loop, I didn't know that after we get back to the second loop initialization is made again.

    Thanks for helping!

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No j is not saved because you don't go back to the outer loop until the inner one is finished and each time you re-enter the inner loop it's starting over... the numerical printout from my example should have demonstrated that very clearly.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    Like I said, I got it now, but didn't know this when I encountered the code on the 1st post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about assigning values
    By DCMann2 in forum C Programming
    Replies: 7
    Last Post: 04-18-2008, 07:36 AM
  2. assigning values to constructor
    By sujeet1 in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2007, 09:34 AM
  3. assigning values
    By alex_6169 in forum C Programming
    Replies: 7
    Last Post: 05-15-2006, 02:16 AM
  4. Re-assigning random values
    By Prophet-ex in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2005, 10:27 AM
  5. Assigning Values in Structs
    By kirby1024 in forum C Programming
    Replies: 2
    Last Post: 06-02-2002, 02:21 AM

Tags for this Thread