Create your own function that accepts one input parameter

Assignment Help C/C++ Programming
Reference no: EM13848105

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.

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:

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

Questions Cloud

Conduct research on managerial communication : Conduct research on managerial communication - outside of the textbook, focusing on the questions posed in theoutline:
Create the star schema for the data warehouse : Develop a specific and detailed discussion about the ETL process is to be used to move data from the OLTP environment to the data warehouse - Create a star schema and the DDL to create the Star schema for the data warehouse.
Effective meetings are not only integral to achieve : Effective meetings are not only integral to achieve team goals and successful completion of tasks, but also are reflective overall of team functioning. What are the ways a manager can organize effective productive meetings.
Define components of the acronym smart in goal setting : Identify at least four forces in the general environment of organizations and provide an example of each for an industry or business. How do these forces differ from the external stakeholders in the task environment?
Create your own function that accepts one input parameter : 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.
Problem based on laplace transform : Show transcribed image text Y(s) = ? Discontinuous Forcing Functions: Problem Take the Laplace transform of the following initial value problem and solve for Y(s) = Laplace {y(t)}: y'' - 2y' - 35y = S(t) y(0) = 0, y'(0) = 0
Analysis of real-world situations or problems : Apply the concepts and techniques of the module to the analysis of real-world situations or problems. You will be expected to use diverse sources of information and to carry out an original analysis rather than summarise or rehash existing work.
How can this company use the concepts of trust : Pick any company that has a large number of customers. How can this company use the concepts of "trust, "relationships," and "customer-centricity" mentioned by Don Peppers to become more profitable
Three most defining moments in united states history : What are the three most defining moments in United States History

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Program that uses an instance of the timeoff

Write a program that uses an instance of the TimeOff class you designed in Programming Challenge 5. The program should ask the user to enter the number of months an employee has worked for the company. It should then use the TimeOff object to calc..

  Ansi-c program complete assignment as per written in the

complete assignment as per written in the attached

  Write a program that asks the user to enter the monthly cost

Write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost ..

  Add a copy constructor

Take the code below and add a copy constructor, assignment operator definition and destructor to the class. These functions have already been started for you.

  You are to implement a simple file system with login

you are to implement a simple file system with login authentication and access control. specificallyconstruct a

  Documenting the testing of c program

Writing a test plan and writing a C program with reasonable comments and sensible variable names - Test and documenting the testing of your c program

  Write a program that asks the user for a positive integer

Write a program that asks the user for a positive integer N and generates the Nth Fibonacci number.

  Find the average of four 16-bit unsigned integers

Write a program (starting at address $4000) to find the average of four 16-bit unsigned integers stored at locations $5000, $5002, $5004 and $5006 and store the 16-bit result at location $5010.

  Write a program that accepts as input a string variable

Write a program that accepts as input a string variable called strFirstName, then another string variable called strLastName, and an integer variable called intAge.

  Create a program that would give you the day of the week

Create a program that would give you the day of the week if you entered month/date/year. please show every step.

  What are reasons for using generics

Explain generics in C#. What are reasons for using generics? How do generics apply to collections? Write the code for a small example using C# code

  A constructor and a destructor

A constructor and a destructor, Insert a new element chosen by the user at the correct place in the list

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