首页 > 编程语言 >CSCI1120 Introduction to Computing Using C++

CSCI1120 Introduction to Computing Using C++

时间:2024-10-01 17:50:13浏览次数:8  
标签:Introduction C++ 2024 length program gumball Enter Using side

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.

标签:Introduction,C++,2024,length,program,gumball,Enter,Using,side
From: https://www.cnblogs.com/comp9021/p/18442743

相关文章

  • C++类型参数化
     C++程序设计语言继承于C程序设计语言并且增加面向对象的程序设计思想。面向过程的编程和面向对象的开发设计编程思想的区别在于数据的处理类型。C语言的程序设计基于基础的数据类型。结构体struct的概念设计是基础数据类型数据的组合。C++程序设计的开发增加类class的数据构建......
  • GESP C++四级样题卷
    (满分:100分考试时间:90分钟)PDF试卷及答案回复:GESPC20234一、单选题(每题2分,共30分)1.在C++中,指针变量的大小(单位:字节)是()A2B4C8D与编译器有关2.以下哪个选项能正确定义一个二维数组()Ainta[][];Bcharb[][4];Cdoublec[3][];Dboold[3][......
  • C++和OpenGL实现3D游戏编程【连载12】——游戏中音效的使用
    1、游戏中音效的使用前面我们实现了图片纹理的显示功能,是不是感觉到非常的简单。那么今天我们就继续说下游戏声音的实现。音效也是游戏的灵魂,只有搭配了美妙动听的音效以后,游戏才能令人耳目一新,与玩家产生良好的效果。音效文件最常用的可分为两种,分别为.wav和.mp3后缀的......
  • 南沙C++信奥赛陈老师解一本通题 1983:【19CSPJ普及组】公交换乘
    ​ 【题目描述】著名旅游城市B市为了鼓励大家采用公共交通方式出行,推出了一种地铁换乘公交车的优惠方案:1、在搭乘一次地铁后可以获得一张优惠票,有效期为 4545 分钟,在有效期内可以消耗这张优惠票,免费搭乘一次票价不超过地铁票价的公交车。在有效期内指开始乘公交车的时间......
  • 每日OJ题_牛客_DP2跳台阶_动态规划_C++_Java
    目录牛客_DP2跳台阶_动态规划题目解析C++代码Java代码牛客_DP2跳台阶_动态规划跳台阶_牛客题霸_牛客网题目解析        当前值只和数组的前两个值有关,在往前面的就无关了,所以没必要申请一个数组,直接使用两个变量即可,这样空间复杂度就满足要求了。C++代码......
  • Protobuf 为什么这么快?解密它背后的高效编码机制与 C++ 实践
    目录1.Protobuf的基本使用1.1定义`.proto`文件1.2生成C++代码2.Protobuf的二进制编码机制2.1Varint编码:更少的字节,更高的效率2.2字段编号与键:精准定位每个数据3.C++序列化与反序列化示例3.1序列化示例3.2反序列化示例4.性能对比与优化分析4.1数据......
  • dev c++ cout中文显示不出来怎么办
    比如你随便创建了一个项目,起初“牛逼”这两个字应该是不显示出来的,但是当你的光标在这一行会时显示出来,等你编译运行时控制台也是乱码。点击工具(tool)选择第一个编译选项,填入:“-fexec-charset=GBK”并勾选选择第三个,编辑器选项取消这个勾选,点击确定就好了......
  • Qt/C++音视频开发 - Onvif时间设置
    Qt/C++音视频开发-Onvif时间设置介绍Onvif(OpenNetworkVideoInterfaceForum)是一种开放的网络视频接口标准,旨在实现不同品牌设备之间的互操作。为了确保网络摄像头和其他视频设备的时间同步,Onvif提供了时间设置功能,这对于准确记录事件和协调多个设备的活动至关重要。......
  • C++(关键字)
    5.作用域限定符::5.1名字空间(掌握)名字空间是一种代码的层级划分。#include<iostream>usingnamespacestd;//C++课程中几乎所有的类型(不包括基本数据类型)都在std中inta=1;//新建一个名字空间namespacemy_space{inta=3;strings="哈哈......
  • C++在游戏开发中的卓越性能:优势解析与代码示例
    在游戏开发领域,C++一直是一种备受青睐的编程语言。它以其高性能、灵活性和强大的功能集,成为了游戏开发者的首选语言之一。在本文中,我们将深入探讨C++在游戏开发中的优势,并提供一些代码示例来展示这些优势是如何在实际开发中发挥作用的。高性能与低级控制C++提供接近硬件......