资源描述
我爱新浪微博
精品资料
实验4 数组
本次实验将练习数组的用法。完成一个程序,该程序要求用户输入整数,并按照输入顺序记录进一个数组。当用户输入-1时标识结束输入结束。程序将记录的数组的顺序进行反序,即原数组第一个数与最后一个数进行交换,依此类推。反序后的数组进行输出在屏幕上。最后对数组进行从小到大排序,并输出排序结果。程序的一次运行结果如下。你可以运行作业材料中附带的已经完成的array.exe程序来了解更多的该程序的输入和输出要求。
Enter numbers, one per line, ending with the
sentinel value -1 The program will then
display those values in reverse order and sorted order.
? 9
? 3
? 7
? 9
9 is already in the array. Please input again
? 3
3 is already in the array. Please input again
? 1
? 2
? 6
? 19
? -1
The reverse array is as follows:
19 6 2 1 7 3 9
The sorted array is as follows:
1 2 3 6 7 9 19
作业提示:
1. 该程序的主程序和常量定义已经在作业材料的array.cpp中给出,你不能进行改变。
2. 程序中需要完成的函数的功能和使用方法如下,你可以增添新的函数,但是你必须在主程序中正确地写出这些函数的原型并实现这些函数的功能。
/*
* Function: GetIntegerArray
* Usage: n = GetIntegerArray(array, max, sentinel);
* -------------------------------------------------
* This function reads elements into an integer array by
* reading values, one per line, from the keyboard. The end
* of the input data is indicated by the parameter sentinel.
* The caller is responsible for declaring the array and
* passing it as a parameter, along with its allocated
* size. The value returned is the number of elements
* actually entered and therefore gives the effective size
* of the array, which is typically less than the allocated
* size given by max. If the user types in more than max
* elements, GetIntegerArray generates an error.
*/
/*
* Function: PrintIntegerArray
* Usage: PrintIntegerArray(array, n);
* -----------------------------------
* This function displays the first n values in array,
* one per line, on the console.
*/
/*
* Function: ReverseIntegerArray
* Usage: ReverseIntegerArray(array, n);
* -------------------------------------
* This function reverses the elements of array, which has n as
* its effective size. The procedure operates by going through
* the first half of the array and swapping each element with
* its counterpart at the end of the array.
*/
/*
* Function: SwapIntegerElements
* Usage: SwapIntegerElements(array, p1, p2);
* ------------------------------------------
* This function swaps the elements in array at index
* positions p1 and p2.
*/
/*
* Function: GiveInstructions
* Usage: GiveInstructions();
* --------------------------
* This function gives instructions for the array reversal program.
*/
/*
* Function: SortIntegerArray
* --------------------------
* This implementation uses an algorithm called selection sort,
* which can be described in English as follows. With your left
* hand, point at each element in the array in turn, starting at
* index 0. At each step in the cycle:
*
* (1) Find the smallest element in the range between your left
* hand and the end of the array, and point at that element
* with your right hand.
*
* (2) Move that element into its correct index position by
* switching the elements indicated by your left and right
* hands.
*/
/*
* Function: FindSmallestInteger
* Usage: index = FindSmallestInteger(array, low, high);
* -----------------------------------------------------
* This function returns the index of the smallest value in the
* specified array of integers, searching only between the index
* positions low and high, inclusive. It operates by keeping track
* of the index of the smallest so far in the variable spos. If the
* index range is empty, the function returns low.
*/
/*
* Function: SearchArray
* Usage: bool = SearchArray(array, n, value);
* -----------------------------------------------------
* This function search the specified array of integers. If value is in the specified array
* then return ture, otherwise return false.
*/
3. 函数SortIntegerArray要求采用选择排序法对数组进行排序,这是一种简单的排序算法。你可以在学习了课本7.8节的插值排序算法后完成选择排序算法,并比较它们的不同。选择排序算法的具体算法在函数说明里有详细描述。
4. 函数SearchArray是对数组元素进行查找,你可以采用课本第7章7.7节中学到的简单的线性查找算法来实现。对于一个已经排序的数组你可以考虑用更好的搜索算法来实现该函数。阅读课本第20章获得对于排序和搜索算法的更多知识。
仅供学习与交流,如有侵权请联系网站删除 谢谢5
展开阅读全文