Assignment #10:Pointers (Due:Nov.31) Solve each of the following problems and turn your answer in on paper.You should work these problems through by hand.You should not use VC(nor any C compiler) to obtain answers to any of the problems in Part A of this assignment. Problem 1:Memory diagrams For each of the following programs,draw a diagram showing the structure of memory at the indicated points.In creating your diagrams,you should observe the following conventions: Your diagram must make clear whether each memory block is allocated on the stack of the heap by dividing the diagram into two regions,as illustrated in the example below. Whenever a memory location has been assigned a value,you should show the value; uninitialized memory locations should be left blank. ● You need not show memory that is allocated during the program but is later made inaccessible because all pointers to that data are lost. ● Remember to show the value of all variables that are currently active at each checkpoint. For example,if the program is int main() int *p,*q,r[3]; p=r; q new int[4]; *p+=17; //Draw a diagram indicating the contents of memory at this point. } your diagram should look like that shown bellow: Stack Heap 17 These examples are intentionally written in a cryptic fashion to make sure that you understand the details of pointer manipulation.When you write code of your own,it should not look like these examples.Note also that problem 1b requires you to draw four separate diagrams of memory at each of the four checkpoints
Assignment #10: Pointers (Due:Nov. 31) Solve each of the following problems and turn your answer in on paper. You should work these problems through by hand. You should not use VC (nor any C compiler) to obtain answers to any of the problems in Part A of this assignment. Problem 1: Memory diagrams For each of the following programs, draw a diagram showing the structure of memory at the indicated points. In creating your diagrams, you should observe the following conventions: z Your diagram must make clear whether each memory block is allocated on the stack of the heap by dividing the diagram into two regions, as illustrated in the example below. z Whenever a memory location has been assigned a value, you should show the value; uninitialized memory locations should be left blank. z You need not show memory that is allocated during the program but is later made inaccessible because all pointers to that data are lost. z Remember to show the value of all variables that are currently active at each checkpoint. For example, if the program is int main() { int *p, *q, r[3]; p = r; q = new int[4]; *p++ = 17; // Draw a diagram indicating the contents of memory at this point. } your diagram should look like that shown bellow: These examples are intentionally written in a cryptic fashion to make sure that you understand the details of pointer manipulation. When you write code of your own, it should not look like these examples. Note also that problem 1b requires you to draw four separate diagrams of memory at each of the four checkpoints
Problem 1a) int main() int i; double *basic,pascal[3]: for (i=0;i=array &ptr1 array +ARRAY_SIZE) temp =*ptr2; *ptr1 =*ptr2--; *ptr1++=temp; } /Draw a diagram indicating the contents of memory at this point. *(--ptr1)=ARRAY_SIZE; for(i=0;i<ARRAY SIZE;i+=2) { *(ptr2+i-1)=i, /Draw a diagram indicating the contents of memory at this point
Problem 1a) int main() { int i; double *basic, pascal[3]; for (i = 0; i = array && ptr1 < array + ARRAY_SIZE) { temp = *ptr2; *ptr1 = *ptr2--; *ptr1++ = temp; } // Draw a diagram indicating the contents of memory at this point. *(--ptr1) = ARRAY_SIZE; for (i = 0; i < ARRAY_SIZE; i += 2) { *(ptr2 + i - 1) = i; } // Draw a diagram indicating the contents of memory at this point. }
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
int main() int numbers[1000]; int **odds,**evens: int *numOdds,*numEvens; Init(numbers,1000); Partition(numbers,1000,evens,numEvens,odds,numOdds); delete (evens); delete (odds); } void Init(int array[],int n) inti; RandomT randInt; for (i=0;i #include using namespace std /*Private function prototypes * static void Bizarre(int *p1,int *p2,int n); static int *Weird(int array[,int n); static void Checkpoint(int ckpt,int array[,int n); /*Statically initialized arrays * static int a10={100,200,300,400,500; static int a20={3,4,0.1.2};
int main() { int numbers[1000]; int **odds, **evens; int *numOdds, *numEvens; Init(numbers, 1000); Partition(numbers, 1000, evens, numEvens, odds, numOdds); delete (evens); delete (odds); } void Init(int array[], int n) { int i; RandomT randInt; for (i = 0; i #include using namespace std; /* Private function prototypes */ static void Bizarre(int *p1, int *p2, int n); static int *Weird(int array[], int n); static void Checkpoint(int ckpt, int array[], int n); /* Statically initialized arrays */ static int a1[] = { 100, 200, 300, 400, 500 }; static int a2[] = { 3, 4, 0, 1, 2 };
/*Main program*/ main( { int i,*p; Checkpoint(0,a1,5); Bizarre(a1,a2,5); Checkpoint(1,a1,5); p=Weird(Weird(a2,2),4); Checkpoint(2,a2,5); static void Bizarre(int *p1,int *p2,int n) int*p3,*array; array p3=new int[n]; while(n-->0) *p3++=p1[*p2+小 while(p3--I=array) p1[p3-array]=*p3; } static int *Weird(int array[],int n) int *p,i; p=array; for (i=1;i0) cout<<"," cout <array[i]; cout <<"HIn"; The Checkpoint function does exactly what its comments say it does:it display the contents of an array preceded by a checkpoint number.Thus,to answer this problem,all you have to do is show the output at each of the calls to Checkpoint.The results of the first call are filled in as an example. Checkpoint0:{100,200,300,400,500} Checkpoint 1: Checkpoint 2:
/* Main program */ main() { int i, *p; Checkpoint(0, a1, 5); Bizarre(a1, a2, 5); Checkpoint(1, a1, 5); p = Weird(Weird(a2, 2), 4); Checkpoint(2, a2, 5); } static void Bizarre(int *p1, int *p2, int n) { int *p3, *array; array = p3 = new int[n]; while (n-- > 0) *p3++ = p1[*p2++]; while (p3-- != array) p1[p3 - array] = *p3; } static int *Weird(int array[], int n) { int *p, i; p = array; for (i = 1; i 0) cout << ", "; cout << array[i]; } cout << " }\n"; } The Checkpoint function does exactly what its comments say it does: it display the contents of an array preceded by a checkpoint number. Thus, to answer this problem, all you have to do is show the output at each of the calls to Checkpoint. The results of the first call are filled in as an example. Checkpoint 0: { 100, 200, 300, 400, 500 } Checkpoint 1: Checkpoint 2:
Problem 4b) /*File:anotherptrtrace.c*/ #include using namespace std static void Mystery(int array[],int *ip,int k); static int *Enigma(int *p1,int *p2); static void Confusion(int **ap,int *list,int n): static void ResetArray(int array[],int n); static void Checkpoint(int checkNumber,int array[],int n); main( int array [8],*ip; ResetArray(array,8); Checkpoint(0,array,8); ip=&array[4]; Mystery(ip,ip,*ip); Checkpoint(1,array,8); ResetArray(array,8); ip Enigma(array +8,array); Checkpoint(2,ip,4); ResetArray(array,8); Confusion(&ip,array,4); Checkpoint(3,array,8); Checkpoint(4,ip,4); } static void Mystery(int array],int *ip,int k) while (k-->0) *ip--=array[k-1]; static int *Enigma(int *p1,int*p2) while (p1-p2 >2) *-p1=*p2++: return(p2); } static void Confusion(int **ap,int *list,int n) int i,*ip; "ap ip new int[n] for (i=0;i<n;i++) list++: *ip++=list[i]++;
Problem 4b) /* File: anotherptrtrace.c */ #include using namespace std; static void Mystery(int array[], int *ip, int k); static int *Enigma(int *p1, int *p2); static void Confusion(int **ap, int *list, int n); static void ResetArray(int array[], int n); static void Checkpoint(int checkNumber, int array[], int n); main() { int array[8], *ip; ResetArray(array, 8); Checkpoint(0, array, 8); ip = &array[4]; Mystery(ip, ip, *ip); Checkpoint(1, array, 8); ResetArray(array, 8); ip = Enigma(array + 8, array); Checkpoint(2, ip, 4); ResetArray(array, 8); Confusion(&ip, array, 4); Checkpoint(3, array, 8); Checkpoint(4, ip, 4); } static void Mystery(int array[], int *ip, int k) { while (k-- > 0) *ip-- = array[k-1]; } static int *Enigma(int *p1, int *p2) { while (p1 - p2 > 2) *--p1 = *p2++; return (p2); } static void Confusion(int **ap, int *list, int n) { int i, *ip; *ap = ip = new int[n] ; for (i = 0; i < n; i++) { list++; *ip++ = list[i]++; } }
Function:ResetArray Usage:ResetArray(array,n); 米 This function sets the first n elements of array to the first n integers starting with 1.Thus,calling *ResetArray(array,4)would set the first four elements of array to 1,2,3,and 4,respectively. static void ResetArray(int array[].int n) int i; for (i=0;i0) cout<<",": cout <array[i]; cout <<"HIn"; } The functions on this page of the exam do exactly what their comments say they do.In particular,the Checkpoint function displays the contents of an array preceded by a checkpoint number.Thus,to answer this problem,all you have to do is show the output at each of the calls to Checkpoint.The results of the first call are filled in as an example. Checkpoint0:{1,2,3,4,5,6,7,8} Checkpoint 1: Checkpoint 2: Checkpoint 3: Checkpoint 4:
/* * Function: ResetArray * Usage: ResetArray(array, n); * --------------------------------- * This function sets the first n elements of array to the * first n integers starting with 1. Thus, calling * ResetArray(array, 4) would set the first four elements * of array to 1, 2, 3, and 4, respectively. */ static void ResetArray(int array[], int n) { int i; for (i = 0; i 0) cout << ", "; cout << array[i]; } cout << " }\n"; } The functions on this page of the exam do exactly what their comments say they do. In particular, the Checkpoint function displays the contents of an array preceded by a checkpoint number. Thus, to answer this problem, all you have to do is show the output at each of the calls to Checkpoint. The results of the first call are filled in as an example. Checkpoint 0: { 1, 2, 3, 4, 5, 6, 7, 8 } Checkpoint 1: Checkpoint 2: Checkpoint 3: Checkpoint 4: