• 11-29-2002
Morgan
This problem is giving me a real headache...

Using two input variables, number_of_units and max_number, produce this number sequence...

e.g.
number_of_units = 3;
max_number = 5;

...must produce this number sequence...

1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

It must work for any input values ( though number_of_units is always less than max_number ).

I thought it would be easy, but I've tried so hard and failed that I can't event think straight anymore!! Can anyone help?

Thanks...
• 11-29-2002
biosninja
Let me look at the code.
• 11-29-2002
Morgan
It's not so much the coding that I have a problem with as the logic. I've had a couple of aborted attempts at dynamically declaring an array of ints, then performing a number of nested loops incrementing each int in the array when appropriate, but all I've done is got myself confused...!!

How would you approach the problem?
• 11-29-2002
Monster
Code:

```A B C ------ 1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5```
A runs from 1 till number_of_unit
B runs from A+1 till max_number-1
C runs from B+1 till max_number
Code:

```A = 1 WHILE A <= NUMBER_OF_UNITS DO   B = A + 1   WHILE B < MAX_NUMBER DO       C = B + 1       WHILE C <= MAX_NUMBER DO         PRINT A B C         C = C + 1       END WHILE       B = B + 1   END WHILE   A = A + 1 END WHILE```
• 11-30-2002
Morgan
Thanks Monster, but it's not that easy, because it has to work for any value of number_of_units and max_number. As one nested loop is required for each unit, the code would have to be created dynamically during runtime...

I've tried using an array of integers, created dynamically at runtime, then used the approach you've sketched out, but with an outer loop cycling for each integer in the array. The code is quite basic, but I cant keep track of which integer is doing what when I write it, and it all falls to pieces...

Any other ideas...
• 12-02-2002
Monster
Quote:

Originally posted by Morgan
Thanks Monster, but it's not that easy, because it has to work for any value of number_of_units and max_number. As one nested loop is required for each unit, the code would have to be created dynamically during runtime...

I've tried using an array of integers, created dynamically at runtime, then used the approach you've sketched out, but with an outer loop cycling for each integer in the array. The code is quite basic, but I cant keep track of which integer is doing what when I write it, and it all falls to pieces...

Any other ideas...

And why does my pseudocode not work for any value of max_number? Just change the MAX_NUMBER value.
Code:

```#include <stdio.h> #include <stdlib.h> int print_numbers(int number_of_units, int max_number) {   int a, b, c;   if(number_of_units >= max_number)       return -1;   for(a = 1; a <= number_of_units; a++)       for(b = a+1; b < max_number; b++)         for(c = b+1; c <= max_number; c++)             printf("%d %d %d\n", a, b, c);   return 0; } int main(int argc, char *argv[]) {   return print_numbers(3, 6); }```
• 12-02-2002
Morgan
Sorry, I've obviously not phrased the problem well enough.

If number_of_units=4
and max_number=6

the number sequence would start like this...

1 2 3 4
1 2 3 5
1 2 3 6
1 2 4 5
1 2 4 6
etc

so using a set number of nested loops won't work. Maybe this is more of a logic problem than a C problem... sorry.
• 12-03-2002
Monster
Now I understand, sorry :D

You can solve this with a recursive function. Here a quick (and dirty) solution:
Code:

```#include <stdio.h> #include <stdlib.h> #include <string.h> int print_numbers(int col, int nr, int max_col, int max_nr, char *line) {   char line2[BUFSIZ];   char number[BUFSIZ];   if(col <= max_col)   {       for(++col; nr <= max_nr; nr++)       {         sprintf(line2, "%s%d ", line, nr);         print_numbers(col, nr+1, max_col, max_nr, line2);       }   }   else       printf("%s\n", line);   return 0; } int main(int argc, char *argv[]) {   char line[BUFSIZ] = {0};   return print_numbers(1, 1, 4, 6, line); }```
The code is buggy (no dynamic allocation of buffer) but it might give you some ideas how to solve your problem.