Top-Rated Free Essay
Preview

PT1420 Unit 4 Assignment 1 Homework

Satisfactory Essays
765 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
PT1420 Unit 4 Assignment 1 Homework
Short Answer 1-7 on page 111
1. How do modules help you to reuse code in a program?
If a operation needs to be performed in several places, the same module can be called and re-used cutting down on unnecessary code.

2. Name and describe the two parts that a module definition has in most languages.
The two parts are a header and a body

3. When a module is executing, what happens when the end of the module is reached?
It jumps back to the part of the program that called it. This is known as the return point

4. What is a local variable? What statements are able to access a local variable?
A local variable is declared inside a module and cannot be accessed by statements that are outside the module.

5. In most languages, where does a local variable’s scope begin and end?
Usually begins at the variables declaration and ends at the end of the module

6. What is the difference between passing an argument by value and passing it by reference?
Passing an argument by value copies the data from the variable but does not change the original data. Passing a variable by reference will overwrite the existing data at that reference point in memory.

7. Why do global variables make a program difficult ot debug?
If there is a debug issue, you have to step through ALL modules looking for the module that is incorrectly accessing that variable. Most programs have many many modules. This makes global variables very time consuming to debug.

Algorithm Workbench Review Questions 1,5,6, and 7 from page 111

1. Design a module named timesTen. The module should accept an Integer argument. When this module is called, it should display the product of its argument multiplied by 10

1. Declare a variable called number and set the value of it
2. Call the module timesTen passing as an argument the variable number by reference
3. In the module named timesTen create a parameter named number that the argument will be passed into
4. Calculate and set the variable number * 10
5. Display the variable number

5. Design a module named getNumber, which uses a reference parameter variable to accept an Integer argument. The module should prompt the user to enter a number and then store the input in the reference parameter variable

Module main()

Declare integer number
Call getNumber(number)
Display “Your number is” number
End Module
Module.getNumber(Ref number) Display “Please enter a number” Input number
End Module

6. What will the following pseudocode program display?
Module main() Declare Integer x =1 Declare Real y = 3.4 Display x, “ “, Y Call changeUs(x, y) Display x, “ “, y
End Module
Module changeUs(Integer a, real b) Set a = 0 Set b = 0 Display a, “ “, b
End Module
This will display 1, 3.4

7. What will the following pseudocode program display?
Module main() Declare integer x =1 Declare Real y = 3.4 Display x, “ “, y Call changeUS(x, y) Display x, “ “, y
End Module
Module changeUs(integer ref a, real ref b)
Set a = 0
Set b = 0.0
Display a, “ “, b
End Module
The display on this pseudocode would be.
1, 3.4
0, 0.0
0, 0.0

Programming Exercises 1 and 2 page 113
1. Kilometer Converter
Design a modular program that asks the user to enter a distance in kilometers, and then converts that distance to miles. The conversion formula is Miles= Kilometers * 0.6214
Module main()
Declare real km =0
Declare real miles =0
Call getKM(km)
Call doCalc(km, miles)
Call displayMiles(km, miles)
End Module
Module getKM(Ref Real KM) Display “Please enter the Kilometers” set km
End Module
Module doCalc(Value real km, Ref real miles) Set miles = km * 0.6214
End Module
Module displayMiles(value real km, value real miles)
Display “You entered” km “Kilometers”
Display “That converts to” miles “Miles”
End Module

2. Sales Tax Program Refactoring
ReWrite sales tax program from chapter 2 into module design
Module main() Declare real purchaseAmount = 0 Declare real constant stateTax = .04 Declare real constant countyTax = .02 Declare real StateTotalTax = 0 Declare real CountyTotalTax = 0 Declare real TotalTax = 0 Declare real total = 0 Call getPurchaseAmount(purchaseAmount) Call doCalc(purchaseAmount, stateTax, StateTotalTax, countyTax, CountyTotalTax TotalTax, total) Call displayTotals(purchaseAmount, StateTotalTax, CountyTotalTax, TotalTax, total)
End Sub
Module getPurchaseAmount(Ref real purchaseAmount) Display “Enter the Purchase amount” Set purchaseAmount
End Sub
Module doCalc(Value real purchaseAmount, Value const stateTax, Ref real StateTotalTax, Value const countyTax, Ref real CountyTaxTotal, Ref real TotalTax, Ref real total) Set StateTotalTax = purchaseAmount * stateTax Set CountyTotalTax = purchaseAmount * countyTax Set TotalTax = StateTotalTax + CountyTotalTax Set total = TotalTax + purchaseAmount
End Module
Module displayTotals(value real purchaseAmount, value real StateTotalTax, value real CountyTotalTax, value real TotalTax, value real total) Display “Your purchase amount is” purchaseAmount Display “The total state tax is” StateTotalTax Display “The total county tax is” CountyTotalTax Display “TheTotal Tax amount is” TotalTax Display “Your Purchase Total is” total
End Sub

