正在加载图片...
1b)Debug as a compiler(20 points) 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: /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 the array is full,the insertValue displays an error message then quit. Unfortunately,the corresponding implementation is buggy and looks like this: void Insertvalue(int value,int array[],int n,int max)//n shoulb be &n(3 pts) { int i=0,pos; bool quit false; if (n>max) /should be n =max (4 pts) console.runTimeError("No space in array"); while (!quit &&i<n) if (value array[i]) /should be value array[i](4 pts) E pos =i; quit true; else i++; } for (i=pos;i<n;i++)//should be (i=n-1;i>=pos;i++)(6 pts) array[i 1]array[i]; } array[i]value; //missing n++;(3 pts) } Circle the bugs in the implementation and write a sentence or two explaining the precise nature of each problem you identify.You don't need to write the correct version:just pointing out the wrong one and gave some explains.1b) Debug as a compiler (20 points) 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: /* * 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 the array is full, the insertValue displays an error message then quit. */ Unfortunately, the corresponding implementation is buggy and looks like this: void InsertValue(int value, int array[], int n, int max) // n shoulb be &n (3 pts) { int i = 0, pos; bool quit = false; if (n > max) // should be n == max (4 pts) console.runTimeError("No space in array"); while (!quit && i < n) { if (value > array[i]) // should be value < array[i] (4 pts) { pos = i; quit = true; } else i++; } for (i = pos; i < n; i++) // should be (i = n-1; i >= pos; i++) (6 pts) { array[i + 1] = array[i]; } array[i] = value; // missing n++; (3 pts) } Circle the bugs in the implementation and write a sentence or two explaining the precise nature of each problem you identify. You don’t need to write the correct version; just pointing out the wrong one and gave some explains
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有