0% found this document useful (0 votes)
5 views5 pages

Lab1COS10009 Week1

Uploaded by

thanhnam0810.tnl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Lab1COS10009 Week1

Uploaded by

thanhnam0810.tnl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Answers to Questions from TT1.

2
Name: Le Thanh Nam
Student ID: 104999380

1. Desk Check Task: Calculate Bill Total


Required Variables:
Real (floating point):
appetizer_price, main_price, dessert_price
total_price

Pseudocode:
Read the value of appetizer_price
Read the value of main_price
Read the value of dessert_price
total_price = appetizer_price + main_price +
dessert_price
Print ‘$’ then the value of total_price to the terminal
showing two decimal places.

Test Data:
First data Second data
set set
appetizer_p 10.30 12.40
rice
main_price 34.00 41.00
dessert_pric 8.50 9.80
e

Expected Result:

First data Second data


set set
Output: $52.80 $63.20
Desk check - fill this in by hand-tracing/hand-executing the
pseudocode provided with the test data above:

Statement appeti main desse total outp


zer _pric rt _pric ut
_price e _price e
First Read the value of 10.30 - - - -
Pass appetizer_price
Read the value of 10.30 34. - - -
main_price 00
Read the value of 10.30 34.0 8.50 - -
dessert_price 0
Calculate the 10.30 34.0 8.50 52.8 -
total_price 0 0
Convert to dollars $10.3 $34. $8.50 $52. -
0 00 80
Output the $10.3 $34. $8.50 $52. $52.
total_price 0 00 80 80
Seco Read the value of 12.40 - - - -
nd appetizer_price
Pass Read the value of 12.40 41.0 - - -
main_price 0
Read the value of 12.40 41.0 9.80 - -
dessert_price 0
Calculate the 12.40 41.0 9.80 63.2 -
total_price 0 0
Convert to dollars $12.4 $41. $9.80 $63. -
0 00 20
Output the $12.4 $41. $9.80 $63. $63.
total_price 0 00 20 20

2. Complete Program Calculate Bill Total

Now check the actual code produces the output you expected

Do this by completing the missing code in bill_total.rb in Task 1.3 then running the
program.
3. Short Answer Questions:

Focus in the following on using the correct computing terminology.

Here are some terms that may help you: Assignment, evaluate, increment,

1. Using a few sentences explain why it may be important to


execute statements in the correct sequence. (eg: what might
happen if the last statement in Program 2 was executed earlier)
The order of execution in programming is crucial because it determines
the outcome of the program.

2: The code main_price = 10 is an example of which kind of


programming statement?

This is an assignment statement.

3: What actions does the computer perform when it executes a = a


+ b?

The computer first evaluate


Then it increment

4: How would the value of variable i change in the statement i = i +


1?

The value of i will be increment

5: What sort of types will Ruby use to store the following


variables (given the associated variable values)?

Data Type

A person's name e.g: “Fred Smith” String

Number of students in a class e.g: 23 Integer


Average age of a group of people e.g: 23.5 Float
A temperature in Celsius e.g: 45.7 Float
True or false e.g: 1 == 2 Boolean
Note: possible types include: Integer, String, Float, Boolean
6: Variables have a scope – what are two different scopes
variables can have in Ruby?

· Local Scope: Variables defined within a method, block, or class have local scope. They
are only accessible within that specific context and are destroyed when the context ends.

· Global Scope: Variables defined outside of any method, block, or class have global scope.
They can be accessed from anywhere within the program, including within methods, blocks,
and classes.

See the lesson materials for help with Question 6. You could also see:
https://www.tutorialspoint.com/ruby/ruby_variables.htm

You might also like