Thread: Generating number combinations

  1. #16
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    i think we are trying to do 2 different things maybe? program should take a number entered by the user (1-5)-the user entered number is the amount of digits total and give and in binary all the combos for ones and zeros.
    ex.
    user enters: "3"
    program prints:
    000
    001
    011
    010
    111
    110
    101
    100.
    user enters "2"
    program would print:
    00
    01
    11
    10

    we still doing the same thing?

  2. #17
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    are we still going for the samething? the digit enter by the user is the number of digits in each combo.
    user enters 3:
    program prints:
    000
    001
    011
    010
    111
    110
    100
    101
    User enters 2
    program prints:
    00
    01
    11
    00
    user enters 1
    program prints:
    0
    1
    it would be the same patter for 4 and 5 but that was alot to write out for a noticeable pattern

    so what i have going so far just needs to be expanded to do more than "2" but also less than 2
    heres what i have working right now it compiles ad runs bu only does what i want for user entering 2
    Code:
    int main(void) {
    
      int wheel1, wheel2, stop;
      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);	 	  
         }		 	 	 
      }												 
      return 0;
    }

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That requires a bit more logic, but not much.

    What would you do to solve this problem?

    Ack! I've been ninja-posted!!

    Let me study what you've just posted, a bit.

    What is the upper limit for the number of digits the user could ever enter?

    Edit: On second thought, you need something more flexible, and "wheels", needs an array. See below
    Last edited by Adak; 02-28-2011 at 03:00 PM.

  4. #19
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    well i actually really like how you put it as like an odometer o ive kinda been thinking like that and i saw how your for loop said wheel<2 only giving it the 2 digits i though about adding like a wheel 3,4,5 but then when it prints i get a long number that makes no sense

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The trick with Gray Code is that for an n+1 bit code you simply enumerate an n bit code forwards with a prepended zero, then backwards with a prepended one. So it's actually:

    000
    001
    011
    010
    110
    111
    101
    100

    See the reverse of the two bit code in red?
    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"

  6. #21
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    ok so only one more issue that i can see now is that with the new code its now doing only user entering 5 correctly but now 1-4 is giving the same results and i understand that it has to do with the %d part of that how can i make that so when 1-4 is entered ill only get 1-4 digits?
    Code:
    #include<stdio.h>
    int main(void) {
    
      int wheel1, wheel2, wheel3, wheel4, wheel5;
      
      printf("Number of variables: ", wheel1);
      
      scanf("%d", &wheel1);
      
      printf("\n Results:\n");
      
      for(wheel1 = 0; wheel1<2;wheel1++) 
        											 
    		for(wheel2 = 0; wheel2<2;wheel2++)
          							 	 
    				for(wheel3 = 0; wheel3<2;wheel3++) 
        											 
    					for(wheel4 = 0; wheel4<2;wheel4++)
    					
    						for(wheel5 = 0; wheel5<2;wheel5++)
    						
    								printf("%d%d%d%d%d\n",wheel1, wheel2, wheel3, wheel4, wheel5);	  	   
    
         	 	 	 	 	 	 	 	 	 
      	  	  	  	  	  	 	 	 	 	 	  
    									 
      return 0;
    }

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Best to throw out that code, and start fresh with something like this:

    Code:
    #include <stdio.h>
    #define SIZE 5
    
    int main(void) {
      int i, j, k, max ;
      int w[SIZE] = { 0 };  //the "wheels"
      int carry[SIZE] = { 0 };
    
      printf("\n\nEnter the number of digits to show [1-5]: ");
      scanf("%d", &max);
      i = j = 0;
      while(1) { 
        k = i;
        if(w[k] > 1) {  
          while(w[k] > 1) {  //handles the "carries" when w[N] > 1
            w[k] = 0;
            carry[++k] = 1;
            w[k] += carry[k];
          }
        }
        if(k > j)  
          ++j;
        if(j == max)
          break;
    
        putchar('\n');
        for(k = j; k > -1; k--)   {
           printf("%d", w[k]);  
        }
        w[i]++;
      }
      printf("\n\n\t\t\t     press enter when ready");
      (void) = getchar();
      return 0;
    }
    Which isn't what you need, but it's close. How would you alter this code to make it print out only when it has the max number of digits, instead of printing up from one digit, all the way up to max digits?

    Which evolves into this:
    Code:
    /* a permutation generator - fixes output at max columns of digits
    (no letters here)
    
    Adak, Feb., 2011
    
    status: OK
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    //#include <dos.h>  //for delay()
    #include <time.h>
    
    int main(void) {
      clock_t start, stop;
      int i, j, k, max;
      int *w;  //the "wheels"
      int *carry; 
    
      printf("\n\nHow many digits do you want displayed [1-5]? ");
      scanf("%d", &max);
      getchar();
      putchar('\n');
      w=calloc(max, sizeof(int));
      carry=calloc(max, sizeof(int));
      if((w==NULL) || carry==NULL) {
        printf("\nError allocating memory\n");
        return 1;
      }
      start = clock();
      i = j = 0;
      while(1) { 
        //delay(70); //getch();
        
        k = i;
        if(w[k] > 1) {  
          while(w[k] > 1) {
            w[k] = 0;
            carry[++k] = 1;
            w[k] += carry[k];
          }
        }
    
        if(k > j)  
          ++j;
        if(j == max)
          break;
    
        if(j < max) {
          putchar('\n');
          for(k = max-1; k > -1; k--)  
            printf("%d", w[k]);  
        }
        w[i]++;
      }
      stop = clock();
      printf("\n\n Elapsed time: %f", (stop - start)/CLOCKS_PER_SEC);  
      printf("\n\n\t\t\t     press enter when ready");
      (void) getchar();
      return 0;
    }
    This isn't a fast way to generate the digits, but it is generally well suited.
    Last edited by Adak; 02-28-2011 at 05:25 PM.

  8. #23
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    Thank you Adak I didn't Turn in your last post in because well that just wouldn't be right but i did kinda use a modified version of the FOR loops one, ended up using a total of 5 loops
    thank you so much for your help i did save your last program and first chance i get (in about 3-4 hours after classes) you can bet im gonna be walking myself through it to fully see how everything works.

    Thank you so much for all your help

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome, Litzkrieg.

    This is one of those programs that are so simple in concept, that you feel like screaming at the computer "Hey!". It takes a bit of tinkering to get it right. The carry[] array helps out more than it seems like it would, imo.

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