Thread: Generating number combinations

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    36

    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.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    When you increment an integer, you "turn" the "odometer" of the 16 or 32 "wheels" (bits), just as you need them, for this program.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    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);
    Last edited by litzkrieg; 02-27-2011 at 09:58 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 02-27-2011 at 10:07 PM.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    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?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    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)

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yep!

    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.

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    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

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by litzkrieg View Post
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    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

  14. #14
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    Adak if i remove the scanf() line how will the program know what the user has entered as far as number of variables?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by litzkrieg View Post
    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
    Last edited by Adak; 02-28-2011 at 12:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number guessing game
    By kazoo in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 11:31 AM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. computing the number of combinations
    By clover in forum C Programming
    Replies: 34
    Last Post: 06-06-2004, 01:12 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

Tags for this Thread