COMP 2012H Honors Object-Oriented Programming and Data Structures

Lab 1 VSCode, Fundamentals of C++, and Program Flow Control

Registering for a CSD Account

A CSD (Computer Science Department) account is required to:
  • Access the lab rooms (when they are open) and use the lab machines.
  • Access the official course website from an off-campus network.
  • SSH (Secure Shell) remote access lab machines or Linux servers (csl2wkXX@cse.ust.hk), where XX is a number from 01 to 52 (e.g. csl2wk44@cse.ust.hk).

If you do not have a CSD account yet, you may create it with the instructions here. Please pay special attention to which two checkboxes you need to check. You may already have it if you have studied some other COMP courses at HKUST.

  1. Go to this webpage.
  2. CSD Account Name: Enter your ITSC Username (HKUST Student Username), which is the same as your HKUST Email Username, e.g. cwingho.
  3. Password is independent from your ITSC Password. Recommended to be different for security.
  4. Check the two checkboxes of "Unix account at UG domain" and "PC account at domain CSD".
  5. Click on the "Go UPDATE" button.

Setting Up VSCode

VSCode will be used as the IDE (Integrated Development Environment) for COMP2011, COMP2012, and COMP2012H. Please check here for more information about setting up VSCode.

Review


Operators

To express condition/s that we will use to control program flow, relational and logical operators can be used.

  • Relational Operators
  • Logical Operators

Selection/Branching

To select or to branch according to certain condition/s, the following selection/branching statements can be used.

  • if Statement
  • if-else Statement
  • if-else-if Statement
  • switch Statement

Iteration/Looping

To iterate or to loop according to certain condition/s, the following iteration/looping statements can be used. The break and continue statements provide additional control to these iteration/looping statements.

  • while Loop
  • do-while Loop
  • for Loop
  • break and continue
Vending machines

Introduction

This is a lab on program flow control.

In this lab, we are going to implement a simple vending machine. We assume the vending machine is one of the old ones where users insert coins in order to purchase items. So, a user can interact with the vending machine by performing one of three actions, until the user leaves:

  • Insert a coin: The user inserts one coin at a time. The balance of the vending machine is increased by the value of the inserted coin.
  • Purchase an item: The user selects an item to purchase. If the balance is greater than or equal to the cost of the item and the item not yet sold out, then the item is purchased and is available for pickup. The balance is decreased by the cost of the item.
  • Return coins: The user activates the "Coin return" lever. The vending machine attempts to clear the balance by returning the coins with equivalent value. This is done usually after the user had purchased something.
Vending machines

To support above operations, internal states are maintained in the vending machine:

  • Balance: This is the total value available to purchase items, from the coins that were inserted by the user.
  • Cost and count of items: For simplicity, the vending machine only sells two kinds of items. Thus, the vending machine needs to know the cost and count (number of remaining) of each item:
    • Item A: $6. Initially, the vending machine has 3 Item A in stock.
    • Item B: $4. Initially, the vending machine has 3 Item B in stock.
  • Count of coins: For simplicity, the vending machine only accepts three kinds of coin: $5 coin, $2 coin, and $1 coin. Thus, the vending machine keeps track of the count of each kind of coin. Initially, the vending machine has the following stockpile of coins:
    • 3 $5 coins
    • 3 $2 coins
    • 3 $1 coins

Since this is a lab on program flow control, please complete the lab without the following features:

  • Functions
  • Arrays
  • Other topics that are not yet discussed in lectures

Lab Work

Task 1 - Define Variables

To represent internal states, define a variable for each internal state.

For example, define an int variable for balance. Also, define three int variables, for the count of each kind of coin, and so on:


                int balance = 0;

                int coin_5_count = 3;
                int coin_2_count = 3;
                int coin_1_count = 3;

                /* more... */
              

For best practice, avoid defining global variables. Please define the variables inside the main function.

Task 2 - Display Menu and Handle User Input

When the program is started, display the following menu, including the balance, then prompt the user to provide input. This is repeated forever after taking each action until the user decides to leave. (All menu text cout statements are given in the program skeleton, see Resources & Sample I/O)


                The balance is $0
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4:
              

The user input needs to be validated, though it can be assumed that the user always enters an integer. Suppose the user has entered 5, which is invalid. In this case, report invalid input to the user and prompt the user for input again:


                Please enter 1-4: 5
                Invalid choice
                The balance is $0
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4:
              

Task 3 - Handle "Insert coin"

If the user decided to insert a coin, then prompt the user for the coin:


                Please insert a coin: $
              

If the user did not supply a $5, $2 or a $1 coin, then report invalid input to the user and prompt the user for input again:


                Please insert a coin: $3
                Only $5, $2 and $1 coins are accepted
                Please insert a coin: $
              

Otherwise, increase the balance and the count of the corresponding coin.

Task 4 - Handle "Purchase"

If the user decided to purchase, then display the following submenu to show each item and the cost, also a menu option to go back without purchasing. If an item has been sold out (count of that item is zero), then display "SOLD OUT" for that item. Lastly, prompt the user for input:


                Please select an item:
                1. Item A - $6
                2. Item B - SOLD OUT
                3. Go back
                Please enter 1-3:
              

