Thread: Need some help with my program please.

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

    Need some help with my program please.

    Our professor gave us an assignment but I'm having some trouble with it:

    ASSIGNMENT 3


    We can define a triple as a sequence of three non-negative numbers. Here are some examples of triples:


    8 2 4 6 0 9 9 0 6 12 7 7 3 38 10


    Write a program that prompts the user for an integer, then prints all possible triples that add up to that number and a count of how many triples there are.



    For example, if the user enters 3, the program prints



    0 0 3

    0 1 2

    0 2 1

    0 3 0

    1 0 2

    1 1 1

    1 2 0

    2 0 1

    2 1 0

    3 0 0


    count: 10


    Hint: use three nested loops.



    Here's what I have so far:

    Code:
    #include <stdio.h>
    
    int main() {
      int i,j,num;
      printf("Enter a number: ");
      scanf("%d", &num);
      for(i=0; i<num; i++) {
    		for (j=0; j<num; j++)
    			printf("%d %d %d\n", i, j, num);
    
      }
    
      return 0;
    }
    * I'm stuck at this point because I can't figure out how to make it so that when a user is asked to enter an integer, the program can print out possible integers to add up to that number & keep count. Any advice that anybody can give is greatly appreciated.*


    Thank You So Much!
    Last edited by agentxx04; 10-07-2004 at 04:04 PM.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    EDIT: Bad code removed. My bad

    I tested it with a few values and it was working.

    The last for loop can be replaced with an if statement, but you said 3 loops.
    Last edited by pablo615; 10-08-2004 at 01:59 PM.

  3. #3
    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.

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

    Smile Thank You. :)

    I really appreciate your advice.

  5. #5
    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.*

  6. #6
    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.

  7. #7
    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.

  8. #8
    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