Thread: My headache

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    98

    My headache

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

  2. #2
    Bios Raider biosninja's Avatar
    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.

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

  4. #4
    Me want cookie! Monster's Avatar
    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
    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

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

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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);
    }

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

  8. #8
    Me want cookie! Monster's Avatar
    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);
    }
    The code is buggy (no dynamic allocation of buffer) but it might give you some ideas how to solve your problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. libusb headache
    By cbee in forum C Programming
    Replies: 2
    Last Post: 12-02-2008, 08:27 PM
  2. Headache error message
    By Strait in forum C++ Programming
    Replies: 6
    Last Post: 01-31-2005, 03:48 PM
  3. Recursion = headache;
    By RoD in forum C++ Programming
    Replies: 9
    Last Post: 12-20-2002, 09:34 PM
  4. Pointers + Arrays = Headache
    By Estauns in forum C++ Programming
    Replies: 7
    Last Post: 11-22-2001, 06:53 PM
  5. Linked List headache
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 09-23-2001, 06:42 PM