Topics: Computer programming, Floating point, Programming language Pages: 6 (993 words) Published: April 8, 2014
THE HONG KONG POLYTECHNIC UNIVERSITY
Department of Electronic and Information Engineering
Computer Programming (ENG236) Homework 1
(Note: the format of Programming Test 1 is very similar to this homework.) A. Background
Sorting is one of the most common operations in computing. There are many studies on the methods of sorting and many fast algorithms have been devised. Nevertheless, if the number of items to be sorted is not that many, some simple approaches can also be very effective. The following is one of them. Assume that A, B, C, D, E are 5 variables storing 5 numbers respectively; and we would like to show the numbers on the screen in descending order. It can be done using the procedure as follows.

1.
2.
3.
4.
5.

Initialize a variable number F that keeps a very large value (must be larger than those values of A, B, C, D, E) Search among the batch for the biggest number, which is smaller than the value of F Show that number on the screen

Save that number’s value to F and replace the value that number variable in the batch with a very small value Repeat steps 2, 3, and 4 until all the 5 numbers in the batch are shown on the screen.

By using C++ and the Visual Studio 2010 development environment, implement a simple sorting application program with details as follows.
Q1.

Start a new project with name “partQ1” to do this question. The project MUST be stored in the folder e:\temp\a\. It should be a console application that shows the following main menu: 1. Input data

2. Select operation
3. Quit
Users are expected to enter an integer of value 1, 2 or 3 only. When any other numbers (i.e. not characters) are entered, the program will display “Invalid input..... Please enter again!” and the main menu will be shown again. Note: It is assumed that the user must enter an integer; the program need not handle non-integer input.

If the user selects 3, the program will show a message “Goodbye!” and then end (return to the Command Prompt).
If the user selects 1, the program will show a message “Please enter 10 floating-point numbers.” Your program should then allow the user to enter 10 floating-point numbers and store them as 10 different variables. Get back to the main menu when the user finishes entering the numbers. If the user selects 2, the program will show another message “Please select the desired operation” and get back to the main menu.

---------------------------------- PASS line -----------------------------Q2.

Start a new project with name “partQ2” to do the following. The project MUST be stored in the folder e:\temp\a\. Modify the program developed for Q1 to perform the following tasks: It is assumed that the user must select option 1 before selecting option 2 for the first time. If the user selects 2 in the main menu, rather than showing the message as mentioned in Q1, a sub-menu should be shown as follows. a.

b.
c.
d.
q.

Find the
Find the
Show the
Show the
Return

maximum
minimum
numbers in descending order
numbers in ascending order

Please enter your choice (a, b, c, d, or q to return):
a.

b.
c.
d.
e.
f.

The user is expected to enter a character of ‘a’, ‘b’, ‘c’, ‘d’ or ‘q’ only. If any other characters are entered, the program will display “Invalid input..... Please enter again!” and then show this submenu again. If the user chooses ‘a’, the biggest of the 10 entered numbers should be shown on the screen. Get back to this sub-menu after the number has been shown.

If the user chooses ‘b’, the smallest of the 10 entered numbers should be shown on the screen. Get back to this sub-menu after the number has been shown.
If the user chooses ‘c’, show a message “Numbers in descending order: ” and get back to this sub-menu.
If the user chooses ‘d’, show a message “Numbers in ascending order: ” and get back to this submenu. If the user chooses ‘q’, do nothing but return to the main menu....