### Function that accepts one input parameter

Assignment Help Basic Computer Science
##### Reference no: EM13850119

The Assignment:

Using Pseudocode, create your own function that accepts one input parameter and returns a float number. You decide the theme.

You should provide the pseudocode and an example call. Be sure to provide an overview of what your function is doing.

LAB

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 (using pseudocode visualization), and implementation with 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 program will prompt theuser 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 will 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 calculations 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 numbers: IntValue, MenuSelect, Results to store the 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

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 MenuSelect=N/A Program exits

Pseudocode:

// This program will provide options for a user to calculate the square

// or cube of a positive Integer input by a user.
// Start Main program
Main

// Declare variables

// Set intValue to positive value to start loop
Set intVal = 1;

// Loop While input is a positive number

While intValue > 0

Print "Enter a positive Integer to calculate the square or cube; OR a negative Integer to exit the program. "
Input intValue

// Only perform menu and function calls is integer is positive
If intValue > 0 Then
Print "Enter 1 to calculate Square, 2 to Calculate Cube "

// Call the Square Function
Set Results = Square(intValue)
Print intValue,Results
Else If menuSelect == 2 Then
// Call the Cube function
set Results = Cube(intValue)
Print intValue,Results
Else
Print "Invalid menu item, only 1 or 2 is accepted"
End If
End If

END While

// End of Main program
End Program

// Square Function
Function Square(value) as Integer
Set Square = value*value
End Function

// Cube Function
Function Cube(value) as Integer
Set Cube = value*value*value
End Function

C Code

The following is the C Code that will compile in execute in the online compilers.

// 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 <stdio.h>

int main ()

{

/* variable definition: */

intValue = 1;

// While a positive number

while (intValue > 0)

{

printf ("Enter a positive Integer to calculate the square cube or cube; OR a negative Integer to exit the program.\n: ");

scanf("%d", &intValue);

if (intValue > 0)

{

printf ("Enter 1 to calculate Square, 2 to Calculate Cube \n: ");

{

// Call the Square Function

Results = Square(intValue);

printf("Square of %d is %d\n",intValue,Results);

}

{

// 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");

}

}

return 0;

}

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

Results from running the programming at ideone.com:

Learning Exercises for you to try:

Using the Square and Cube functions are models, how would create a function name Shrink that would take an Integer and return the Integer divided by 2?

What would happen if we removed the following code from our design?

If intValue > 0

(Hint: You can try in the C code, or walk through it in the Pseudocode to see what happens.)

What happens if you entered a 4 for the menuSelect variable?

Try to modify the design and add and use an additional function. Describe your approach. What issues did you face?

CODE

// 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 <stdio.h>

int main ()

{

/* variable definition: */

intValue = 1;

// While a positive number

while (intValue> 0)

{

printf ("Enter a positive Integer to calculate the square or cube; OR a negative Integer to exit the program. \n: ");

scanf("%d", &intValue);

if (intValue> 0)

{

printf ("Enter 1 to calculate Square, 2 to Calculate Cube \n: ");

{

// Call the Square Function

Results = Square(intValue);

printf("Square of %d is %d\n",intValue,Results);

}

{

// 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");

}

}

return 0;

}

/* 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;

}

#### Benefits for transport-layer security applications

Next, explain the risks and benefits of applications that use Public Key Cryptography to encrypt files or messages (such as PGP) and the risks and benefits for transport-layer

#### Estimate the portion of the surface of the sphere

Estimate the portion of the surface of the sphere that can be covered by solar cells. The solar irradiation can be assumed to be 1420 W/m2 of surface perpendicular to the ray

#### Marginal corporate tax rate

For MAMA, the required return on debt is 7%, the required return on equity is 7.8%, and the marginal corporate tax rate is 32%. To the nearest \$1000, the NPV of this investm

#### Calculate the surface area

Implement a class IceCreamCone with methods getSurfaceArea() and getVolume(). In the constructor, supply the height and radius of the cone. Be careful when looking up the fo

#### Er diagram, normalization, relational structure only

The proprietors of BigM have approached you and asked if you could design a database to help them manage their business. The management has commissioned you (in your capacit

#### Define a class poly that represents a polygon

Define a class Poly that represents a polygon but checks that its points really do make a polygon in its constructor. Hint: You'll have to supply the points to the construct

#### Organizational of infrastructure and security

The last section of the project plan will present the infrastructure in accordance with the parameters set forth at the outset of the project. The network solution that is c

#### Explaining statement for security policy

Write a statement for security policy for the following:Let LAN for small 100-person business, Pixel Inc. Business occupies one floor in office building. Everybody has a co

### Write a Review 