Week 7 Functions Solution

\$30.00

Category:

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:

1. a(x) = x*x

1. b(x) = x*x*x

1. c(x) = x^2 + 2*x + 7

1. 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.

1. Create a main input loop to select a function or quit.

1. Create a good prompt, including a list of possible selections to help the user.

1. Include appropriate printf functions within the math functions.

1. 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.

1. while (menu () == 0);

1. if the menu function returns a 0 then continue

1. returning any other value will end the program.

1. Use the ^ symbol in the display to indicate a power of x, i.e., x^4 means x*x*x*x.

1. 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 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

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 <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

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:

1. 3.3

1. 4

1. 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:

1. a(x) = x*x

1. b(x) = x*x*x

1. c(x) = x^2 + 2*x + 7

1. quit

a 3.3

a(3.30) = 3.30^2 = 10.89

1. a(x) = x*x

1. b(x) = x*x*x

1. c(x) = x^2 + 2*x + 7

1. quit b 4

a(4.00) = 4.00^3 = 64.00

1. a(x) = x*x

1. b(x) = x*x*x

1. c(x) = x^2 + 2*x + 7

1. quit

c 5.5

c(5.50) = 5.50^2 + 2*5.50 + 7 = 48.25

1. a(x) = x*x

1. b(x) = x*x*x

1. c(x) = x^2 + 2*x + 7

1. quit

q

… bye …

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.

1. (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.

1. 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.

1. What would have to be changed in the code if the while statement were changed to: while (menu == 5);

1. 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.

1. (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