+1(978)310-4246 credencewriters@gmail.com

Overview
This hands-on lab allows you to follow and experiment with the critical steps of developing a program
including the program description, analysis, test plan, design, and implementation with C code. The
example provided uses sequential, repetition, selection statements and user-defined functions. Program Description
Write a program that will allow the user to select from a variety of mathematical functions, evaluate
that function at a numerical value, and keep going until the user enters a selection indicating that the
user is done with the program.
The program should be written in such a way that adding new functions to the source code is relatively
easy. Interaction
A menu will be presented to the user as a prompt. The exact menu will depend upon the actual
functions currently available in the program. Here is one typical interaction, where a, b, c, etc. represent
function selections.
>Make a selection:
a: a(x) = x*x
b: b(x) = x*x*x
c: c(x) = x^2 + 2*x + 7
q: quit
Enter selection and value, or q: a 3.45
c(3.45) = 3.45^2 + 2*3.45 + 7 = 25.80
Analysis
1. Use float data types for input, calculations and results.
2. Create a main input loop to select a function or quit.
3. Create a good prompt, including a list of possible selections to help the user.
4. Include appropriate printf functions within the math functions.
5. Make the main method simple by using a while loop, and a menu function which will return a
special number if the user chooses the quit option.
a. while (menu () == 0);
b. if the menu function returns a 0 then continue
c. returning any other value will end the program.
6. Use the ^ symbol in the display to indicate a power of x, i.e., x^4 means x*x*x*x.
7. Write a simple message when the program is over, such as “… bye …”.CMIS 102 Hands-On Lab
Week 7 – Functions
Overview
This hands-on lab allows you to follow and experiment with the critical steps of developing a program
including the program description, analysis, test plan, design, and implementation with C code. The
example provided uses sequential, repetition, selection statements and user-defined functions.
Program Description
Write a program that will allow the user to select from a variety of mathematical functions, evaluate
that function at a numerical value, and keep going until the user enters a selection indicating that the
user is done with the program.
The program should be written in such a way that adding new functions to the source code is relatively
easy.
Interaction
A menu will be presented to the user as a prompt. The exact menu will depend upon the actual
functions currently available in the program. Here is one typical interaction, where a, b, c, etc. represent
function selections.
>Make a selection:
a: a(x) = x*x
b: b(x) = x*x*x
c: c(x) = x^2 + 2*x + 7
q: quit
Enter selection and value, or q: a 3.45
c(3.45) = 3.45^2 + 2*3.45 + 7 = 25.80
Analysis
1.
2.
3.
4.
5.
Use float data types for input, calculations and results.
Create a main input loop to select a function or quit.
Create a good prompt, including a list of possible selections to help the user.
Include appropriate printf functions within the math functions.
Make the main method simple by using a while loop, and a menu function which will return a
special number if the user chooses the quit option.
a. while (menu () == 0);
b. if the menu function returns a 0 then continue
c. returning any other value will end the program.
6. Use the ^ symbol in the display to indicate a power of x, i.e., x^4 means x*x*x*x.
7. Write a simple message when the program is over, such as “… bye …”.
Page 1 of 6
Test Plan
To verify this program is working properly the following input values and expected outputs could be
used for testing:
Test Case
1
2
3
4
5
Input
a3
b5
c1
a 2.2
q
Expected Output
a(3) = 3^2 = 9
b(5) = 5^3 = 125
c(1) = 1*1 + 2*1 + 7 = 10
a(2.2) = 2.2^2 = 4.84
… bye …
Pseudocode
main
write “bye”
end main
declare selection as char
declare x as float
printHelp
input selection
if (selection == ‘q’) return 1
input x
if (selection == ‘a’) a(x)
// other selections
return 0 // continue
void printHelp
write “a: a(x) = x*x”
// other function selections
write “q: quit”
end printHelp
void a(float x)
declare v as float
v = x*x;
write “a(x) = x*x = v” // replace x and v with values
end function a(x)
// other functions
Page 2 of 6
C Code
The following is the C Code that will compile and execute in the online compilers.
The header comment block is not shown allowing the new code to fit here on one page.
#include
void printHelp () {
printf (“n”);
printf (“a: a(x) = x*xn”);
printf (“b: b(x) = x*x*xn”);
printf (“c: c(x) = x^2 + 2*x + 7n”);
printf (“q: quitn”);
}
void a(float x) {
float v = x*x;
printf (” a(%.2f) = %.2f^2 = %.2fn”, x, x, v);
} // end function a
void b(float x) {
float v = x*x*x;
printf (” b(%.2f) = %.2f^3 = %.2fn”, x, x, v);
} // end function b
void c(float x) {
float v = x*x + 2*x + 7;
printf (” c(%.2f) = %.2f^2 + 2*%.2f + 7 = %.2fn”,
x, x, x, v);
} // end function c
char selection;
float x;
printHelp ();
scanf (“%s”, &selection);
if (selection == ‘q’) return 1;
scanf (“%f”, &x);
if (selection == ‘a’) a(x);
if (selection == ‘b’) b(x);
if (selection == ‘c’) c(x);
return 0;
int main() {
printf (“… bye …n”);
return 0;
} // end main
Page 3 of 6
Test Run
Note the Input values for this run were:
a 3.3
b 4
c 5.5
q
You can change these values to any valid selections and values to match your test cases.
Here are the results from running the program with the input above, the user input is shown in red:
a: a(x) =
b: b(x) =
c: c(x) =
q: quit
a 3.3
a(3.30)
x*x
x*x*x
x^2 + 2*x + 7
a: a(x) =
b: b(x) =
c: c(x) =
q: quit
b 4
a(4.00)
x*x
x*x*x
x^2 + 2*x + 7
a: a(x) =
b: b(x) =
c: c(x) =
q: quit
c 5.5
c(5.50)
x*x
x*x*x
x^2 + 2*x + 7
a: a(x)
b: b(x)
c: c(x)
q: quit
q
… bye
= 3.30^2 = 10.89
= 4.00^3 = 64.00
= 5.50^2 + 2*5.50 + 7 = 48.25
= x*x
= x*x*x
= x^2 + 2*x + 7