You May Also Find These Documents Helpful

  • Good Essays

    4. Find at least one use of parallel structure in the Declaration. What key terms are repeated in identical or equivalent constructions, and to what effect?…

    • 695 Words
    • 3 Pages
    Good Essays
  • Better Essays

    6) When you pass by value you pass a copy. When you pass by reference, you can modify the contents.…

    • 1580 Words
    • 11 Pages
    Better Essays
  • Satisfactory Essays

    PT1420 Unit 8 Lab 8

    • 365 Words
    • 2 Pages

    Step 3: Write a for loop that will print 60 minutes to the screen. Complete the missing…

    • 365 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    8 T | Rather than put several values in a single column, you should create several columns and number them like column1, column2, etc.…

    • 313 Words
    • 1 Page
    Satisfactory Essays
  • Good Essays

    Operating System

    • 1099 Words
    • 5 Pages

    Answer: d. It redirects the output of one command to the input of another command.…

    • 1099 Words
    • 5 Pages
    Good Essays
  • Good Essays

    8. Is it possible that the instructions within a While statement might never be executed at all? Give an example.…

    • 610 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    C. It will move the contents of temp1 from directory temp to temp1 of current directory and when finished it will erase the contents of temp1 of temp directory…

    • 529 Words
    • 3 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Positional Notation: Quiz

    • 518 Words
    • 4 Pages

    times. Finally, and that is what we have to use here to find any decimal number (Michal does not specify…

    • 518 Words
    • 4 Pages
    Satisfactory Essays
  • Satisfactory Essays

    a.x is available to code that is written outside the Sphere class. b.radius is not available to code written outside the Sphere class. c.radius, x, y, and z are called members of the Sphere class. d.z is available to code that is written outside the Sphere class.…

    • 1368 Words
    • 9 Pages
    Satisfactory Essays
  • Satisfactory Essays

    top/beginning. This way, you can see, in one place, the names of variables that will be…

    • 400 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    Onetop Master

    • 439 Words
    • 2 Pages

    A public static method named computeSum() is located in classA. To call the method from within classB, use the statement ___________.…

    • 439 Words
    • 2 Pages
    Good Essays
  • Satisfactory Essays

    Java 01

    • 914 Words
    • 4 Pages

    Statement 1 would cause and error as it contains the wrong type as it is looking for hoverfrog but can only find frog. Statement 2 is ok as the variable has been renamed as Hoverfrog.…

    • 914 Words
    • 4 Pages
    Satisfactory Essays
  • Satisfactory Essays

    notes on arrays

    • 326 Words
    • 2 Pages

    DERIVED DATA TYPES: REFERENCES, ARRAYS AND POINTERS Krishna M. Singh Department of Mechanical and Industrial Engineering Indian Institute of Technology Roorkee Roorkee 247667 Reference: Deitel and Deitel. C++ : How to Program. Prentice Hall, 2008 Krishna M. Singh, Department of Mechanical & Industrial Engineerig, IIT-Roorkee 1 Fundamental Data Types Basic Types: Boolean, character, integer and floating point types. Additional Types: void : To signify absence of information void f() // function f() does not return a value void *pv; // pointer to object of unknown type enumeration (enum): To represent specific set of values.…

    • 326 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    The SDLC Process

    • 907 Words
    • 4 Pages

    Another practical example of a module. Procedures tend to perform actions as with functions these are reusable…

    • 907 Words
    • 4 Pages
    Good Essays
  • Better Essays

    4.1 Three Structured Constructs All programs can be constructed using only the three basic constructs. It is the concept of structured programming that a program should be developed using only these three basic constructs. It is not wrong to use the GOTO structure but it is discouraged, because programs with the GOTO structure are often classified as unstructured. The three basic constructs are Sequence Selection Iteration/Repetition 4.1.1 Sequence Construct In a sequence construct, the way the statements are placed implies the order in which the computer will execute the statements. The computer will interpret the statements in a left to right, top to bottom fashion. Consider the following Calculate-Average Module DO Get Two Numbers Calculate Sum Determine Average Print Average ENDDO The sequence for the above module is straightforward. You get the two numbers, calculate the sum, determine the average and print out the result. If the statements are not in that sequence, the result obtained will not be the answer intended Figure 4-1 Simple sequence The Calculate Average module is not a separate module away from the rest of the four modules below it. But it actually contains all the four modules. Each process (a rectangle) represents a component with their function/action stated in the function list. If the component does not contain or form other constructs (i.e. other sequences, selections or iterations), it is regarded as an elementary component. The diagram should be interpreted in a top-down, left to right manner. The Calculate-Average Module has four elementary components. Each time it is activated, it will activate the Input Number module, Calculate Sum module, Determine Average module and the Print Result module in that order. Consider another example Figure 4-2 Complex sequence The sequence of activation for the above will be A, B, E, F, C, D, G, I, J, H 4.1.2 Selection Construct The selection construct consists of condition(s) and one or…

    • 2133 Words
    • 5 Pages
    Better Essays