Problem solving and algorithm design

1 CMIS 102 Hands -On Lab Week 6 Overview: This hands -on lab allows you to follow and experiment with the critical steps of developing a program including the program description, Analysis, Design( program design, pseudocode ), Test Plan, and implementation wit h C code. The example provided uses sequential, repetition, selection statements and two user -defined function. Program Description: This program will provide options for a user to calculate the square or cube of a positive Integer input by a user. The pr ogram will prompt the user to enter an Integer and then prompt the user if they want to calculate the square of the cube of the number. Based on the inputs of the user, the program will output the square of the cube of the positive integer. The program wil l then print the Integer and square or cube of the integer based on the user’s original choice. The program will continue to prompt the user for Integers and their calculation choice until the user enters a negative integer. The square and cube calculation s should be calculated using a function. Analysis: I will use sequential, selection, and repetition programming statements and functions for the cube and square calculations. I will define three Integer variables : IntValue, MenuSelect, Results to store th e Integer value input by the user, the Menu selection (1 for Square, 2 for Cube) of the user, and the results of the Square or Cube functions. The Square function will take one Integer as input and return one Integer as the output. The calculation within the Square function is: Results = IntValue * IntValue For example, if 10 was entered as the IntValue. Results = 10*10 = 100 The Cube function will take one Integer as input and return one Integer as the output. The calculation within the Cube function is: Results = IntValue * IntValue*IntValue For example, if 10 was entered as the IntValue. Results = 10*10*10 = 1000 A repetition loop can be used to loop through iterations until a negative is entered: while(intValue > 0) ( … End For 2 Progra m Design: Main // This program will provide options for a user to calculate the square // or cube of a positive Integer input by a user. // Declare variables // Initialize loop variable intValue to positive value to start loop // Loop While input is a positive number //Prompt user for a number //Get user response // Only perform menu and function calls i f integer is positive If intValue > 0 Then //Prompt user for selection Square or Cube // "Ente r 1 to calculate Square, 2 to Calculate Cube " If menuSelect == 1 Then // Call the Square Function //Print results Else If menuSelect == 2 Then // Call the Cube function //Print results Else //Print Invalid msg End If //End of If menuSelect End If //End of If intValue > 0 //END While End // End of Main program // Square Function ------------------------------ //Calculates the square of an Integer // Cube Function ------------------------------ //Calculates the cubeof an Integer Test Plan: To verify this program is working properly the input values could be used for testing: Test Case Input Expected Output 1 IntValue=10 MenuSelect=1 Square of 10 is 100 2 IntValue=10 MenuSelect=2 Cube of 10 is 1000 3 intValue= -1 Program exits 3 MenuSelect=N/A Pseudocode: Main // This program will provide options for a user to calculate the square // or cube of a positive Integer input by a user. // Declare variables Declare intValue, menuSelect,Results as Integer // Set intValue to positive value to start loop Set intVal = 1; // Loop While input is a positive number While intValue > 0 //Prompt user for a number Pri nt "Enter a positive Integer , Enter -1 to exit: :” Input intValue // Only perform menu and function calls i f integer is positive If intValue > 0 Then //Prompt user for selection Square or Cube Print "Enter 1 to calc ulate Square, 2 to C alculate Cube: " Input menuSelect If menuSelect == 1 Then // Call the Square Function Set Results = Square(intValue) Print (“The sqaure of “ + intValue ) Print (“ is: “ + Results + ) Else If menuSelect == 2 Then // Call the Cube function set Results = Cube(intValue) Print (The cube of “ + intValue ) Print (“ is: “ + Results +) Else Print “Invalid menu item, only 1 or 2 is accepted” End If //End of If menuSelect End If //End of If intValue > 0 END While End // End of Main program 4 // Square Function ---------------------- -------- Function Square(Integer value) as Integer //This function calculates the square of an integer //Input: value //Output: Square Set Square = value*value Return (Square) End Function // Cube Function ------------------------------ Function Cube(Integer value) as Integer //This function calculates the cube of an integer //Input: value //Output: Cube Set Cube = value*value*value Return (Cube) End Function C Code The following is the C Code that will compile in execute in the online co mpilers. // C code // This program will provide options for a user to calculate the square // or cube of a positive Integer input by a user. // Developer: Faculty CMIS102 // Date: Jan 31, 2014 #include // -- the newer C compilers require that functions be proto type d // -- this tells the compiler what the input and output datatypes of the functions are // -- the functions are later defined after the main. // function prototypes int Square ( int ); int Cube ( int ); int main () { /* variabl e definition: */ int intValue, menuSelect, Results; intValue = 1; // While a positive number while (intValue > 0) 5 { printf ("Enter a positive Integer , Enter 0 or neg. no. to exit \n: "); scanf("%d", &intValue); if (intValue > 0) { pri ntf ("Enter 1 to calculate Square, 2 to Calculate Cube \n: "); scanf("%d", &menuSelect); if (menuSelect == 1) { // Call the Square Function Results = Square(intValue); printf("Square of %d is %d \n",intValue,Results); } else if (menuSelect == 2) { // Call the Cube function Results = Cube(intValue); printf("Cube of %d is %d \n",intValue,Results); } else printf("Invalid menu item, only 1 or 2 is accepted \n"); } //E nd If (intValue >0) } //End WHile return 0; } //end of main /*********************************************/ /* function returning the Square of a number */ int Square(int value) { return value*value; } /* function returning the Cube of a number */ int Cube(int value) { return value*value*value; } Setting up the code and the input parameters in ideone.com: Note the Input values for this run were: 10 1 10 2 -99 You can change these values to any valid integer values to match your test cases. 6 Results from running the programming at ideone.com: 7 Learning Exercises for you to try: 1. Modify the original code and u sing the Square and Cube functions a s models, create a new function name d Divide2 that would take an Integer input and return s a Float value of the input Integer divided by 2? Note this should be a float function. Take care when you prototype youir function. Add this as menu option 3 to the program to execute this function. Also include in menu option 3, the di splay of the results from this function. Make you have the datatypes correct for your variables. Support your experimentation with screen captures of executing the new code. 2. Modify the original code and create a new function of your own choice. This functi on should be unique and something you created for this assignment. Add this as menu option 4 to the program to execute this function. Your new function should have at least one argument input and return a calculated value to the main. You should also in t he main display that returning value. Support your experimentation with screen captures of executing the new code. Make sure your datatypes of the variables/function are correct. Submit code a s a separate .txt (or .c ) file that includes all four menu opt ions. 3. Prepare a new test table with at least 3 distinct test cases listing input and expected output for the code you created after step 2 . 4. What would happen if we didn’t have t he following test condition. I.e remove the following code from our design? If intValue > 0 { 8 } //End IF intValue > 0 What happens if you entered a 0 for the menuSelect variable? (Hint: You can try in the C code, or walk through it in the Pseudocode to see what happens.) 9 Grading guidelines Submission Points No 1. Modifies the original code and create s a new function named D ivide2 that takes an Integer and returns Float value of the input divided by 2 . Support your experimentation with screen captures of executing the new code. 3 No 2 . Modifies th e original code and adds a new unique function of your own choice. Adds this as menu option 4 to the program to execute this function. Support your experimentation with screen captures of executing the new code. Submits code as a separate .txt (or .c ) fil e. 4 No 3 . Provides a new test table with at least 3 distinct test cases listing input and expected output for t he code you created after step 2 . 1 No 4 . Describes what would happen if you removed the “ if intValue =0“ line was removed. And what happe ns if you entered a 0 for the menuSelect variable . Support your argument with screen capture s of executing the new code . 1 Document is well organized, and contains minimal spelling and grammatical errors. 1 Total 10