CSCI1120 Introduction to Computing Using C++

CSCI1120 Introduction to Computing Using C++, Fall 2024/25

Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright © 2024 CSE, CUHKPage 1 of 8 Assignment 2: Gumball MachinesDue: 23:59, Thu 3 Oct 2024File name: gumball.cppFull marks: 100

Introduction

The objective of this assignment is to let you practice control flow structures in C++. It also involvesthe use of variables, operators, expressions, and standard input/output to reinforce your learning inthe course thus far. You are to write a program to print an ASCII character pattern resembling thedrawing of a gumball vending machine, such as Figure 1 below.

_ _ _ _ _

/ _ _ _ _ \

/ / \ \

/ / \ \

/ / O O O O O \ \

/ / O O O O O O O \ \

\ \ O O O O O O O / /

\ \ O O O O O O / /

\ \ O O O O O / /

\ \ _ _ _ _ / /

\ _ _ _ _ _ /

| _ |

| |_| |

| |

| |

|_ _ _ _ _|

Figure 1: A sample character pattern resembling a gumball machine

This gumball machine character pattern is composed of two parts:

  1. Container: A hexagonal shape in double dashed lines is used to represent the container holding

the gumballs. Each gumball is denoted by a capital letter 'O'.

  1. Stand: A square shape below the hexagon is used to represent the stand supporting the

gumball container. A small square of unit length (always at the center of the 2nd line) inside this

stand shape is used to represent the chute door, i.e., the opening where gumballs come out.

The whole pattern is formed from the set of characters in Table 1 below.

Table 1: Characters for printing the ASCII art drawing

Character

Name of the Character

_

Underscore

|

Pipe (Vertical bar)

\

Backslash

/

Forward slash

O

Letter O (denoting a gum)

Space

? = side length of the square

? = side length of the hexagon

gum flap(chute door)standcontainergumCSCI1120 Introduction to Computing Using C++, Fall 2024/25

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2024 CSE, CUHKPage 2 of 8 Instead of hardcoding, you are required to use loops and conditionals to print the drawing whosesize can be scaled up or down according to the user input (See Table 2 for examples).

User Input There are two user inputs required at the program start.

  1. Side length (?): it is the side length of the hexagon (or the square). This input controls theumber of underscores that form the outer edge of the hexagon.
  1. Stock of gumballs (?????): the initial number of gumballs to load into the vending machine.Due to the double dashed line design of the hexagon and the spaces involved, the maximum numberof gumballs that can be put into the container, i.e., its capacity (?), is limited and determined by theollowing formula (deduced from the sum formula of an arithmetic series):

? = 3?! − 8? + 5 … (1)

Input validation

If the side length (?) is smaller than 3, there is not enough room to print the chute door. When? is getting too big, the output may overrun your terminal width and look distorted due to linewrapping. So, let us assume its valid range is between 3 and 29. (Note: in case you still see linewrapping issues in this range, you may resize your terminal via its settings.)

  1. The initial number gumballs (?????) to load into the machine must lie between ⌊?/2⌋ (floor ofthe division) and ?, inclusive.f the user input falls outside the valid range, the program will terminate immediately with an errormessage. See the Sample Runs section.

Size Scaling Table 2 shows some examples to explain how the container shape and its capacity (?) scale with theside length (?) input.Note that for making the width and height of the hexagon (or square) look similar in the console, weput a single space between every twonderscores or two letter O’s in a horizontal line. For bettervisualization of the spaces required to produce the output, we used the symbol ␣ to denote a spacecharacter.

Machine Operations

Besides printing the gumball machine, the program will also prompt the user to enter a quantity ofgumballs to buy. When the user enters a valid value (between 1 and ?????), the quantity will bededucted from the stock and there will be fewer O’s shown in the next printout of the gumball

