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.