0% found this document useful (0 votes)
15 views3 pages

Ruby Program for Meal Price Calculation

The document outlines a desk check task for calculating a bill total using pseudocode, including test data and expected results for two data sets. It also contains short answer questions regarding programming concepts, such as the importance of statement execution order, types of programming statements, and variable scopes in Ruby. The answers provide insights into programming terminology and the behavior of variables in Ruby.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Ruby Program for Meal Price Calculation

The document outlines a desk check task for calculating a bill total using pseudocode, including test data and expected results for two data sets. It also contains short answer questions regarding programming concepts, such as the importance of statement execution order, types of programming statements, and variable scopes in Ruby. The answers provide insights into programming terminology and the behavior of variables in Ruby.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Answers to Questions from TT1.

2
Name: Khương Hà Phương
Student ID: SWH02312

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 set Second data set
appetizer_price 10.30 12.40
main_price 34.00 41.00
dessert_price 8.50 9.80

Expected Result:

First data set Second data set


Output: $52.80 $63.20
Desk check - fill this in by completing the missing code in bill_total.rb (in the
tasks Resources folder) then running it with the test data above:

Statement appetizer main dessert total output


_price _price _price _price
First Read the value of 10.30 nil nil nil nil
Pass appetizer_price
Read the value of nil 34.00 nil nil nil
main_price
Read the value of nil nil 8.50 nil nil
dessert_price
Calculate the total_price nil nil nil 52.80 nil
Convert to dollars nil nil nil nil $52.80
Output the total_price nil nil nil nil $52.80
Second value
Read the of 12.40 nil nil nil nil
Pass appetizer_price
Read the value of nil 41.00 nil nil nil
main_price
Read the value of nil nil 9.80 nil nil
dessert_price
Calculate the total_price nil nil nil 63.20 nil
Convert to dollars nil nil nil nil $63.20
Output the total_price nil nil nil nil $63.20

2. 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)
Executing statements in the correct sequence may generate the desired results. Otherwise,
it may affect the output (whether it becomes false output or errors occur).

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

This is an expression statement.

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

The computer first converts the variables to integers.


Then it calculates the total number.

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

The value of i will be added 1 more.

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?
The two different scopes for variables in Ruby are the global scope and the local scope.
See the lesson materials for help with Question 6. You could also see:
[Link]

Common questions

Powered by AI

Ruby manages variable scope using global and local scopes. A variable's scope determines its accessibility – global scope variables can be accessed from anywhere in the Ruby program, while local scope variables can only be accessed within the method or block in which they are defined. This is significant as it affects the program modularity, preventing accidental changes to global state and ensuring that variable names do not conflict unnecessarily .

If values are not read before calculating 'total_price', the computation will involve undefined or nil values, causing incorrect outputs or runtime errors. To mitigate this, the program should ensure that all inputs are validated and assigned before performing calculations. Additionally, using control structures or guard clauses to verify that inputs are processed correctly can prevent these errors .

Data types determine how variables like 'appetizer_price', 'main_price', and 'dessert_price' are stored and manipulated in memory. In Ruby, these are floating point numbers, which means they can handle decimal values and are appropriate for financial calculations that require precision beyond integers .

The code 'main_price = 10' is an example of an expression statement. Its function is to assign the value 10 to the variable main_price, setting or updating its value within the program .

When 'a = a + b' is executed, the computer first converts the variables to integers if necessary, then adds the value of b to a, and finally stores the result back in variable a. This increments the value of a by the amount of b .

The process involves ensuring that all variables in the expression have compatible types, often converting them to a common type if necessary. For 'total_price = appetizer_price + main_price + dessert_price', each variable must be a floating point to maintain precision. The significance is that improper type conversion can lead to loss of accuracy or incorrect results if, for example, integers with insufficient precision were used .

Executing statements in the correct sequence is crucial because it directly impacts the result. In computing total prices, if the calculation of total_price is done before assigning values to appetizer_price, main_price, or dessert_price, the calculation will use incorrect or nil values, leading to erroneous outputs or errors .

The operation 'i = i + 1' is an increment operation. It increases the value of i by 1, effectively updating the variable i to store a value that is one greater than its previous value. This is a fundamental operation in loops and other iterative constructs .

Using global scope variables for financial calculations increases the risk of unintended side effects, as they can be altered from anywhere in the program, potentially leading to incorrect results. Local scope variables, in contrast, enhance encapsulation and protect the integrity of financial calculations by restricting access to the relevant context, thereby minimizing the likelihood of errors from external interference .

Understanding assignment is crucial because it determines how data is stored and manipulated. In 'total_price = appetizer_price + main_price + dessert_price', the assignment sets the result of the addition operation to total_price. Misunderstanding assignment could lead to incorrect data being stored or operations being performed on uninitialized variables, which can result in financial miscalculations .

You might also like