Sorting a dynamically created array

From CS100 Homework 2-2

题目描述

After Prof. Wang has got the average score, he wants to sort the scores in non-descending order (from small to large numbers). In this problem, you are also required to input a series of scores based on the user’s input. Then you will implement the bubble sort algorithm to sort these scores, and display them in non-descending order.

The pseudo-code of the Bubble Sort algorithm is given below:

procedure bubble_sort (A: list of sortable items)
	n = length(A)
	for i = 0 to n - 1 
		for j = 1 to n-1
			if A[j-1] > A[j] then 
				swap(A[j-1], A[j])
			end if
		end for
	end for
end procedure

In this problem, you will get some lines of students’ information. A line contains a student's ID number and his/her score, separated by a whitespace (' '). The input ends with a line of "-1 -1". As Prof. Wang doesn't know the number of his students, you will also need a dynamic memory allocation, and the rules of initializing your array and resizing your array are the same as for Problem 1. To store the students' ID numbers and scores, you may need two dynamic arrays, and they should all obey the previously introduced resizing rules.

Note: If two students have same scores, you should sort them by their student ID numbers, in non-descending order.

The following is a demo process for the program if you can do it successfully: (red indicates input)

image

Explanation of two "(resize) ..." lines: Because you use two arrays, each will resize after the 5th input, so your program will output two lines of "(resize) ...".

问题输入

Input contains unknown lines, each line represents a student number and a score, separated by a whitespace.
The student numbers are n different integers. They may look big, but they will not exceed the maximum limit of int.
The scores are n floating point numbers with at most 1 decimal point digit.

问题输出

First, you should output a prompt: Please type scores to be calculated:
Note that there should be no whitespaces after the colon, and you should end this prompt with a newline.
Then, once you need to resize your array, output a message like this: (resize) from 5 to 10
Then, after getting all the inputs, you should output ---Result--- ended with a newline.
Finally, output students’ information (student numbers and scores) in a non- descending order. If two students have the same score, you should sort them by their student numbers, in non-descending order.

* All scores should be printed with "printf" to only 1 decimal point digits floating point number, even if a score is an integer, and all student numbers should be printed with "printf" to integers.

样例输入1
2099581001 92.6
2099583065 70
2099581022 66.3
2099581020 66.3
2099583199 51.9
2099000000 99.6
-1 -1



样例输出1
Please type scores to be calculated:
(resize) from 5 to 10
(resize) from 5 to 10
---Result---
2099583199 51.9
2099581020 66.3
2099581022 66.3
2099583065 70.0
2099581001 92.6
2099000000 99.6
在线测试 (语言:C;时间限制:1s)
当前状态

无提交记录

有话想说?