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.

Additional part to assignment:

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

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 "
Input menuSelect

If menuSelect == 1 Then
// 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: */

int intValue, menuSelect,Results;

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

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

}

}

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

 

intintValue, menuSelect,Results;

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

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

     }

 

 

    }  

 

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;

}

Reference no: EM13850119

Questions Cloud

A 7 year bond issues by bigtop ltd : James Thompson has been offered a 7 year bond issues by Bigtop Ltd at a price of $943.22. The bond has a coupon rate of 9% and pays the coupon semiannually. Similar bonds in the market will yield 10% per annum. Should he buy the bonds at the offered ..
Rain corp is issuing a 10-year bond with a face value : Rain Corp is issuing a 10-year bond with a face value of $1 000 with a coupon rate of 7 percent. The interest rate for similar bonds is currently 9 percent, compounded annually. Assuming annual payments, what is the present value of the bond? (Round ..
What is the current price of van burens stock : Harrison Corporation is interested in acquiring Van Buren Corporation. Assume that the risk-free rate of interest is 5% and the market risk premium is 5%. Van Buren currently expects to pay a year-end dividend of $1.70 a share (D1 = $1.70). Van Buren..
Value of the warrants : Gregg Company recently issued two types of bonds. The first issue consisted of 20-year straight (no warrants attached) bonds with an 8% annual coupon. The second issue consisted of 20-year bonds with a 7% annual coupon with warrants attached. Both bo..
Function that accepts one input parameter : 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.
Accident victim has received a structured settlement : An accident victim has received a structured settlement. According to the terms of the agreement, the victim will receive $10,000 per year at the end of each year for the next 10 years. Additionally, the victim will receive $20,000 in 10 years.
College costs are rising and saving for college education : College costs are rising and you are saving for the college education of your two children. One child will enter college in 5 years, while the other child will enter college in 7 years. How much will college costs be per year when the children are re..
How to respond to criminal behavior among the youth : Write paper about How to Respond to Criminal Behavior among the Youth
Company a can borrow fixed at 14.8 percent : Company A can borrow fixed at 14.8 percent and floating at LIBOR percent. Company B can borrow fixed at 16.2 percent and floating at LIBOR+ 0.35 percent. A financial intermediary charges a fee of 0.14 percent. Company A wishes to borrow floating and ..

Reviews

Write a Review

Basic Computer Science Questions & Answers

  What kind of watermarking mechanism

What kind of watermarking mechanism can you propose to protect an image copyright?

  Design of free text message communication system

For this task you will have to design a system for text message communication after a major disaster, for example, after a major earthquake, tsunami, or hurricane.

  Determine at least five advantages and five disadvantages o

1. Determine at least five advantages and five disadvantages of a smart card compared to Radio Frequency Identification (RFID) tags. 2. Assume that you are a supervisor for your organization's IT security team. You want to install biometric devices o..

  What is a peer-to-peer network

Explain what is a peer-to-peer network and the advantages and disadvantages a peer-to-peer network has over server based networks.

  Difference between value type and reference

Difference between value type and reference type arguments in c#

  What is primary security risk users acknowledge using macros

Write a 200- to 300-word response that answers the following question: Based on the article by Lenning (2005), what is a primary security risk that users should acknowledge when using macros?

  network structure-Management Information system

Which of the following is true of a network structure? The Management Information system (MIS) structure with one main computer system is called a

  . what options does atrium have for acquiring a new system

Atrium Bio-Medical Supply is a medium sized regional supplier of medical and laboratory equipment.Since starting 12 years ago,Victoria Dawn has built Atrium into a competitive supply company across several metro areas,but her growth potential is l..

  Write a usevehicle class to instantiate one vehicle object

Write a UseVehicle class to instantiate one Vehicle object, one Car object, one MotorCycle object, and display each object's values.

  What ip range would an isp provide to a customer

Using the IP ranges below: a) What IP range would an ISP provide to a customer, if the customer wanted a range of Public IP's for use on theInternet? Explain your choice and why you feel the other choices are not adequate ?

  What is the number of juggling patterns of length

a. What is the number of juggling patterns of length 2 using at most 3 balls? b. Write the stack numbers for the patterns of part (a). c Write the tables for these juggling patterns.

  What is the general solution to handle such game trees

The min max algorithm cannot handle some games (even-with alpha-beta pruning) because of the depth of the search trees involved. What is the general solution to handle such game trees?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd