Description
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(x) = x*x
-
b(x) = x*x*x
-
c(x) = x^2 + 2*x + 7
-
quit
Enter selection and value, or q: a 3.45 c(3.45) = 3.45^2 + 2*3.45 + 7 = 25.80
Analysis
-
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.
-
-
while (menu () == 0);
-
-
-
if the menu function returns a 0 then continue
-
-
-
returning any other value will end the program.
-
-
Use the ^ symbol in the display to indicate a power of x, i.e., x^4 means x*x*x*x.
-
Write a simple message when the program is over, such as “… bye …”.
Page 1 of 6
To verify this program is working properly the following input values and expected outputs could be used for testing:
-
Test Case
Input
Expected Output
1
a 3
a(3) = 3^2 = 9
2
b 5
b(5) = 5^3 = 125
3
c 1
c(1) = 1*1 + 2*1 + 7 = 10
4
a 2.2
a(2.2) = 2.2^2 = 4.84
5
q
… bye …
Pseudocode
main
while (menu == 0)
write “bye”
end main
int menu
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
end menu
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
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 <stdio.h>
void printHelp () {
printf (“\n”);
printf (“a: a(x) = x*x\n”);
printf (“b: b(x) = x*x*x\n”);
printf (“c: c(x) = x^2 + 2*x + 7\n”);
printf (“q: quit\n”);
}
void a(float x) {
float v = x*x;
printf (” a(%.2f) = %.2f^2 = %.2f\n”, x, x, v); } // end function a
void b(float x) {
float v = x*x*x;
printf (” b(%.2f) = %.2f^3 = %.2f\n”, 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 = %.2f\n”, x, x, x, v);
} // end function c
int menu () {
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;
} // end function menu
int main() {
while (menu() == 0);
printf (“… bye …\n”);
return 0;
} // end main
Page 3 of 6
Note the Input values for this run were:
-
3.3
-
4
-
5.5
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(x) = x*x
-
b(x) = x*x*x
-
c(x) = x^2 + 2*x + 7
-
quit
a 3.3
a(3.30) = 3.30^2 = 10.89
-
a(x) = x*x
-
b(x) = x*x*x
-
c(x) = x^2 + 2*x + 7
-
quit b 4
a(4.00) = 4.00^3 = 64.00
-
a(x) = x*x
-
b(x) = x*x*x
-
c(x) = x^2 + 2*x + 7
-
quit
c 5.5
c(5.50) = 5.50^2 + 2*5.50 + 7 = 48.25
-
a(x) = x*x
-
b(x) = x*x*x
-
c(x) = x^2 + 2*x + 7
-
quit
q
… bye …
Page 4 of 6
Learning Exercises for you to complete
-
Demonstrate you successfully followed the steps in this lab by preparing screen captures of you running the lab as specified in the Instructions above.
-
(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.
-
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.
-
What would have to be changed in the code if the while statement were changed to: while (menu == 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.
-
(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
degrees to radians:
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. |
|
Modifies the original code and adds an additional unique function of your choice. Supports |
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