please help me understand this

This is a discussion on please help me understand this within the C Programming forums, part of the General Programming Boards category; ok so i have this code and i must be missing something, im just starting to program and need to ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    36

    please help me understand this

    ok so i have this code and i must be missing something, im just starting to program and need to make a similar program to this but i just cant understand too much of this code can someone run through it with me (like almost line for line after the first {.

    Thank you heres the code its a factorial program
    Sorry for not formatting it right, im fairly new here

    Code:
     .
    
    #include<stdio.h>
    main()
    {
    int n,a;
    int b(int );
    printf("Enter positive number less than 10\n");
    scanf("%d",&n);
    a=b(n);
    printf("\nResults of:   %d",n,a);
    }
    int b(int n)
    {
    int k;
    if(n==1)
    return(1);
    else
    k=n*b(n-1);
    
    return(k);
    }
    .
    ok think i got it right this time

    //*again thank you even just for looking*//
    Last edited by litzkrieg; 02-16-2011 at 12:55 PM. Reason: formatting

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    First, start by reading the forum guidelines, homework policy and "Posting code" section, then come edit your post to use proper code tags and indentation.

    Some tips to start off:
    It's int main(void) and a return 0; at the end.
    Don't put a function prototype inside another function. Move "int b(int );" up above main, or move the whole definition of b up there.

    As for what's happening, have you tried running through this by hand? If you go through line by line, keeping track of all the variables, you'll see what happens when you enter 1, 2, ..., 5 for n, and call b with these values. You'll probably recognize the pattern by that point.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    I'll give you a B- for effort, since you nicely re-edited your post, but forgot to indent the code in your shiny new code tags:
    Code:
    #include<stdio.h>
    int b(int);
    
    int main(void)
    {
        int n,a;
    
        printf("Enter positive number less than 10\n");
        scanf("%d",&n);
        a=b(n);
        printf("\nResults of: %d",n,a);
    
        return 0;
    }
    
    int b(int n)
    {
        int k;
        if(n==1)
            return(1);
        else
            k=n*b(n-1);
    
        return(k);
    }
    Now, given your previous post, I'm guessing you get the bulk of this (unless you copied that from somewhere -- for shame!). Why don't you write down what you think each line does and we'll help you if there's a problem. I'm feeling tempted to lower that effort grade.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    no not trying to copy just trying to fully understand and learn it all, ok well the stuff im still not so sure on are would be:

    1. "int b(int n)"..... does this simply set b equal to n?
    2. "if(n==1)"...... the double equals signs mean what as compared to just "="?
    3. difference between return 0 and return 1?
    4. and variable k? like the printf function is told to display the result as variable a, where does this k from and why is it there? i can see what seems to be the main point of k how its really doing most the work of the program in the line "k=n*b(n-1);" but after "int b(int n)" i get really lost as to whats happening

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by litzkrieg View Post
    no not trying to copy just trying to fully understand and learn it all, ok well the stuff im still not so sure on are would be:

    1. "int b(int n)"..... does this simply set b equal to n?
    2. "if(n==1)"...... the double equals signs mean what as compared to just "="?
    3. difference between return 0 and return 1?
    4. and variable k? like the printf function is told to display the result as variable a, where does this k from and why is it there? i can see what seems to be the main point of k how its really doing most the work of the program in the line "k=n*b(n-1);" but after "int b(int n)" i get really lost as to whats happening
    I'm thinking you need to sit down with the books and this time actually learn C...
    I don't know where you got that code from, but it's getting real obvious you didn't write it.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    No i didnt is from my instructor for us to do our best over the week to understand and write a similar program on Friday really just looking for some understanding

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your instructor wrote that?

    Whew... does he know it contains errors?

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    he wrote it in class and it works so i dont think he cared (and ill be honest no one really likes him or understands his accent so yea no one said anything) an i might have copied some stuff off the paper wrong to lol
    Last edited by litzkrieg; 02-16-2011 at 01:42 PM. Reason: bad grammar

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    So Tater's right. It's Wednesday and you have some time to read your books and some tutorials on functions and recursion in C.

Popular pages Recent additions subscribe to a feed

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