Thread: Can someone explain this program to me?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    27

    Can someone explain this program to me?

    Code:
    #include <stdio.h>
    
    int left(int a);
    int right(int b);
    
    int main(void)
    {
    
    int n = 1, x = 1;
    
    	while( n < 50 )
    		{
    			printf("%3d, %3d\n", n, x);
    				x = left( n );
    				n = right( x );
    		}
    	printf("%3d, %3d\n", n, x);
    }
    	int left(int a)
    		{
    			return (a * 2);
    		}
    	int right(int b)
    		{
    			return (b * 3);
    		}
    Code:
     Output:
    1,   1
    6,   2
    36,  12
    216,  72
    Press any key to continue . . .
    I'm trying to understand how this function program works. I dont understand how 'n' increases to be more than 50, but from I can tell by messing around with the program, is that when the printf in the 'while' is taken out, the program only prints the last set of numbers '216, 72'. I see the pattern with the numbers, but I have no clue how the program does it.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Both x and n increase when they are assigned the values returned by the functions left() and right(). A greater value is produced in that function and is then assigned to either x or n in main.

    For example:

    x = left(n) = n*2 = 2.

    Then n = right(x) = x * 3 = 2 * 3 = 6.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jaja009
    I'm trying to understand how this function program works.
    For starters, you might want to indent the program properly, e.g.,
    Code:
    #include <stdio.h>
    
    int left(int a);
    int right(int b);
    
    int main(void)
    {
        int n = 1, x = 1;
    
        while( n < 50 )
        {
            printf("%3d, %3d\n", n, x);
            x = left( n );
            n = right( x );
        }
        printf("%3d, %3d\n", n, x);
    }
    
    int left(int a)
    {
        return (a * 2);
    }
    
    int right(int b)
    {
        return (b * 3);
    }
    Quote Originally Posted by jaja009
    I dont understand how 'n' increases to be more than 50
    Notice that left returns twice its argument and right returns thrice its argument. Therefore, on each iteration of the loop, x is assigned a value twice that of n, then n is assigned a value thrice that of the new value of x, i.e., 6 times of its current value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Ok, I think I understand it now. So there arent any real values for a & b, and instead left and right represent 2 & 3.
    So basically in order, the program goes,

    The program prints "1 , 1" for the values of " n, x" which are defined outside the 'while'
    It then calculates x = (2)(1), where left (a) returns as 2
    n = (3)(2), where right (b) returns as 3
    The program prints "6 , 2" for the values of " n, x" which are now defined within the 'while'
    It then calculates x = (2)(6)
    n = (3)(12), where 12 comes from the 2 * 6
    The program prints "36 , 12"
    It then calculates x = (2)(36)
    n = (3)(72)
    The program prints " 216 , 72"

    Is that a correct interpretation?

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes, remember argument names are just generic names for the function. It is their call that determines their actual value inside the function. So by calling left(x) you are basically indicating to the function to use the value of x for a.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM