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

#include <iostream> using namespace std; //Function and Variables int getDigit(c

ID: 3537277 • Letter: #

Question

#include <iostream>
using namespace std;




//Function and Variables
int getDigit(char);
int main()
{
// User Instructions
cout << "****************User Instructions***************" << endl;
cout << "This program will write a function that returns" << endl;
cout << "the digit value corresponding to the letter passed" << endl;
cout << "to it as an argument based on the encoding" << endl;
cout << "on your telephone handset. If the argument is not one" << endl;
cout << "of the letters of the alphabet, return a value of -1." << endl;
cout << "************************************************" << endl;
cout << endl;

//More Variables
char letter;
int num;
//Statements
cout<<" Please enter a letter, to exit press *: ";
cin>>letter;
while(letter!='*')
{num=getDigit(letter);
if(num==-1)
cout<<letter<<" umm, go back to Pre-school because that's not a letter!! ";
else
cout<<letter<<" is number "<<num<<endl;
cout<<"Please enter a letter, to exit press *: ";
cin>>letter;
}

return 0;
}
int getDigit(char l)
{l=toupper(l);

//Nested if statement
if(l=='A'||l=='B'||l=='C')
return 2;
else if(l=='D'||l=='E'||l=='F')
return 3;
else if(l=='G'||l=='H'||l=='I')
return 4;
else if(l=='J'||l=='K'||l=='L')
return 5;
else if(l=='M'||l=='N'||l=='O')
return 6;
else if(l=='P'||l=='Q'||l=='R'||l=='S')
return 7;
else if(l=='T'||l=='U'||l=='V')
return 8;
else if(l=='W'||l=='X'||l=='Y'||l=='Z')
return 7;
else
return -1;
}
_______________________________________________________

Please answer these questions using the code above, if not used in code leave blank. thank you

Program inputs?

Program outputs?

Program Algorithm?

Program test plan?

Program test results?

Functions?

Function Analysis?

Function Preconditions and Post conditions?

Function Inputs and Outputs?

Function Formulas?

Function Algorithm?

Explanation / Answer

Program inputs?


First let me tell you, what this program does. And how it works. You must have seen a cell phone or basic telephone. Right?


I am talking about a normal one. Nor qwerty nor like your touch screen phone. A simple basic handset.

How does it keypad look like?


Something like this?



Now look at the second key. It has "2abc" written on it.

What this program does is if you input an alphabet, it ll return the integer value printed on that key. So if you press b, it ll output 2.

If you press c, again the output will be 2. if you press s, then output is 7


To terminate the program, program expects that you enter a *


Say my input is s

Program outputs?


my output would be


****************User Instructions***************


This program will write a function that returns


the digit value corresponding to the letter passed


to it as an argument based on the encoding


on your telephone handset. If the argument is not one


of the letters of the alphabet, return a value of -1.


************************************************




Please enter a letter, to exit press *: s


s is number 7


Please enter a letter, to exit press *: q

q is number 7

Please enter a letter, to exit press *: *








Program Algorithm?

There is no such complex algorithms here. Code is pretty simple.

The only logic applied here is there is one while loop which runs until it encounters a * (star).

if anything else other than an alphabet is entered, then it returns -1.

if any alphabet is entered, corresponding number is returned using nested if else loop.



Program test plan?


Test plan includes of both correct and wrong cases.

So, we have to consider both of them as well.

Correct case will be to check the behaviour of the program when an alphabet is enetred.

Wrong case will be to check the behaviour of the program when any special character or any digit is entered.

Another wrong case would be to press any digit or alphabet but dont press *. In this case, the while loop will never get a termination point and it ll go into infinite loop.




Program test results?



Correct Case

Please enter a letter, to exit press *: s


s is number 7


Please enter a letter, to exit press *: q

q is number 7

Please enter a letter, to exit press *: *








Wrong case (entering a digit)


****************User Instructions***************


This program will write a function that returns


the digit value corresponding to the letter passed


to it as an argument based on the encoding


on your telephone handset. If the argument is not one


of the letters of the alphabet, return a value of -1.


************************************************





Please enter a letter, to exit press *: 1 umm, go back to Pre-school because that's not a letter!!








Wrong case (entering a number but not pressing a *)


****************User Instructions***************


This program will write a function that returns


the digit value corresponding to the letter passed


to it as an argument based on the encoding


on your telephone handset. If the argument is not one


of the letters of the alphabet, return a value of -1.


************************************************




Please enter a letter, to exit press *: 1 umm, go back to Pre-school because that's not a letter!!


Please enter a letter, to exit press *: 1 umm, go back to Pre-school because that's not a letter!!


Please enter a letter, to exit press *: 1 umm, go back to Pre-school because that's not a letter!!


Please enter a letter, to exit press *: 1 umm, go back to Pre-school because that's not a letter!!


Please enter a letter, to exit press *: 1 umm, go back to Pre-school because that's not a letter!!


Please enter a letter, to exit press *: 1 umm, go back to Pre-school because that's not a letter!!


Please enter a letter, to exit press *: 1 umm, go back to Pre-school because that's not a letter!!



Please enter a letter, to exit press *: 1 umm, go back to Pre-school because that's not a letter!!


...


..


.


.


.


This ll go infinite times.





Functions?


There are two functions

1. main function - which is mandatory. Without it, no C++ program can run.

2. int getDigit(char l) function


Function Analysis?

main function does nothing special

Analysis has to be done for second function which is int getDigit(char l)

this function, first of all takes a parameter which is a character.

It changes the character into uppercase. Example if you enter r, it ll be changed into R automatically. For this it uses toUpper() which is inbuilt function to convert lowerCase letters into upperCase.

then it uses nested if else loop and check for match of that particular in the range A to Z, if the match is found, corresponding, number is returned from the function (as the return type is an integer), if no match is found, then -1 is returned.



Function Preconditions and Post conditions?


Character which is passed as the parameter should not be null or empty.

Function Inputs and Outputs?

if character is an alphabet (anything in range of a-z and A-Z, then corresponding integer is returned.)

Example - for s or S or r or R, integer value 7 is returned.

if anything other than a-z and A-Z is put in that character which is passed as parameter, then -1 ll be returned.



Function Formulas?


No formulaes used.

Function Algorithm?



nesting of if else . Searching for the desired element in if statement, if found return the value, if not, go into the first if else statement, if the desired character is found, return the corresponding integer value. If not, go to second if else. And thats how it continues until the character match is found. If no match is found , then else statement will return -1 .