phone.cpp Use the following structure struct PhoneTone { int rowTone, colTone; }
ID: 3694370 • Letter: P
Question
phone.cpp
Use the following structure
struct PhoneTone
{
int rowTone,
colTone;
};
Use the following table:
Key Row Tone(Hz)
1 2 3 697
4 5 6 770
7 8 9 852
* 0 # 941
Column Tone (hz) 1209 1336 1447
For example, pressing the 8 key generates a row tone at 852 Hz and a column tone at 1336 Hz.
Write a function with a character parameter that returns a PhoneTone structure through the function call that represents the row tone and the column tone generated by pressing that key.
PhoneTone keyToTones(char key);
Test plan: Use the driver program phone.cpp that is found on Angel.
Suggestion: Use switch logic in your function. You should have
2 separate switch statements – one to set the row tone
and one to set the column tone
Explanation / Answer
#include <iostream>
using namespace std;
// Definition of the PhoneTones struct
struct PhoneTones
{
// Frequencies of the tones generated by a key press
uint rowTone;
uint colTone;
};
// Function prototype
PhoneTones keyToTones(char key);
int main()
{
char inputKey; // Input key
PhoneTones keyFreqs; // Frequencies of the corresponding tones
// Read in a series of keys and output the corresponding tones.
for (uint i = 1; i <= 12 ; i++)
{
cout << endl << "Enter key pressed (0-9, *, or #): ";
cin >> inputKey;
keyFreqs = keyToTones(inputKey);
cout << "Tones produced at " << keyFreqs.rowTone << " and " << keyFreqs.colTone << " Hz" << endl;
}
return 0;
}
PhoneTones keyToTones(char key)
{
PhoneTones result;
switch (key)
{
case '1':
case '2':
case '3':
result.rowTone = 697;
break;
case '4':
case '5':
case '6':
result.rowTone = 770;
break;
case '7':
case '8':
case '9':
result.rowTone = 852;
break;
case '*':
case '0':
case '#':
result.rowTone = 941;
break;
default:
return result;
}
if (key == '0')
{
result.colTone = 1336;
return result;
}
else if (key == '*')
{
result.colTone = 1209;
return result;
}
else if (key == '#')
{
result.colTone = 1447;
return result;
}
int num = key - '0';
switch (num % 3)
{
case 1:
result.colTone = 1209;
break;
case 2:
result.colTone = 1336;
break;
case 0:
result.colTone = 1447;
break;
}
return result;
}
Sample Output:
Enter key pressed (0-9, *, or #): 6
Tones produced at 770 and 1447 Hz
Enter key pressed (0-9, *, or #): 9
Tones produced at 852 and 1447 Hz
Enter key pressed (0-9, *, or #): *
Tones produced at 941 and 1209 Hz
Enter key pressed (0-9, *, or #): #
Tones produced at 941 and 1447 Hz
Enter key pressed (0-9, *, or #): 8
Tones produced at 852 and 1336 Hz
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.