Starting from:
$35

$29

Lab 4 Solution

Each lab will begin with a recap of last lab and a brief demonstration by the TAs for the core concepts examined in this lab. As such, this document will not serve to tell you everything the TAs will in the demo. It is highly encouraged that you ask questions and take notes. In order to get credit for the lab, you need to be checked off by the end of lab. For non-zero labs, you can earn a maximum of 3 points for lab work completed outside of lab time, but you must finish the lab before the next lab. For extenuating circumstance, contact your lab TAs and Instructor.

(2 pts) Demo

Follow along with your TAs as they show you how to construct a function for determining if a string contains an ‘a’ character just like we did during lecture on Friday.

Recall:

int checkforA(string test)

Input: a string

Output: return true ​//​if it has an ‘a’, false otherwise

On Friday we used this logic to determine if we had to reprompt the user for a string.

By the end of demo you should be able to identify (6) things: a parameter, an argument, a return type, a function call, a function declaration, and a function definition.

(2 pts) Design atoi

atoi() is a common function which takes a character and returns its decimal ASCII value. Start by designing how this function will work. It should take any character found on the ASCII chart (​http://www.asciitable.com/​) and return the decimal value.

/*********************************************************************

    • Function: a_to_i

    • Description: turns a character into a decimal value

    • Parameters: char character

    • Pre-Conditions: the input is a character

    • Post-Conditions: returned the decimal value of the character

*********************************************************************/

(2 pts) Implement atoi

Write the a_to_i() function based on your design. Test your function thoroughly. Will your function properly return the decimal value of: ‘A’, ‘1’, ‘b’, ‘/’, ‘ ‘, etc.?
(2 pts) Design itoa

Similar to a_to_i(), i_to_a() takes an integer and returns the character associated with that value. Design this function.

/*********************************************************************

    • Function: i_to_q

    • Description: turns a decimal value into a character value

    • Parameters: int decimal

    • Pre-Conditions: the input is an integer

    • Post-Conditions: returned the character which represents the decimal value

*********************************************************************/

(2 pts) Implement itoa

Write the i_to_a() function. Test your function thoroughly. Will it return the correct character value for 127, 65, 97, etc.?
For this exercise, you can assume that the input will not be less than 0 or greater than 127.




If you finish your lab early, it is recommended that you stay and work on your Assignment 3 to get feedback from your TAs.

More products