Page 4 of 6
Learning Exercises for you to complete
1. Demonstrate you successfully followed the steps in this lab by preparing screen captures of you
running the lab as specified in the Instructions above.
2. (x/2) Create a new function, shrink (float x), that would divide the input value by 2
using the shown code as a template. Support your experimentation with screen captures of
executing the new code.
3. Prepare a new test table with at least 3 distinct test cases listing input and expected output for
the new program you created for the shrink function.
4. What would have to be changed in the code if the while statement were changed to:
5. Modify the original code and add an additional function of your choice. The function should be
unique and something you created for this assignment. Support your experimentation with
screen captures of executing the new code. Prepare a test table with at least 3 distinct test cases
listing input and expected output for your unique function.
6. (Optional) Consider creating functions using functions available in the math.h library, such as
sinf, cosf, tanf, expf, and logf (f’s for float data types). An example, including changing
float v = sinf (x / 180 * M_PI);
See: http://www.cplusplus.com/reference/cmath/
Page 5 of 6
Submission
Points
Demonstrates the successful execution of this Lab within an online compiler. Provides
20
supporting screen captures.
Modifies the code to create an application that includes a function named “shrink” that
20
would take a number and calculates that value divided by 2. Supports your experimentation
with screen captures of executing the new code.
Prepares a new test table with at least 3 distinct test cases listing input and expected output
10
for the new program you created for the shrink function.
Accurately describes all changes needed to program to work correctly if the while
10
statement were changed to: while (menu ( ) == 5); . Supports your argument with screen
captures of executing the new code.
30
your experimentation with screen captures of executing the new code. Prepares a test table
with at least 3 distinct test cases, listing input and expected output for your unique
function.
Document is well-organized, and contains minimal spelling and grammatical errors.
10
Total
100
Page 6 of 6