machine. The program keeps repeating these operations until running out of stock. CSCI1120 Introduction to Computing Using C++, Fall 2024/25Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright © 2024 CSE, CUHKPage 3 of 8 Table 2: Sample output versus side length (?

␣␣␣␣_␣_␣_

␣␣/␣␣_␣_␣␣\

␣/␣/␣O␣O␣\␣\

/␣/␣O␣O␣O␣\␣\

\␣\␣O␣O␣O␣/␣/

␣\␣\␣_␣_␣/␣/

␣␣\␣_␣_␣_␣/

␣␣␣|␣␣_␣␣|

␣␣␣|␣|_|␣

␣␣␣|_␣_␣_|

␣␣␣␣␣_␣_␣_␣_

␣␣␣/␣␣_␣_␣_␣␣\

␣␣/␣/␣O␣O␣O␣\␣\

␣/␣/␣O␣O␣O␣O␣\␣\

/␣/␣O␣O␣O␣O␣O␣\␣\

\␣\␣O␣O␣O␣O␣O␣/␣/

␣\␣\␣O␣O␣O␣O␣/␣/

␣␣\␣\␣_␣_␣_␣/␣/

␣␣␣\␣_␣_␣_␣_␣/

␣␣␣␣|␣␣␣_␣␣␣|

␣␣␣␣|␣␣|_|␣␣|

␣␣␣␣|␣␣␣␣␣␣␣|

␣␣␣␣|_␣_␣_␣_|

␣␣␣␣␣␣_␣_␣_␣_␣_

␣␣␣␣/␣␣_␣_␣_␣_␣␣\

␣␣␣/␣/␣O␣O␣O␣O␣\␣\

␣␣/␣/␣O␣O␣O␣O␣O␣\␣\

␣/␣/␣O␣O␣O␣O␣O␣O␣\␣\

/␣/␣O␣O␣O␣O␣O␣O␣O␣\␣\

\␣\␣O␣O␣O␣O␣O␣O␣O␣/␣/

␣\␣\␣O␣O␣O␣O␣O␣O␣/␣/

␣␣\␣\␣O␣O␣O␣O␣O␣/␣/

␣␣␣\␣\␣_␣_␣_␣_␣/␣/

␣␣␣␣\␣_␣_␣_␣_␣_␣/

␣␣␣␣␣|␣␣␣␣_␣␣␣␣|

␣␣␣␣␣|␣␣␣|_|␣␣␣|

␣␣␣␣␣|␣␣␣␣␣␣␣␣␣|

␣␣␣␣␣|␣␣␣␣␣␣␣␣␣|

␣␣␣␣␣|_␣_␣_␣_␣_|

␣␣␣␣␣␣␣_␣_␣_␣_␣_␣_

␣␣␣␣␣/␣␣_␣_␣_␣_␣_␣␣\

␣␣␣␣/␣/␣O␣O␣O␣O␣O␣\␣\

␣␣␣/␣/␣O␣O␣O␣O␣O␣O␣\␣\

␣␣/␣/␣O␣O␣O␣O␣O␣O␣O␣\␣\

␣/␣/␣O␣O␣O␣O␣O␣O␣O␣O␣\␣\

/␣/␣O␣O␣O␣O␣O␣O␣O␣O␣O␣\␣\

\␣\␣O␣O␣O␣O␣O␣O␣O␣O␣O␣/␣/

␣\␣\␣O␣O␣O␣O␣O␣O␣O␣O␣/␣/

␣␣\␣\␣O␣O␣O␣O␣O␣O␣O␣/␣/

␣␣␣\␣\␣O␣O␣O␣O␣O␣O␣/␣/

␣␣␣␣\␣\␣_␣_␣_␣_␣_␣/␣/

␣␣␣␣␣\␣_␣_␣_␣_␣_␣_␣/

␣␣␣␣␣␣|␣␣␣␣␣_␣␣␣␣␣|

␣␣␣␣␣␣|␣␣␣␣|_|␣␣␣␣|

␣␣␣␣␣␣|␣␣␣␣␣␣␣␣␣␣␣|

␣␣␣␣␣␣|␣␣␣␣␣␣␣␣␣␣␣|

␣␣␣␣␣␣|␣␣␣␣␣␣␣␣␣␣␣|

␣␣␣␣␣␣|_␣_␣_␣_␣_␣_|

Program Specification

  1. The program first prompts the user for the side length ?.
  2. If ? is invalid (not between 3 and 29), the program prints an error message and terminates.
  3. Print the machine capacity ?, given by equation (1).
  4. Prompt the user for the ????? of gumballs.
  5. If ????? is invalid (not between ⌊?/2⌋ and ?), print an error message and terminate the program.
  6. Print the gumball machine using loops and conditionals. This step comprises more subtasks like:
  7. Determine the left padding, i.e., how many spaces to print before /, \ or | per row.
  8. Print the hexagonal part.
  9. Align the current stock of gumballs properly inside the container.
  10. Print the square part.
  11. Prompt the user for the quantity ? to buy.
  12. If ? is invalid (not between 1 and ?????), print an error message and go back to step 6.
  13. Deduct ? from ?????.
  14. If ????? > 0, go back to step 6.
  15. Print the message "Sold out!" finally.

Note two important points:

  • (Regarding 6.b) For a hexagon container full of gumballs, the number of gumballs varies by one

when going from one row to the next, except the two rows in the middle of the hexagon.

  • (Regarding 6.c) Gumballs are dispensed or “consumed” in a top-to-bottom, left-to-right manner.

The gumballs on the top row should be aligned to the right if their count is less than the row’s

capacity (see Figure 1 again). Once consumed, the letter 'O' denoting a gumball will be replaced

by a space character.

Assumptions: You can assume that all user inputs are always entered as integers. The program

behavior beyond this assumption can be indeterminate and your program behavior can be different

from our sample program.CSCI1120 Introduction to Computing Using C++, Fall 2024/25

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2024 CSE, CUHK

Page 4 of 8

Restrictions: You are NOT allowed to use any arrays, vectors, or any other data containers in this

assignment. You may use the string class (e.g., to store a line 代 写CSCI1120 Introduction to Computing Using C++  of characters if you see fit) but youcannot use its at() method or the subscript operator [] to traverse the individual characters of astring. Defining your own functions or macros is allowed but not mandatory.

Sample Runs

In the following sample runs, the blue numbers after ':' are user inputs and the other text is theprogram printout. You can try the provided sample program for other inputs. Your program printout shall be exactly the same as the sample program (same text, symbols, letter case, spacings, etc.).Note that there is a space after the ':' included in each input prompt.

Enter side length: -1↵

Invalid side length!

Enter side length: 30↵

Invalid side length!

Enter side length: 4↵

Machine capacity: 21

Enter gumball stock: 9↵

Too few / many gumballs to load!

Enter side length: 4↵

Machine capacity: 21

Enter gumball stock: 22↵

Too few / many gumballs to load!

Enter side length: 6↵

Machine capacity: 65

Enter gumball stock: 31↵

Too few / many gumballs to load!

Enter side length: 4↵

Machine capacity: 21

Enter gumball stock: 10↵

 

_ _ _ _

 

/ _ _ _ \

 

/ / \ \

/ / \ \

/ / O \ \

\ \ O O O O O / /

\ \ O O O O / /

 

\ \ _ _ _ / /

 

\ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

|_ _ _ _|

Enter quantity to buy: 1↵CSCI1120 Introduction to Computing Using C++, Fall 2024/25

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2024 CSE, CUHK

Page 5 of 8

 

_ _ _ _

 

/ _ _ _ \

 

/ / \ \

/ / \ \

/ / \ \

\ \ O O O O O / /

\ \ O O O O / /

 

\ \ _ _ _ / /

 

\ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

|_ _ _ _|

Enter quantity to buy: 5↵

 

_ _ _ _

 

/ _ _ _ \

 

/ / \ \

/ / \ \

/ / \ \

\ \ / /

\ \ O O O O / /

 

\ \ _ _ _ / /

 

\ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

|_ _ _ _|

Enter quantity to buy: 5↵

Invalid quantity!

 

_ _ _ _

 

/ _ _ _ \

 

/ / \ \

/ / \ \

/ / \ \

\ \ / /

\ \ O O O O / /

 

\ \ _ _ _ / /

 

\ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

|_ _ _ _|

Enter quantity to buy: 4↵

 

_ _ _ _

 

/ _ _ _ \

 

/ / \ \

/ / \ \

/ / \ \

\ \ / /

\ \ / /

 

\ \ _ _ _ / /

 

\ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

|_ _ _ _|

Sold out!CSCI1120 Introduction to Computing Using C++, Fall 2024/25

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2024 CSE, CUHK

Page 6 of 8

Enter side length: 5↵

Machine capacity: 40

Enter gumball stock: 25↵

 

_ _ _ _ _

 

/ _ _ _ _ \

 

/ / \ \

 

/ / \ \

/ / \ \

/ / O O O O O O O \ \

\ \ O O O O O O O / /

\ \ O O O O O O / /

 

\ \ O O O O O / /

 

\ \ _ _ _ _ / /

 

\ _ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

| |

 

|_ _ _ _ _|

Enter quantity to buy: 26↵

Invalid quantity!

 

_ _ _ _ _

 

/ _ _ _ _ \

 

/ / \ \

 

/ / \ \

/ / \ \

/ / O O O O O O O \ \

\ \ O O O O O O O / /

\ \ O O O O O O / /

 

\ \ O O O O O / /

 

\ \ _ _ _ _ / /

 

\ _ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

| |

 

|_ _ _ _ _|

Enter quantity to buy: 0↵

Invalid quantity!

 

_ _ _ _ _

 

/ _ _ _ _ \

 

/ / \ \

 

/ / \ \

/ / \ \

/ / O O O O O O O \ \

\ \ O O O O O O O / /

\ \ O O O O O O / /

 

\ \ O O O O O / /

 

\ \ _ _ _ _ / /

 

\ _ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

| |

 

|_ _ _ _ _|

Enter quantity to buy: 10↵CSCI1120 Introduction to Computing Using C++, Fall 2024/25

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2024 CSE, CUHK

Page 7 of 8

 

_ _ _ _ _

 

/ _ _ _ _ \

 

/ / \ \

 

/ / \ \

/ / \ \

/ / \ \

\ \ O O O O / /

\ \ O O O O O O / /

 

\ \ O O O O O / /

 

\ \ _ _ _ _ / /

 

\ _ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

| |

 

|_ _ _ _ _|

Enter quantity to buy: 5↵

 

_ _ _ _ _

 

/ _ _ _ _ \

 

/ / \ \

 

/ / \ \

/ / \ \

/ / \ \

\ \ / /

\ \ O O O O O / /

 

\ \ O O O O O / /

 

\ \ _ _ _ _ / /

 

\ _ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

| |

 

|_ _ _ _ _|

Enter quantity to buy: 10↵

 

_ _ _ _ _

 

/ _ _ _ _ \

 

/ / \ \

 

/ / \ \

/ / \ \

/ / \ \

\ \ / /

\ \ / /

 

\ \ / /

 

\ \ _ _ _ _ / /

 

\ _ _ _ _ _ /

 

| _ |

 

| |_| |

 

| |

 

| |

 

|_ _ _ _ _|

Sold out!CSCI1120 Introduction to Computing Using C++, Fall 2024/25

Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright © 2024 CSE, CUHKPage 8 of 8

Submission and Marking

  • Your program file name shall be gumball.cpp. Submit the file in Blackboard(https://blackboard.cuhk.edu.hk/).

nsert your name, student ID, and e-mail as comments at the beginning of your source file.// CSCI1120 Assignment 2// Name:// Student ID:// Email: (the one that you check most often)

  • You can submit your assignment multiple times. Only the latest submission counts.
  • Your program shall be free of compilation errors and warnings when built in VS Community 2022.
  • Your program shall include suitable comments as documentation.
  • Do NOT share your work to others and do NOT plagiarize. Both senders and plagiarists shall bepenalized.
posted @ 2024-10-01 17:51  n58h2r  阅读(1)  评论(0编辑  收藏  举报