CSCI1120代写、代做C++设计程序
CSCI1120 Introduction to Computing Using C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong
Assignment 2: Gumball Machines
Due: 23:59, Thu 3 Oct 2024 File name: gumball.cpp Full marks: 100
Introduction
The objective of this assignment is to let you practice control flow structures in C++. It also involves the use of variables, operators, expressions, and standard input/output to reinforce your learning in the course thus far. You are to write a program to print an ASCII character pattern resembling the drawing of a gumball vending machine, such as Figure 1 below.
container
_____ /____\ // \\ // \\ // OOOOO\\ //OOOOOOO\\ \\OOOOOOO// \\OOOOOO// \\OOOOO// \\____// \_____/ |_|
| |_| |
𝑛 = side length of the hexagon gum
gum flap (chute door)
𝑛 = side length of the square
stand || || |_ _ _ _ _|
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'.
2. 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 Copyright © 2024 CSE, CUHK
Page 1 of 8

CSCI1120 Introduction to Computing Using C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong
Instead of hardcoding, you are required to use loops and conditionals to print the drawing whose size 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 the number of underscores that form the outer edge of the hexagon.
2. 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 number of gumballs that can be put into the container, i.e., its capacity (𝑐), is limited and determined by the following formula (deduced from the sum formula of an arithmetic series):
𝑐 = 3𝑛! − 8𝑛 + 5 ... (1)
Input validation
1. 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 line wrapping. So, let us assume its valid range is between 3 and 29. (Note: in case you still see line wrapping issues in this range, you may resize your terminal via its settings.)
2. The initial number gumballs (𝑠𝑡𝑜𝑐𝑘) to load into the machine must lie between ⌊𝑐/2⌋ (floor of the division) and 𝑐, inclusive.
If the user input falls outside the valid range, the program will terminate immediately with an error message. See the Sample Runs section.
Size Scaling
Table 2 shows some examples to explain how the container shape and its capacity (𝑐) scale with the side length (𝑛) input.
Note that for making the width and height of the hexagon (or square) look similar in the console, we put a single space between every two underscores or two letter O’s in a horizontal line. For better visualization of the spaces required to produce the output, we used the symbol ␣ to denote a space character.
Machine Operations
Besides printing the gumball machine, the program will also prompt the user to enter a quantity of gumballs to buy. When the user enters a valid value (between 1 and 𝑠𝑡𝑜𝑐𝑘), the quantity will be deducted 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.
Copyright © 2024 CSE, CUHK Page 2 of 8

CSCI1120 Introduction to Computing Using C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong
Table 2: Sample output versus side length (𝑛) 𝒏3456
𝒄8 21 40 65
␣␣␣␣_␣_␣_ ␣␣/␣␣_␣_␣␣\ ␣/␣/␣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:
a. Determine the left padding, i.e., how many spaces to print before /, \ or | per row. b. Print the hexagonal part.
c. Align the current stock of gumballs properly inside the container.
d. Print the square part.
7. Prompt the user for the quantity 𝑞 to buy.
8. If 𝑞 is invalid (not between 1 and 𝑠𝑡𝑜𝑐𝑘), print an error message and go back to step 6.
9. Deduct 𝑞 from 𝑠𝑡𝑜𝑐𝑘.
10. If 𝑠𝑡𝑜𝑐𝑘 > 0, go back to step 6.
11. 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.
Copyright © 2024 CSE, CUHK Page 3 of 8

CSCI1120 Introduction to Computing Using C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong
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 of characters if you see fit) but you cannot use its at() method or the subscript operator [] to traverse the individual characters of a string. 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 the program 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\\ \\OOOOO// \\OOOO// \\___// \____/ |_|
| |_| | ||
|_ _ _ _|
Enter quantity to buy: 1↵ Copyright © 2024 CSE, CUHK
Page 4 of 8

CSCI1120 Introduction to Computing Using C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong
____ /___\ // \\ // \\ // \\ \\OOOOO// \\OOOO// \\___// \____/ |_|
| |_| | ||
|_ _ _ _| Enter quantity to ____ /___\ // \\ // \\ // \\ \\ // \\OOOO// \\___// \____/ |_|
| |_| | ||
|_ _ _ _| Enter quantity to Invalid quantity! ____ /___\ // \\ // \\ // \\ \\ // \\OOOO// \\___// \____/ |_|
| |_| | ||
|_ _ _ _| Enter quantity to ____ /___\ // \\ // \\ // \\ \\ // \\ // \\___// \____/ |_|
| |_| | ||
|_ _ _ _|
Sold out!
buy: 5↵
buy: 5↵
buy: 4↵
Copyright © 2024 CSE, CUHK
Page 5 of 8

CSCI1120 Introduction to Computing Using C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong
Enter side length: 5↵ Machine capacity: 40 Enter gumball stock: 25↵
_____ /____\ // \\ // \\ // \\ //OOOOOOO\\ \\OOOOOOO// \\OOOOOO// \\OOOOO// \\____// \_____/ |_|
| |_| | || ||
|_ _ _ _ _|
Enter quantity to buy: 26↵ Invalid quantity!
_____ /____\ // \\ // \\ // \\ //OOOOOOO\\ \\OOOOOOO// \\OOOOOO// \\OOOOO// \\____// \_____/ |_|
| |_| | || ||
|_ _ _ _ _|
Enter quantity to buy: 0↵ Invalid quantity!
_____ /____\ // \\ // \\ // \\ //OOOOOOO\\ \\OOOOOOO// \\OOOOOO// \\OOOOO// \\____// \_____/ |_|
| |_| | || ||
|_ _ _ _ _|
Enter quantity to buy: 10↵ Copyright © 2024 CSE, CUHK
Page 6 of 8

CSCI1120 Introduction to Computing Using C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong
_____ /____\ // \\ // \\ // \\ // \\ \\ OOOO// \\OOOOOO// \\OOOOO// \\____// \_____/ |_|
| |_| | || ||
|_ _ _ _ _|
Enter quantity to buy: 5↵ _____
/____\ // \\ // \\ // \\ // \\ \\ // \\ OOOOO// \\OOOOO// \\____// \_____/ |_|
| |_| | || ||
|_ _ _ _ _|
Enter quantity to buy: 10↵ _____
/____\ // \\ // \\ // \\ // \\ \\ // \\ // \\ // \\____// \_____/ |_|
| |_| | || ||
|_ _ _ _ _|
Sold out!
Copyright © 2024 CSE, CUHK
Page 7 of 8

CSCI1120 Introduction to Computing Using C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong
Submission and Marking
§ Your program file name shall be gumball.cpp. Submit the file in Blackboard (https://blackboard.cuhk.edu.hk/).
§ Insert 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 be
penalized.
Copyright © 2024 CSE, CUHK Page 8 of 8

热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图