Thread: Need some help...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    1

    Need some help...

    I found a program, and am very new to C programming.... I can't make sense of the last line in the program, to change it to an equation....


    Code:
    int p(int n)
    {
        return 1;
    }
    
    int n(int x)
    {
        if (x >= 0) return n(x-5) + p(x);
        return 0;
    }
    
    int d(int x)
    {
        if (x >= 0) return d(x-10) + n(x);
        return 0;
    }
    
    int q(int x)
    {
        if (x >= 0) return q(x-25) + d(x);
        return 0;
    }
    
    int c(int x)
    {
        if (x >= 0) return c(x-50) + q(x);
        return 0;
    }
    
    main()
    {
        int i;
        
        for (i = 1; i < 101; i++)
     printf("%d: %d\n", i, c(i));
    }
    This is more of a math question than a C programming question, but I need to know the true significance of the line:
    printf("%d: %d\n", i, c(i));

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    >printf("%d: %d\n", i, c(i));
    printf is a function that formats data all nice and pretty to standard output (usually the terminal). The double quoted part is the format string, it tells printf how you want things to look. In this case you want a decimal integer followed by a colon, then a space and another decimal integer, then a line break. %d means decimal signed integer, and \n means a newline. It's the same as if you hit return on the command line. The number of arguments past the format string matches the number of formats (things that start with % in the format string). The arguments will replace the formats with their value, so the first %d is replaced by i and the second %d is replaced by c(i).

    It's kind of tough to get a feel for things, but once you do it all makes sense in a convoluted sort of way. But that just the way programmers are...convoluted.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed