# Generating number combinations

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-27-2011
litzkrieg
Generating number combinations
so i need to generate the numbers for the rows on a k-map for a user selected up to 10 total variables meaning 5 digits on the row headers, EX. user enters "4" for the total amount of variables and the program would display:
"00
01
11
00"

and im not so much confused on the programing side of it but more how to design a program to do this just looking for some helping ideas.
Is there maybe a way for the ser to enter the max number 5 and have the program generate all possible binary combinations for 5 binary bits? thats kinda what i have thinking so far
thanks guys.
• 02-27-2011
whiteflags
If I understand correctly, you prompt for a specific number, then take that number of bits and show all combinations. If that's right then you have to compute all numbers from 0 up to 2^n -1 in binary. So, write a decimal to binary conversion function and then compute all the necessary numbers.
• 02-27-2011
Think of the odometer in a car - smallest "wheel" is turning up to 9, then it resets to 0, and turns the digit just to it's left, up by one number.

Your "odometer" will only go to 1, before resetting and incrementing the digit on it's left hand side. You could simulate this with two for loops, nested together, quite nicely - but you don't have to. Think of the binary numbering system. Each integer will have 16 or 32 "wheels", that it uses, ready for your use.

We call them bits. :D

When you increment an integer, you "turn" the "odometer" of the 16 or 32 "wheels" (bits), just as you need them, for this program.
• 02-27-2011
litzkrieg
so how could i use those for loops, ive been messing around for a bit now with ++ lines to incrment by one each time but just cant get it, how would those be stated, this is sad but this is what i have so far after a million and one failed attemps, how could i use those for loops though?

here is what i have
Code:

``` #include<stdio.h> int main(void) { char a; printf("Number of Variables in Each Row?\n"); scanf("%c", &a);```
• 02-27-2011
Code:

```#include .... int main(void) {   int wheel1, wheel2;   for(wheel1 = 0; wheel1<2;wheel1++) {    //start of the first for loop for wheel1     //add your second for loop for wheel 2 right here         //now print out your wheel1 and wheel2 variables     } //end of second for loop   } //end of first for loop   getchar(); //hold the console screen open   return 0; } In practice, I'd never put a lowercase l next to a 1 in a variable, because they look too much alike. i'd use "w1, w2 ", etc.```
• 02-27-2011
litzkrieg
ok so then now it starts to make a bit more sense but he only line im confused on would be the:
Code:

`getchar(); //hold the console screen open`
line im not sure how to fully use that command, am i trying to tell it o retrieve wheel1 and wheel2?
• 02-27-2011
No. That is used ONLY to hold the console window open, so you can see the result before it closes.

If your IDE already holds the console window open for you, delete it.
• 02-27-2011
litzkrieg
ok then just one more and i think i can totally get the rest
the 2nd for loop would look like this right?
Code:

`for(wheel2 = 0; wheel2<2;wheel2++)`
and thats incrementing wheel2 in the correct way right?

(THANK YOU so much for all your help sometimes its not the programming side but its the lack of experience of knowing what to do to fix a situation best)
• 02-27-2011
Yep! :p

Step through your program and see if it's incrementing OK or not. What is it printing out?

You're welcome, and quite right. A lot of programming is solving problems.
• 02-27-2011
litzkrieg
ok well maybe because its late where i am but i can get it to compile and im just not sure but does that for loop statement really take care of all the combinations? i guess now that ive gotten to work on it before bed a bit im still confused on how to correctly format this.
here is what i have
Code:

``` #include<stdio.h> #include<math.h> int main(void) {   int wheel1, wheel2;   printf("Variable");   scanf("%d", &wheel1);   wheel1=wheel2;     for(wheel1 = 0; wheel1<2;wheel1++) {                                                                                                                                for(wheel2 = 0; wheel2<2;wheel2++)                                                                                                       printf("Results",wheel1, wheel2);                      }                                                                }                            }                                                                                                                                                                          return 0; }```
Night everyone and again thank you SO much Adak you really helped and everytime i visit this site i learn something new and look forward to another vist
• 02-27-2011
Code:

```#include<math.h> int main(void) {   int wheel1, wheel2;   printf("Variable");   scanf("%d", &wheel1);   wheel1=wheel2;     for(wheel1 = 0; wheel1<2;wheel1++) {                                                                                                                                for(wheel2 = 0; wheel2<2;wheel2++)                                                                                                       printf("Results",wheel1, wheel2);                      }                                                                }                            }                                                                                                                                                                          return 0; }```
A few suggestions:

Replace <math.h> with <stdio.h> basic arithmetic is included in stdio.h

scanf() line of code - you won't need it AFAIK, delete it.

wheel1=wheel2 - delete it, for loops assign the right values, when they begin.

change:
printf("Results",wheel1, wheel2) to:
printf("%d%d\n",wheel1, wheel2);

Before the first for loop, add:
printf("\n Results:\n");

And that should fix it up.
• 02-28-2011
iMalc
Quote:

Originally Posted by litzkrieg
00
01
11
00

There must be a typo there as you have two 00's.
Do you want the output in increasing binary order (00, 01, 10, 11),
or do you want gray code order (00, 01, 11, 10) which what you typed looks closest to?
• 02-28-2011
litzkrieg
yea sorry that was meant to be 00, 01, 11, 10 and i guess i would choose gray code considering it only changes by one bit each time
• 02-28-2011
litzkrieg
Adak if i remove the scanf() line how will the program know what the user has entered as far as number of variables?
• 02-28-2011
Quote:

Originally Posted by litzkrieg
Adak if i remove the scanf() line how will the program know what the user has entered as far as number of variables?

Using bits or wheels, (either one), you're limited to 2 X the number of bits or wheels that you're using.

For instance, you can't have 16 numbers in bits, if you're only using 2 bits (same with wheels if wheels are representing those bits and limited to 0 or 1).

If you want all numbers less than 100, you can change your code to base 10 logic.

Code:

```int main(void) {   int wheel1, wheel2, stop, num=0;   printf("How many number combinations do you want [1-99] ");   scanf("%d", &stop);   printf("\nResults: \n==============\n");      for(wheel1 = 0; wheel1<10;wheel1++) {                        for(wheel2 = 0; wheel2<10;wheel2++)       printf("%d%d\n",wheel1, wheel2);                        if(++num > stop)         break;     }                                      if(num>stop)       break;   }                                                                                                  return 0; }```
Which will produce:
Results:
============
00
01
02
. . .
09
10
11
. . .
99

If stop equals 100
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last