![]() |
| | #1 |
| Registered User Join Date: Oct 2002
Posts: 98
| My 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... |
| Morgan is offline | |
| | #2 |
| Bios Raider Join Date: Jul 2002 Location: South Africa
Posts: 765
| Let me look at the code.
__________________ The knack of flying is learning to throw yourself at the ground and miss. |
| biosninja is offline | |
| | #3 |
| Registered User Join Date: Oct 2002
Posts: 98
| 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? |
| Morgan is offline | |
| | #4 |
| Me want cookie! Join Date: Dec 2001
Posts: 680
| 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 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
|
| Monster is offline | |
| | #5 |
| Registered User Join Date: Oct 2002
Posts: 98
| 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... |
| Morgan is offline | |
| | #6 | |
| Me want cookie! Join Date: Dec 2001
Posts: 680
| Quote:
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);
}
| |
| Monster is offline | |
| | #7 |
| Registered User Join Date: Oct 2002
Posts: 98
| 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. |
| Morgan is offline | |
| | #8 |
| Me want cookie! Join Date: Dec 2001
Posts: 680
| Now I understand, sorry 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);
}
|
| Monster is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| libusb headache | cbee | C Programming | 2 | 12-02-2008 08:27 PM |
| Headache error message | Strait | C++ Programming | 6 | 01-31-2005 03:48 PM |
| Recursion = headache; | RoD | C++ Programming | 9 | 12-20-2002 09:34 PM |
| Pointers + Arrays = Headache | Estauns | C++ Programming | 7 | 11-22-2001 06:53 PM |
| Linked List headache | Unregistered | C++ Programming | 0 | 09-23-2001 06:42 PM |