Thread: Help with using nested For

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

    Help with using nested For

    I have to design a program where

    input > any positive integer
    output > all combos of 3 integers whose sum add up to input

    using 3 nested for loops, number1 <= number2 <= number3 and number1, number2, number3 != 0.

    I had it to the point where it would print all the possible combos for the 1st number = 1, and then I was working on getting it to increase the 1st number by 1, print those combos, and then loop it. Now, I'm back to square 1 where I can't get it to print any output.

    Any help appreciated.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post your code and someone will help you
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( about100bucks; iwill(); doyourhomework() )
        for( about100more; iwill(); stopopenlymockingyou( ) );
    That'll get you started.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    LOLOL, Quzah you are too funny!!!

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    this is what I have so far. I'm not getting all possible combinations.


    Code:
    
    #include <stdio.h>
    
    main()
    
     {
    
    	int N, number1, number2, number3;	/* initializes 4 integer variables */
    
    	printf("Input a positive integer: ");	/* asks for an integer input, reads the inputted integer, */
    	scanf("%d", &N);			/* and stores into N 					  */
    
    	number1 = 1;	/* initializes number1 to 1 */
    	number2 = 1;	/* initializes number2 to 1 */
    
    	for ( ; number1 < N, number1 != 0 ; number1++ )		/* for loop making sure number1 is less than N but not 0 and then incrementing number1 */
    		
    			for ( ; number2 >= number1, number2 < N ; number2++) /* for loop making sure number 2 is greater than or equal to number1 */
    									     /* and less than N, and incrementing number2 */
    				
    			{	
    				for ( number3 = 1 ; number3 >= number2, number3 < N ; number3++ )	/* for loop setting number3 to 1, checking number3 greater than or equal to number2 */
    													/* and less than N and incrementing number3 	*/
    													
    					if( number1 + number2 + number3 == N && number1 <= number2 && number2 <= number3) /* if number1 + number2 + number3 = N if number1 <= number2 <= number3 */
    					printf("%d + %d + %d == %d\n", number1, number2, number3, N);			  /* then print "number1 + number2 + number3 = N */
    					
    						number1++;
    			}
    	
    	return 0;
      }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    for ( ; number1 < N, number1 != 0 ; number1++ )
    It appears that you are using the comma operator where you intend to use the && operator. But when you take a closer look, you find that it is redundant anyway.

    I'll forgo the user input, which could use some FAQ touches, and just pick a number at random.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       unsigned int a, b, c, n = 9;
       for ( a = 1 ; a < n ; ++a )
          for ( b = a ; b < n ; ++b )
             for ( c = b ; c < n ; ++c )
                if ( a + b + c == n )
                   printf("%u + %u + %u == %d\n", a, b, c, n);
       return 0;
    }
    [edit]
    Deleted closing comment about incompleteness of solution.
    Changed variables to unsigned ints.
    Changed loop initializations to meet requirement that
    Code:
    number1 <= number2 <= number3
    [/edit]
    Last edited by Dave_Sinkula; 09-30-2003 at 07:37 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  4. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 10:16 AM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM