See above figure which shows numbers-letters association used in phone keys. You
ID: 3763048 • Letter: S
Question
See above figure which shows numbers-letters association used in phone keys. Your task is to
1- Keep those association in an array of struct, such as
A, 2
B, 2
C, 2
D, 3
...
...
Z, 9
You will build your array of struct with hard-coded values.
2- Ask user repeatedly a four-letter combination, and find corresponding numbers for the combination.
Once user enters CSCI
Your program should output 2724. Similarly,
ABCD should be map to 2232
TOMY should be map to 8669
GONE should be map to 4663
Exit the program if user enters EXIT.
Now write a C++ program following above information.
International Standard Key Pad ABC DEF 4 GHI JKL MNO 7 PQRS TUV WXYZ 8 9 0Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
struct key_map
{
// char for each alphabet
char ch;
// int for number on keypad
int n;
};
int main()
{
// To store 4-letter word from user
string input;
// To store output value
int output;
// integers to use in loops
int i, index;
// array of structs
key_map arr_key_map[] = {
{'A', 2},
{'B', 2},
{'C', 2},
{'D', 3},
{'E', 3},
{'F', 3},
{'G', 4},
{'H', 4},
{'I', 4},
{'J', 5},
{'K', 5},
{'L', 5},
{'M', 6},
{'N', 6},
{'O', 6},
{'P', 7},
{'Q', 7},
{'R', 7},
{'S', 7},
{'T', 8},
{'U', 8},
{'V', 8},
{'W', 9},
{'X', 9},
{'Y', 9},
{'Z', 9} };
// Ask user for 4-letter word
cout << "Enter a four letter word : ";
// Store the 4-letter word
cin >> input;
// Loop until EXIT word is received from user
while(input != "EXIT")
{
// initialize output value to 0
output = 0;
// Loop over each letter of the word
for(i=0;i<4;i++)
{
// search for the letter in the array of structs
for(index = 0;index<26;index++)
{
if(input[i]==arr_key_map[index].ch)
{
// multiply output by 10 and then add current letter's number
/* To append 5 to 3, we need to multiply 3 by 10 and then add 5
output = 3
output = 3*10 = 30
output = 30+5 = 35
*/
output *= 10;
output += arr_key_map[index].n;
// beak out of for loop once the match is found
break;
}
}
}
// print the output value
cout << "The Output is " << output << " ";
cout << "Enter a four letter word : ";
cin >> input;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.