正在加载图片...
-3- 1b)Suppose that you have been assigned to take over a project from another programmer who has just been dismissed for writing buggy code.One of the functions you have been asked to rewrite has the following comment and prototype: /者 Function:InsertValue Usage:Insertvalue(value,array,n,max)i This function takes four parameters: ★ 1.A new value to be inserted in the array (value) 2.An integer array sorted in ascending order (array) 3.The effective size of the array (n) 4.The allocated size of the array (max) The effect of the function is to insert value at its proper position in the array.When the function returns,the new effective size of the array will be n+1.If there is no space for the value,the Insertvalue function generates an error message. */ void Insertvalue(int value,int array[],int n,int max); Unfortunately,the corresponding implementation is buggy and looks like this: void Insertvalue(int value,int array[],int n,int max) int i,pos; if (n max)Error("No space in array"); for (i=0;i<n;i++){ if (value array[i]){ pos i; break; } } for (i=pos;i<n;i++){ array[i +1]array[i]; array[i]value; Circle the bugs in the implementation and write a sentence or two explaining the precise nature of each problem you identify.– 3 – 1b) Suppose that you have been assigned to take over a project from another programmer who has just been dismissed for writing buggy code. One of the functions you have been asked to rewrite has the following comment and prototype: /* * Function: InsertValue * Usage: InsertValue(value, array, n, max); * ----------------------------------------- * This function takes four parameters: * * 1. A new value to be inserted in the array (value) * 2. An integer array sorted in ascending order (array) * 3. The effective size of the array (n) * 4. The allocated size of the array (max) * * The effect of the function is to insert value at * its proper position in the array. When the function * returns, the new effective size of the array will * be n+1. If there is no space for the value, the * InsertValue function generates an error message. */ void InsertValue(int value, int array[], int n, int max); Unfortunately, the corresponding implementation is buggy and looks like this: void InsertValue(int value, int array[], int n, int max) { int i, pos; if (n > max) Error("No space in array"); for (i = 0; i < n; i++) { if (value > array[i]) { pos = i; break; } } for (i = pos; i < n; i++){ array[i + 1] = array[i]; } array[i] = value; } Circle the bugs in the implementation and write a sentence or two explaining the precise nature of each problem you identify
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有