Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Week 7 - Functions Overview CMIS 102 Hands-On Lab This hands-on lab allows you to follow and experiment with the critical steps of developing a

Week 7 - Functions

Overview

CMIS 102 Hands-On Lab

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.

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

    2. if the menu function returns a 0 then continue

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

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 

Test Case

Input

Expected Output

1

a3

a(3) = 3^2 = 9

2

b5

b(5) = 5^3 = 125

3

c1

c(1) = 1*1 + 2*1 + 7 = 10

4

a 2.2

a(2.2) = 2.2^2 = 4.84

5

q

... bye ...

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 (" "); printf ("a: a(x) = x*x "); printf ("b: b(x) = x*x*x "); printf ("c: c(x) = x^2 + 2*x + 7 "); printf ("q: quit "); 

}

void a(float x) { float v = x*x; printf (" a(%.2f) = %.2f^2 = %.2f ", x, x, v); 
} // end function a 
void b(float x) { float v = x*x*x; printf (" a(%.2f) = %.2f^3 = %.2f ", 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 ", 
 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 ... "); return 0; 

} // end main

Page 3 of 6

Test Run

Note the Input values for this run were:

a 3.3 b4 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 b4

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

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. Prepare a new test table with at least 3 distinct test cases listing input and expected output for the new code you created for the shrink function.

  3. Why is the number of xs different in the printf statements in the functions?

  4. What happens if the user makes a selection other than one of the ones shown in the help

    prompt (a, b, c, or q in the code shown)? Justify your answer with documented experiments.

  5. What would have to be changed in the code if the while statement were changed to:

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

  7. (Optional) Consider creating functions using functions available in the math.h library, such as sinf, cosf, tanf, expf, and logf (fs 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/

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

Fresent Value of en Aanwiff of s1 at Conipeonel laterest

Answered: 1 week ago