How Can I Use A Scanf to alter combinations?
Write a program that prompts the user for a five digit lab access code, read it in as an unsigned integer then print to the screen each of the five digit access codes (with possible leading zeroes) that are equivalent (there are 32). Be aware that 1 and 2 are on the same button, as are 3 and 4, etc. 9 and 0 are on the same button, as well. The picture below is an example of a key access device that has 0 and 1 on the first button, 2 and 3 on the next button and so on.
#include <stdio.h>
int main(void) {
#define foreach( intpvar, intary ) int* intpvar; for( intpvar=intary; intpvar < (intary + (sizeof(intary)/sizeof(intary[0]))) ; intpvar++)
int a1[]={1,2};
int a2[]={3,4};
int a3[]={5,6};
int a4[]={7,8};
int a5[]={9,0};
foreach (p1, a1) {
foreach (p2, a2) {
foreach (p3, a3) {
foreach (p4, a4) {
foreach (p5, a5) {
printf("%d %d %d %d %d\n",*p1,*p2,*p3,*p4, *p5);
}
}
}
}
}
return 0;
}
since 5 sets of 2 only gives me 10 combinations.
No, it gives you 2^5 = 32 combinations, because you can choose one of two values, five times. Imagine the code is 13579 - the equivalent codes are:
13579
23579
14579
24579
13679
23679
14679
24679
13589
23589
14589
24589
13689
23689
14689
24689
13570
23570
14570
24570
13670
23670
14670
24670
13580
23580
14580
24580
13680
23680
14680
24680
All of these codes map to hitting the five buttons in sequence. Anyhow: your algorithm is basically:
foreach option in group 0
foreach option2 in group 1
foreach option3 in group 2
foreach option4 in group 3
foreach option5 in group 4
print option option2 option3 option4 option5