- #1
toforfiltum
- 341
- 4
So here is the question:
On a phone keypad, many of the numbers have letters associated with them. For instance, the letters A, B, and C are associated with the number 2. Write a program that accepts a number as input and prints all of the possible letter combinations associated with that number. For example, if the input is n=23, the possible letter combinations are AD, AE, AF, BD, BE, BF, CD, CE, and CF.
I don't know how to solve this problem, but here is my idea, and the accompanying program. My idea is that since there are just 8 unique numbers, I would go through each number typed by the user, and if it is new, do a form of bitwise operation to record it. Therefore, if the user were to repeat that number again, there is some sort of memory.
Then after that, I would write a program to count the number of 1s and therefore, the different unique numbers entered by user.
Here is where I get stuck. I just can't seem to figure out how to 'store' the different numbers, because I don't know beforehand while writing the program how many the user would enter. And I don't really know how to generate all the combinations though I think a nested for loop could do it.
Anyway, I thought that my idea is way too complicated and should be simpler, since I'm in a beginner programming course. I have not learned arrays yet too. So any ideas on how to tackle this problem is much appreciated.
On a phone keypad, many of the numbers have letters associated with them. For instance, the letters A, B, and C are associated with the number 2. Write a program that accepts a number as input and prints all of the possible letter combinations associated with that number. For example, if the input is n=23, the possible letter combinations are AD, AE, AF, BD, BE, BF, CD, CE, and CF.
I don't know how to solve this problem, but here is my idea, and the accompanying program. My idea is that since there are just 8 unique numbers, I would go through each number typed by the user, and if it is new, do a form of bitwise operation to record it. Therefore, if the user were to repeat that number again, there is some sort of memory.
Then after that, I would write a program to count the number of 1s and therefore, the different unique numbers entered by user.
Here is where I get stuck. I just can't seem to figure out how to 'store' the different numbers, because I don't know beforehand while writing the program how many the user would enter. And I don't really know how to generate all the combinations though I think a nested for loop could do it.
Anyway, I thought that my idea is way too complicated and should be simpler, since I'm in a beginner programming course. I have not learned arrays yet too. So any ideas on how to tackle this problem is much appreciated.
C:
#include <stdio.h>
int main ()
{
int input, i, j, k = 0, count_1 = 0, count_2 = 0, integer, check, monitor = 0;
char alphabets;
printf("Enter number as input: ");
scanf("%d", &input);
if ( input < 50 || input > 57 )
{
printf("Enter number as input: ");
scanf("%d", &input);
}
integer = input;
while ( integer > 0 )
{
check = input % 10;
input = integer / 10;
switch ( check )
{
case 2: monitor | ( 2 * 1 );
break;
case 3 : monitor | ( 2 * 10 );
break;
case 4 : monitor | ( 2 * 100 );
break;
case 5 : monitor | ( 2 * 1000 );
break;
case 6 : monitor | ( 2 * 10000 );
break;
case 7 : monitor | ( 2 * 100000 );
break;
case 8 : monitor | ( 2 * 1000000 );
break;
case 9 : monitor | ( 2 * 10000000 ) ;
break;
}
}
for ( i = 0; i < 8; i++ )
{
if ( i == 0 )
{
power = 1;
}
else
{
for ( j = 1; j <= i; j++ )
{
power *= 10;
}
}
}
while ( k < 7 )
{