Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Programming Language Expert

Pass Task 2.4: My Functions

Overview

Procedures are a great tool for capturing the instructions needed to perform a task, but some- times you need to be able to capture the instructions needed to calculate a value. Using functions you can now create artefacts to encapsulate the steps needed to calculate a value.

Purpose: Learn how to create your own functions.

Task: Use the following instructions to implement a function and use it to calculate some values based on user input.

Instructions

In programming, functions are used to calculate a value. They are very similar to procedures, with the added feature that they return a result when they end. For example, the following program uses the Pi, Sqr and Round functions to calculate the area of a circle.

program CircleArea;
uses TerminalUserInput;

procedure Main();
var
cirRadius, cirArea: Single; roundedValue: Integer;
begin
cirRadius := ReadInteger('Enter a radius: '); cirArea := Pi() * Sqr(cirRadius); WriteLn('Circles area is ', cirArea:4:2); roundedValue := Round(cirArea);
WriteLn('Which rounded off has a value of ', roundedValue);
end;

begin
Main();
end.

In this program we could create a CircleArea function that calculates the area of a circle. That would mean that we could use that function any time we wanted to get the area of a circle.

Designing a function is just like designing a procedure. One way of approaching it is to think that someone has asked you to perform the calculation. Putting yourself in that position will help you think about the data the function will need to be given, and the data it will return. In this case you need to be told the radius of the circle, and you then return the area. The radius will be a number, and could have a fractional part so you use the Single data type. The result will then be a number and could have a fractional part, so it is also a Single. This gives you the functions prototype - which is all the details about the functions name, its parameters, and re- turn type.

The body of the function will then calculate the area of the circle, from the radius parameter. It will then set this as the result of the function. The value in the result variable is returned to the caller when the function ends.

function CircleArea(radius: Single): Single; var
area: Single; begin
area := Pi() * Sqr(radius); result := area;
end;

You can then call the CircleArea function in Main, and store the result of calling the function into Main's cirArea variable.

procedure Main();
var
cirRadius, cirArea: Single; roundedValue: Integer;
begin
cirRadius := ReadInteger('Enter a radius: '); cirArea := CircleArea(cirRadius); WriteLn('Circle area is ', cirArea:4:2); roundedValue := Round(area);
WriteLn('Which rounded off has a value of ', roundedValue);
end;

For this task you can choose to implement one of the following three programs:

- Air Speed Velocity: Calculate the air speed velocity of an unladen swallow.

- Compound Interest: Calculate how much interest you get on an investment.

- Distance Travelled: Calculate the distance travelled from an initial velocity, acceleration and time.

Instructions for each of these programs follow. Once you have completed the task, submit your code and a screenshot of the Terminal with the output of the program.

Option 1: Air Speed Velocity Instructions

For this task you will create a program to answer the age old question "What is the airspeed velocity of an unladen swallow?".

The air speed velocity of a bird can be calculated using an equation based on the Strouhal Number (also see). From this information we can determine that the air speed velocity of a bird (U) can be calculated from the frequency (f) at which the bird beats its wings multiplied by the amplitude (A) of each wing stroke, divided by the Strouhal Number (s). This is shown in the following equation:

U = (fA)/s

1. Create a program named AirSpeed, which will read and write values from the Terminal (using TerminalUserInput).

2. Declare a constant for the Strouhal Number with the value 0.33. In the code use UPPER- CASE for the constant. e.g.: const STROUHAL_NUM = 0.33;

3. Declare a function named AirSpeedVelocity. This function will have parameters for fre- quency and amplitude - both of the Double type, and it will return a Double value.

4. Main will write out 'African Swallow', and calculate the air speed based on a frequency of 15hz and amplitude of 21cm, and write the resulting velocity to the console.

5. Main will write out 'European Swallow', calculate the air speed based on a frequency of 14hz and an amplitude of 22cm, and write the resulting velocity to the console.

6. Main should then ask the user to enter a frequency and an amplitude and print out the air- speed based on the values entered.

Compile and run the program and check that you are getting the correct values, then prepare it for your portfolio.

Option 2: Investment Calculator

With compound interest, interest is paid on the principal and any interest received during the period of the investment. For example, if you invest $1000 at 15% interest then in the first year you will receive $150 interest. This interest is then added to your investment, and in the sec- ond year you will receive $172.50 interest (15% of the $1150).

The following formula can be used to calculate the amount of interest an investment will re- ceive over a period of time.

I = P (1 + i)n - P

1. Create a program named InvestmentCal, which will read and write values from the Terminal (using TerminalUserInput).

2. Declare a function named CompoundInterest. This function will have parameters for principal, years (both integers) and rate (a Double) and it will return a Double value.

3. Main will write out 'Bank A', and calculate the interest based on a principal of $1000, 3 years, at 3.5% (0.035).

4. Main will write out 'Bank B', and calculate the interest based on a principal of $1000, 3 years, at 4.5% (0.045).

5. Main should then ask the user to enter a principal and rate, and output the interest for a 5 year investment.

Compile and run the program and check that you are getting the correct values, then prepare it for your portfolio.

Option 3: Distance Calculator

It is possible to determine the distance travelled given an initial velocity, acceleration, and time. For example, if you start with a velocity of 8m/s and accelerate at 0.1m/s2 for 5 minutes (300 seconds) then you will have travelled 6900 meters.

The following formula can be used to calculate the distance from initial velocity (v) acceleration (a) and time (t).

D = vt + 0.5 at2

1. Create a program named DistanceTravelled, which will read and write values from the Terminal (using TerminalUserInput).

2. Declare a function named Distance. This function will have parameters for initial velocity, acceleration and time (all Double values), and it will return a Double value.

3. Main will write out 'Distance 1', and calculate the distance based on an initial velocity 0 m/ s, 120 seconds, with an acceleration of 0.07m/s2.

4. Main will write out 'Distance2', and calculate the distance based on an initial velocity 8.33
m/s, 120 seconds, with an acceleration of 0 m/s2.

5. Main should then ask the user to enter initial velocity, time, and acceleration, and output the distance for these values.

Compile and run the program and check that you are getting the correct values, then prepare it for your portfolio.

Attachment:- 2.4P-resources.zip

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91780447

Have any Question?


Related Questions in Programming Language

Extend the adworks applicationi add dialogs to allow the

Extend the AdWorks application I. Add Dialogs to allow the user to Add, Edit, Read and Delete a Customer and refresh the view accordingly. 1. The user should be able to select a specific customer from the DataGrid and cl ...

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Task arrays and structsoverviewin this task you will

Task: Arrays and Structs Overview In this task you will continue to work on the knight database to help Camelot keep track of all of their knights. We can now add a kingdom struct to help work with and manage all of the ...

Background informationthis assignment tests your

Background Information This assignment tests your understanding of and ability to apply the programming concepts we have covered throughout the unit. The concepts covered in the second half of the unit build upon the fun ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Overviewthis tasks provides you an opportunity to get

Overview This tasks provides you an opportunity to get feedback on your Learning Summary Report. The Learning Summary Report outlines how the work you have completed demonstrates that you have met all of the unit's learn ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Question 1 what is a computer program what is structured

Question: 1. What is a Computer program? What is structured programming? 2. What is modular programming? Why we use it? 3. Please evaluate Sin (x) by infinite series. Then write an algorithm to implement it with up to th ...

Task - hand execution of arraysoverviewin this task you

Task - Hand Execution of Arrays Overview In this task you will demonstrate how arrays work by hand executing a number of small code snippets. Instructions Watch the Hand Execution with Arrays video, this shows how to ste ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As