If the chosen item has been sold out, report this to user and prompt the user for input again. If the balance is smaller than the cost of the chosen item, report insufficient balance to user and prompt the user for input again. Also, the input needs to be validated:


                Please enter 1-3: 2
                Item B has been sold out
                Please select an item:
                1. Item A - $6
                2. Item B - SOLD OUT
                3. Go back
                Please enter 1-3: 1
                Insufficient balance
                Please select an item:
                1. Item A - $6
                2. Item B - SOLD OUT
                3. Go back
                Please enter 1-3: 4
                Invalid choice
                Please select an item:
                1. Item A - $6
                2. Item B - SOLD OUT
                3. Go back
                Please enter 1-3:
              

Otherwise, decrease the balance and the count of the corresponding item. Also, report to user the purchased item:


                Please enter 1-3: 1
                Purchased item A
              

Task 5 - Handle "Return coins"

If the user decided to return coins, then attempt to return an available coin with the largest value not exceeding the current balance, until the balance reaches zero or there are no available coins to return. Report to user for each returned coin.

Suppose the balance is $6,

  • If there are at least one $5 coin and at least one $1 coin, then a $5 coin and $1 coin are returned:
    
                        Returned a $5 coin
                        Returned a $1 coin
                      
  • If there are no $5 coins, but at least 3 $2 coins are available, then 3 $2 coins are returned:
    
                        Returned a $2 coin
                        Returned a $2 coin
                        Returned a $2 coin
                      
  • If there are no $5 coins, but 2 $2 coins and at least 2 $1 coins are available, then 2 $2 coins and 2 $1 coins are returned:
    
                        Returned a $2 coin
                        Returned a $2 coin
                        Returned a $1 coin
                        Returned a $1 coin
                      
  • If there are one $5 coin and at least 3 $2 coins, then only 1 $5 coin is returned. In this case, the remaining balance is not 0 but there are no available coins to return. Report to user for this:
    
                        Returned a $5 coin
                        Oops... Not enough coins to return...
                      
    The remaining balance is $1.
  • Note: For simpliticy, coin return is greedy. The coin to return is the one that can reduce the most balance. It does not consider other options that can yield better results. For example, suppose there are 1 $5 coin and 3 $2 coins. Under the current greedy principle, a $5 coin is always returned, but the balance would not reach zero. An alternative exists which is to return 3 $2 coins, and that the balance would become zero. But this is never considered since the greedy action is always chosen.

Task 6 - Handle "Leave"

If the user decided to leave, then print the following message and terminate the program.


                Bye
              

Resources & Sample I/O

Sample I/O

A sample I/O is given below, with the user input highlighted. You are encouraged to try the demo program to try more possible cases.


                The balance is $0
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 1
                Please insert a coin: $5
                The balance is $5
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 2
                Please select an item:
                1. Item A - $6
                2. Item B - $4
                3. Go back
                Please enter 1-3: 2
                Purchased item B
                The balance is $1
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 3
                Returned a $1 coin
                The balance is $0
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 1
                Please insert a coin: $5
                The balance is $5
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 2
                Please select an item:
                1. Item A - $6
                2. Item B - $4
                3. Go back
                Please enter 1-3: 2
                Purchased item B
                The balance is $1
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 3
                Returned a $1 coin
                The balance is $0
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 1
                Please insert a coin: $5
                The balance is $5
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 2
                Please select an item:
                1. Item A - $6
                2. Item B - $4
                3. Go back
                Please enter 1-3: 2
                Purchased item B
                The balance is $1
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 3
                Returned a $1 coin
                The balance is $0
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 1
                Please insert a coin: $5
                The balance is $5
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 1
                Please insert a coin: $2
                The balance is $7
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 2
                Please select an item:
                1. Item A - $6
                2. Item B - SOLD OUT
                3. Go back
                Please enter 1-3: 1
                Purchased item A
                The balance is $1
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 3
                Oops... Not enough coins to return...
                The balance is $1
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 1
                Please insert a coin: $1
                The balance is $2
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 3
                Returned a $2 coin
                The balance is $0
                Please choose an action:
                1. Insert coin
                2. Purchase
                3. Return coins
                4. Leave
                Please enter 1-4: 4
                Bye
              

Submission & Grading

Test Cases

Here are the test cases used in ZINC:

  1. 4
  2. 0 5 4
  3. 1 5 1 2 1 1 4
  4. 1 3 4 0 1 4
  5. 2 3 4
  6. 2 0 4 3 4
  7. 1 5 1 5 2 1 2 2 4
  8. 1 1 3 4
  9. 1 2 3 4
  10. 1 5 3 4
  11. 1 5 1 5 1 2 1 1 3 4
  12. 2 1 2 3 1 2 2 1 2 3 1 2 2 1 2 4
  13. 1 5 1 5 1 5 1 5 2 1 2 1 2 1 2 1 3 4
  14. 1 5 1 5 1 5 2 2 2 2 2 2 2 2 3 4
  15. 1 5 1 5 2 1 3 1 5 1 5 2 1 3 1 5 1 5 2 1 3 4
  16. 1 5 2 2 3 1 5 2 2 3 1 5 2 2 3 1 5 1 2 2 1 3 4
Page maintained by
Homepage