Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function that returns the digit value (an integer) corresponding to the

ID: 3650436 • Letter: W

Question

Write a function that returns the digit value (an integer) corresponding to the letter passed to it an argument based on the encoding on your telephone handset. For example, if the argument is the letter a, b, or c (uppercase or lowercase), your function should return the digit 2. If the argument is not one of the letters of the alphabet, return a value of -1. Implement two versions of the function: one using a switch statement and one using a nested if statement. Provide a program which tests each function (you may provide two separate source code files or combine them in one source code file which gives you the choice of which function to run).

Explanation / Answer

Please rate...

Program:

========================================================

#include<iostream>
using namespace std;
int funcIf(char);
int funcSwitch(char);
int main()
{
    char a;
    cout<<"Enter the letter: ";
    cin>>a;
    int a1=funcIf(a);
    int a2=funcSwitch(a);
    cout<<" Using if statement, value is: "<<a1;
    cout<<" Using Switch statement, value is: "<<a2;
}
int funcIf(char a)
{
    if(a>='a' && a<='c')return 2;
    else if(a>='d' && a<='f')return 3;
    else if(a>='g' && a<='i')return 4;
    else if(a>='j' && a<='l')return 5;
    else if(a>='m' && a<='o')return 6;
    else if(a>='p' && a<='s')return 7;
    else if(a>='t' && a<='v')return 8;
    else if(a>='w' && a<='z')return 9;
    else return -1;
}
int funcSwitch(char a)
{
    switch(a)
    {
        case 'a':case 'b':case 'c':return 2;break;
        case 'd':case 'e':case 'f':return 3;break;
        case 'g':case 'h':case 'i':return 4;break;
        case 'j':case 'k':case 'l':return 5;break;
        case 'm':case 'n':case 'o':return 6;break;
        case 'p':case 'q':case 'r': case 's':return 7;break;
        case 't':case 'u':case 'v':return 8;break;
        case 'w':case 'x':case 'y': case 'z':return 9;break;
        default:return -1;
    }
}

========================================================

Sample output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote