Starting from:

$25+

CMIS 102 Lab 7 – Functions Overview Solved


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

                 

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

                 

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

 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

 

 

       

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) = x*x b: b(x) = x*x*x c: c(x) = x^2 + 2*x + 7 q: quit a 3.3   a(3.30) = 3.30^2 = 10.89

 a: a(x) = x*x b: b(x) = x*x*x c: c(x) = x^2 + 2*x + 7 q: quit b 4   a(4.00) = 4.00^3 = 64.00

 a: a(x) = x*x b: b(x) = x*x*x c: c(x) = x^2 + 2*x + 7 q: quit c 5.5

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

 a: a(x) = x*x b: b(x) = x*x*x c: c(x) = x^2 + 2*x + 7 q: quit q

... bye ...

                 

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: while (menu == 5);

 

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 degrees to radians:

  float v = sinf (x / 180 * M_PI);

See: http://www.cplusplus.com/reference/cmath/

 

More products