Thread: increasing series

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    increasing series

    this function run a series of numbers.
    if n=3
    out put is:
    1,1,1
    1,1,2
    1,1,3
    1,2,1
    1,2,2
    1,2,3
    1,3,1
    1,3,2
    1,3,3

    Please help me understand how the increasing process in this function works. I cann't see how the array's elemt increase their values.

    Code:
    void print_num(int n)
    {
    	int times;
    	times=pow(n,2);
    	while (times--)
    	{
    		for(i=0;i<n-1;i++)
    		{
    			printf ("%d,", arr[i]);
    		}
    		printf ("%d\n",arr[i]);
    		if (++arr[n-1]>n)
    		{
    			arr[n-1]=1;
    		
    			for(j=i-1;j>=0;j++)
    				if (++arr[j]>n)
    				arr[j]=1;
    				else
    				break; 
    		}
    	}
    }
    Last edited by ronenk; 07-21-2004 at 01:21 AM.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    if (++arr[n-1]>n)

    ++ is the prefix operator for addition

    before arr[n-1]>n is evaluated ++arr[n-1] is executed and it increases arr[n-1] with 1.

    Take a look at this link
    http://crasseux.com/books/ctutorial/...---and---.html

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    This I know:
    ++ is the prefix operator for addition
    ...and still don't get it.

    From what I see in debugger I can tell the increment process happens in this loop:
    [code]

    for(i=0;i<n-1;i++)
    {
    printf ("%d,", arr[i]);
    }
    printf ("%d\n",arr[i]);

    /[code]

    on the other hand when put this in comment:

    if (++arr[n-1]>n)

    increment process doesn’t happen at all with output of:
    1,1,1
    1,1,1
    1,1,1
    1,1,1
    1,1,1
    1,1,1
    1,1,1
    1,1,1
    1,1,1
    please try to clarify it.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    I'm not sure what you don't understand but..

    if (++arr[n-1]>n)

    ++ has higher precedance then > so it get executed first.

    You can see it like this

    arr[n-1] = arr[n-1] + 1;

    if(arr[n-1] >n )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with this series
    By NoUse in forum C Programming
    Replies: 6
    Last Post: 01-22-2009, 11:57 PM
  2. Cosine - Maclaurin Series
    By carrotcake1029 in forum C Programming
    Replies: 12
    Last Post: 12-07-2008, 12:20 PM
  3. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  4. Problem with implementing Liebniz series
    By C_Necessity in forum C Programming
    Replies: 6
    Last Post: 06-15-2005, 12:39 PM
  5. array of series
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 06-22-2004, 01:06 PM