Program to find combination of letters associated with phone

  • Context:
  • Thread starter Thread starter toforfiltum
  • Start date Start date
  • Tags Tags
    Combination Program
Click For Summary

Discussion Overview

The discussion revolves around creating a program that generates all possible letter combinations associated with a number input on a phone keypad. Participants explore various programming approaches, challenges faced in implementation, and the conceptual understanding of the problem, particularly in the context of beginner programming.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests using bitwise operations to track unique numbers entered by the user but expresses uncertainty about how to store these numbers and generate combinations.
  • Another participant points out that the current code does not effectively store or utilize the results of bitwise operations, indicating a flaw in the logic.
  • A different participant mentions the challenge of generating combinations without using strings or arrays, suggesting that standard library functions could complicate the process for beginners.
  • One participant proposes that recursion might be a more suitable approach for generating combinations, highlighting the exponential growth of output as a hint towards this method.
  • Another participant provides a code snippet that illustrates how to format the input number as a string and recursively generate combinations based on the characters associated with each digit.

Areas of Agreement / Disagreement

Participants express various ideas and approaches, but there is no consensus on a single solution or method. Multiple competing views remain regarding the best way to implement the program and handle the challenges presented.

Contextual Notes

Participants note limitations in their understanding of programming concepts such as arrays and recursion, which may affect their ability to implement solutions effectively. There is also mention of the need for clarity in output ordering based on the input.

Who May Find This Useful

This discussion may be useful for beginner programmers interested in algorithm design, particularly in generating combinations and handling user input in programming languages like C.

toforfiltum
Messages
341
Reaction score
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.

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 )
{
 
Technology news on Phys.org
toforfiltum said:
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.

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 )
{
I don't see any way to do this without using strings of three characters, and being able to pick the individual characters out of them. You wouldn't necessarily have to use array indexing to get the characters, as there are functions in the C standard library header (string.h) that could be used. Unfortunately, using them is probably more difficult than learning how to access elements of an array.

A lot of your code doesn't do anything useful.
C:
switch ( check )
   {
       case 2: monitor | ( 2 * 1 );
       break;
       // <snip>
The expression monitor | (2 * 1) is evaluated, but is not stored anywhere, and not printed, nor is anything done with it. Including this expression has exactly the same effect as not including it.
 
Mark44 said:
I don't see any way to do this without using strings of three characters, and being able to pick the individual characters out of them. You wouldn't necessarily have to use array indexing to get the characters, as there are functions in the C standard library header (string.h) that could be used. Unfortunately, using them is probably more difficult than learning how to access elements of an array.

A lot of your code doesn't do anything useful.
C:
switch ( check )
   {
       case 2: monitor | ( 2 * 1 );
       break;
       // <snip>
The expression monitor | (2 * 1) is evaluated, but is not stored anywhere, and not printed, nor is anything done with it. Including this expression has exactly the same effect as not including it.
That's precisely the problem I had with my idea, I could not store those expressions because while writing the program, I do not know beforehand how many unique numbers the user would enter, thus I can't create a variable to store it. Do I make sense? I hope I do.
 
You are generating output that is exponentially larger than the input. That's a hint that the task is best handled using recursion.
Also, if you want the output in the order indicated, you're in some trouble since the %10 naturally leads to AD, BD, CD, AE, BE, CE ordering.
You could try something like
C:
  ...
  int input;
  char buffer[13]; //13 should be enough for a 32-bit int
  //...input number into "input"
  sprintf(buffer, "%d", input);
  printRest(buffer, 0);
 } //end of program

void printRest(char buffer[13], int position)
 {
  //on input ("23",0) call itself with ("A3",1) then with ("B3",1)
    //then with ("C3",1) then restore the buffer to "23"
  //on input ("A3",1) call itself with ("AD",2) and so on
  //on input ("AD",2) print the buffer
 }
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
Replies
47
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
18
Views
3K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K