C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-29-2002, 08:54 AM   #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...
Morgan is offline   Reply With Quote
Old 11-29-2002, 08:58 AM   #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.
biosninja is offline   Reply With Quote
Old 11-29-2002, 09:01 AM   #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   Reply With Quote
Old 11-29-2002, 10:25 AM   #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
Monster is offline   Reply With Quote
Old 11-30-2002, 08:50 AM   #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   Reply With Quote
Old 12-02-2002, 03:32 AM   #6
Me want cookie!
 
Monster's Avatar
 
Join Date: Dec 2001
Posts: 680
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);
}
Monster is offline   Reply With Quote
Old 12-02-2002, 09:45 AM   #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   Reply With Quote
Old 12-03-2002, 03:33 AM   #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.
Monster is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:54 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22