# Thread: Need some help with my program please.

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

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

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

4. ## Thank You. :)

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