这个作业是用C++完成一些小练习

SEHH2042 Computer Programming

Mid-term Assignment

To answer the questions, you are allowed to insert codes into the functions as specified in

the template file.

There should be NO BLANK LINE in your output, except those near the program selection

menu as given in main( ) of the template file.

IMPORTANT: You will get ZERO mark if your file CANNOT be opened, or your

program CANNOT be compiled.

IMPORTANT: Do not display unnecessary message to ask for user input. This will cause

you to fail in all test cases. The format of your program output should be identical to that

of the sample display.

SEHH2042 19-20 Semester 2 – Mid-term Assignment

1

ShowInfo

Insert your code in the showInfo function so that your personal particulars are displayed in the

following format when the program executes:

Sample display:

Name

Student ID: 19xxxxxxA

Class : 201A

: XXX YYY ZZZ

Question 1 (25%)

Write a program to accept a weekday and a time (composed of hour and minute) in 24-hour

format. The user further inputs a number of minutes to be added to (or subtracted from) the

time. Then it outputs the resulted day and time in 2-digit 24-hour format.

3

^{r}^{d }digit of your student IDOdd

Number of minutes to be …

Added

Even

Subtracted

User inputs:

1

2

3

4

. day (a string, abbreviation of weekdays: Sun, Mon, Tue, Wed, Thu, Fri, Sat)

. hour (an integer from 0 to 23, inclusive)

. minute (an integer from 0 to 59, inclusive)

. number of minutes (a non-negative integer)

Program outputs:

1

. day and time in 2-digit 24-hour format

Sample display (for odd)

Sample display (for even)

Mon 12 34

2468

Mon 12 34

2468

Wed 05 42

Sat 19 26

Remark:

•

•

2468 minutes = 1 day 17 hours 8 minutes.

The number of minutes to add (or subtract) can be any non-negative integer. The

result may be many weeks later (or before).

•

The output time is displayed in 2-digit 24-hour format.

SEHH2042 19-20 Semester 2 – Mid-term Assignment

2

Question 2 (25%)

Consider the definition below of a good integer:

A k-digit non-negative integer is said to be good if its value equals to the sum

of its own digits each raised to the power of k. Examples of good integers are

0

, 1, 5, 153, 1634:

1

= 0

1

0

1

5

=1

1

= 5

3

3

3

1

1

53 =1 +5 +3

4

4

4

4

634 =1 + 6 + 3 + 4

Write a program that accepts a range (two non-negative integers a and b) from user. Then it

displays two numbers, one at a line. The first number is the number of good integers in the

range (from a to b, or from b to a, depending on whether a or b is smaller), inclusively. The

second number is the sum of …

4

^{t}^{h }digit of your student IDThe sum of …

the largest good integer and the second smallest

good integer in the range

Odd

the second largest good integer and the smallest

good integer in the range

Even

User inputs:

1. Two non-negative integers specifying the range

Program outputs:

1

2

. Number of good integers in the range

. The sum of two selected good integers according to the table above

Sample display (for odd)

Sample display (for even)

1 1000

13

1

1

4

1000

3

09

372

Assumption: The user always inputs a range that contains at least two good integers.

Remark: If there are just two good integers only, then second smallest is the same as largest

and second largest is the same as smallest.

SEHH2042 19-20 Semester 2 – Mid-term Assignment

3

Question 3 (25%)

Suppose that there are

n

integer numbers x

_{1}, x_{2},, x_{n}, whose values are determined by thefollowing table.

5

^{t}^{h }digit of your student IDValues of the

n numbers

2

Odd

x

_{i }= 20i +10i − 20 , for all i =1,2,,n2

x

_{i }=15i +11i −3.

.

Even

,

for all i =1,2,,n

From the

n

numbers above, we want to select (without replacement) three numbers to be the

sides of a triangle. The three numbers can form a triangle only if the sum of every two numbers

is larger than the value of the remaining number. We say that these three numbers are in a

triangular group.

