Thread: Need some help with my program please.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    26
    One thing that's useful in these sorts of situations is to try to say in words what you would do to figure this out.

    You have successfully written code to get a (nonnegative) number from the user.

    Now, how can you print out all possible combinations of nonnegative numbers (x, y, z) where x+y+z = number?

    Notice first that we don't have to test any combinations where x, y, or z is bigger than the number.

    So, you want to somehow go through all possible combinations of nonnegative integers (x,y,z), where x, y, and z are all between 0 and number, and test to see if x+y+z = number. If it is, then print out x, y, z, and keep a running count of how many such x, y, z you have run across.

    At the end, print out your running count.

    Hopefully that should help!

    As an exercise for the more mathematically inclined, prove that given n >= 0, the number of such triples is C(n+2,2) (the number of ways to choose 2 objects out of n+2 objects).
    Last edited by zzzaaahhh; 10-07-2004 at 04:27 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Smile Thank You. :)

    I really appreciate your advice.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    16
    Umm... Pablo615?

    I executed what you typed, but it didn't work when I entered some values. But you did help me a little. This is what I have so far:

    Code:
    #include <stdio.h>
    
    int main() {
      int i,j,k,num;
      printf("Enter a number: ");
      scanf("%d", &num);
      for(i=0; i<=num; i++) {
                 for (j=0; j<=num; j++){
    	         for (k=num; k>=i+j; k--){
    		 printf("%d %d %d\n", i, j, k);
    }
    }
    }
    
      return 0;
    }
    * I have some problems configuring this program the way I want it. Any advice on what I can do, is greatly appreciate it.*

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    What values were giving you problems? I've run it using lots of values (3, 8, 12, 102, 1212, etc) and I am getting the output I expect.

    In the last loop, remember that the combined values of all 3 variables has to be EQUAL to num. Any other combination is wrong, so your "loop" should reflect that. All thats really needed is two loops, because one the first 2 values are decided, the third value is fixed.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Sorry, I posted bad code above, that was what I was playing with.

    Use this.

    Code:
    int num, count, t1, t2, t3;
      printf("Enter a number: ");
      scanf("%d", &num);
      count = 0;
      for (t1 = 0; t1 <= num; t1++)
      {
        for (t2 = 0; t2 <= num; t2++)
        {
    		/* This can replace the for loop.
    		t3 = num - t1 - t2;
    		if (t3 >= 0)
    		{
    			printf("%d %d %d\n", t1, t2, num - t1 - t2);
    			count++;
    		}
    	}*/
    	  for(t3 = num - t1 - t2; (t3 >= 0); t3--)
             {
                printf("%d %d %d\n", t1, t2, t3);
                count++;
                break;
          }
        }
      }
      printf ("Count = %d\n", count);
      return 0;
    }
    Don't forget to keep a counter for everytime you print. That will keep count for you.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    16
    Ok! The values I plugged in work now. Now I know how I can fix that third for loop. Thank you SO Much for your help!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM