正在加载图片...
2)Pointer Function Traces For the following code segments,you are asked to draw the state of the computer's memory during code execution.Trace through,starting from main and stop at the point marked.Be sure to distinguish between stack memory and heap memory.Local variables are allocated in the stack,and all dynamically allocated memory is in the heap.Label all the parts of the drawing: include the names of variables,note uninitialized or garbage values/pointers with a question mark,and indicate whenever memory is orphaned.Note that this code is purposely written to be confusing.When you write code using pointers,it should not look like this. int main() { int i; double **cherry,*orange; double raspberry[4]; cherry &orange; orange =&raspberry[1]; for(i=1;i<4;i++) raspberry[i]=i4; **cherry 3.0; RainbowSherbet(orange,*cherry,raspberry[3]): } void RainbowSherbet(double *pineapple,double *lime,int frozen) { lime new double [3]; for (frozen 0;frozen <*pineapple;frozen++) *(lime frozen)=frozen/2; //<-Draw the state of memory here 3)Pointers and Arrays Write a function CountEvensAndOdds which calculates the number of odd and even numbers in an array of integers.CountEvensAndOdds takes four parameters:the array of integers,the size of the array,and pointers to two integers so that the number of even and odd integers may be returned by reference.Write the prototype,and then write the function. Using your CountEvensAndOdds function from part(a),write a function Partition which takes an array of integers and its effective size,and returns two new integer arrays and their respective sizes by reference.The first new array should contain all those integers of the original array which are even,and the second array should contain all those elements of the original array which are odd.The original array passed in should not be at all changed.What's this function's prototype? ● The following program,presumably designed to test your functions above,does not work.In fact,Thetis interrupts the execution complaining that an uninitialized variable is being passed to Partition.In two or three sentences,identify the problems with the test program and then explain how all the errors should be corrected so that the program runs properly.2) Pointer Function Traces For the following code segments, you are asked to draw the state of the computer’s memory during code execution. Trace through, starting from main and stop at the point marked. Be sure to distinguish between stack memory and heap memory. Local variables are allocated in the stack, and all dynamically allocated memory is in the heap. Label all the parts of the drawing: include the names of variables, note uninitialized or garbage values/pointers with a question mark, and indicate whenever memory is orphaned. Note that this code is purposely written to be confusing. When you write code using pointers, it should not look like this. int main() { int i; double **cherry, *orange; double raspberry[4]; cherry = &orange; orange = &raspberry[1]; for (i = 1; i < 4; i++) { raspberry[i] = i*4; } **cherry = 3.0; RainbowSherbet(orange, *cherry, raspberry[3]); } void RainbowSherbet(double *pineapple, double *lime, int frozen) { lime = new double [3]; for (frozen = 0; frozen < *pineapple; frozen++) { *(lime + frozen) = frozen/2; } // <-Draw the state of memory here } 3) Pointers and Arrays z Write a function CountEvensAndOdds which calculates the number of odd and even numbers in an array of integers. CountEvensAndOdds takes four parameters: the array of integers, the size of the array, and pointers to two integers so that the number of even and odd integers may be returned by reference. Write the prototype, and then write the function. z Using your CountEvensAndOdds function from part (a), write a function Partition which takes an array of integers and its effective size, and returns two new integer arrays and their respective sizes by reference. The first new array should contain all those integers of the original array which are even, and the second array should contain all those elements of the original array which are odd. The original array passed in should not be at all changed. What's this function's prototype? z The following program, presumably designed to test your functions above, does not work. In fact, Thetis interrupts the execution complaining that an uninitialized variable is being passed to Partition. In two or three sentences, identify the problems with the test program and then explain how all the errors should be corrected so that the program runs properly
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有