Write a program that accepts an input

n , which is a positive integer at least 3. It then outputs

the number of triangular groups that can be formed among the

n numbers x

_{1}, x_{2},, x_{n}.User inputs:

1

. An integer n (at least 3)

Program outputs:

1

. Number of triangular groups can be formed

Sample display (for odd)

Sample display (for even)

10

10

16

34

Remark: The ordering of the numbers in a group does not matter. For example, 3,4,5 and 5,4,3

are considered the same group.

SEHH2042 19-20 Semester 2 – Mid-term Assignment

4

Question 4 (25%)

Write a program to accept user inputs of some integers, until the user inputs 0 to indicate the

end of input. Then, according to the given inputs (excluding the final 0, which is used to

indicate the end of input only), calculate the value of the following expression:

2

_{}

_{i }log

_{1}

_{0}| x

_{i}| −

,

where

x

_{m}_{a}_{x }− x_{m}_{i}_{n}+1•

i

x

_{i }is the -th input integer from user (excluding the final 0), and | x_{i}| is the absolutevalue of x

_{i }is the mean (i.e., average) of the input integers (excluding the final 0)

•

•

•

x

_{m}_{a}_{x }is the maximum among the input integers (excluding the final 0)x

_{m}_{i}_{n }is the minimum among the input integers (excluding the final 0)User inputs:

1

. Some integers, ends with 0

Program outputs:

2

. Value of expression calculated using input integers (except 0)

Sample display

1

4

–

8

–

0

–

23

5

67

9

2.07997

Assumption: The user will enter at least one non-zero integer before entering 0.

Remark:

•

•

•

As indicated above, the final input value 0 is not used in the calculation.

The calculation result (floating point value) is displayed in the default format.

you may use the log10(x) function in the <cmath> library to calculate log

_{1}_{0}(x) , anduse other functions whenever appropriate.

•

recall that the notation

is the summation sign. For example, if we have inputs

n

x

_{1}, x_{2},, x_{n}, then_{ }_{i }x

_{i }= x_{1}+ x_{2}+ + x_{n}.SEHH2042 19-20 Semester 2 – Mid-term Assignment

5

Submission

You are required to insert your C++ code into the given template file, and submit the final

source file to Moodle before the deadline. Use your student name and ID as the filename:

StudentID_Name.cpp. Remove all spaces, hyphens and other non-letter characters in the

filename. A correct filename should look like: 12345678A_ChanTaiMan.cpp.

Grading

Your program (i.e. the template file with your answers) will be executed by script with different

test cases in Microsoft Visual Studio. You need to follow EXACTLY the above input and

output requirements. Any deviation from the requirement is considered as incorrect and no

mark is given for that case.

Late submission: 100% deduction. No late submission is allowed. Submit your work to

Moodle some time ahead of the deadline. Late submissions due to slow internet speed will not

be accepted.

Syntax error: 100% deduction. You will get 0 mark if your program fails to be compiled.

Check your final source file using Microsoft Visual Studio (not those online compliers)

carefully before submission.

Runtime error: No mark for the particular test case that triggers the runtime error.

Logic error (bug): No mark for the particular test case that deviates from the requirement.

Note that a logic error may lead to failure in ALL test cases of a question, e.g. displaying

prompt messages, incorrect spelling and/or number format, or incorrectly decide the odd/even

version, etc.

Ensure the originality of your work. Plagiarism in any form is highly prohibited.

–

End –

SEHH2042 19-20 Semester 2 – Mid-term Assignment

6

Appendix

If you implement the questions in separated source files, you need to copy the corresponding

program codes into the template file for assignment submission. Make sure to test the final

source file (i.e. template file with your answers) in Microsoft Visual Studio before

submission.

myQuestion1.cpp

1

#

#

include <iostream>

include <iomanip>

using namespace std;

int main() {

int number = 1234;

2

cout << “This is appendix\n”;

cout << “Display a number: ” << setw(5) << number;

return 0;

}

## 发表评论