上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review

CS106A Handout #37 Summer 2003 August 7,2003 Pointer Practice 1)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. a void RainbowSherbet (double *pineapple,double *lime,int frozen) lime (double *GetBlock(3 sizeof(double)); for (frozen =0;frozen *pineapple;frozen++)( *(lime frozen)=frozen 2; <-Draw the state of memory here main() int i; double **cherry,*orange; double raspberry[4]; cherry &orange; orange =&raspberry[1]; fox(i=1;i<4:1++){ raspberry[i]i*4; **cherry =3.0; RainbowSherbet (orange,*cherry,raspberry[3]);
CS106A Handout #37 Summer 2003 August 7, 2003 Pointer Practice 1) 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. a) void RainbowSherbet(double *pineapple, double *lime, int frozen) { lime = (double *) GetBlock(3 * sizeof(double)); for (frozen = 0; frozen < *pineapple; frozen++) { *(lime + frozen) = frozen / 2; } <—Draw the state of memory here } 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]); }

-2- b) main() int i,*pl,**p2; int array[6]; pl array; p2=&p1; for(i=0;i<3i++){ p1[i]=i; p1++; } *p2 GetBlock(3 sizeof (int)); *p1++=42: Draw a diagram indicating the contents of memory at this point. c) main ( int i; double *kimball,branner[3]; for(i=0;i<3;i++)( branner[i]=6 /(i+1); HousingDraw(&kimball,branner[2],branner); Draw a diagram indicating the contents of memory at this point. void HousingDraw(double **flomo,int lamaison,double wilbur[]) *flomo GetBlock(lamaison sizeof(double)); wilbur++; *(wi1bur+1)=4; for (lamaison 0;lamaison *wilbur;lamaison++){ wilbur [lamaison]wilbur[lamaison]2; }
- 2 - b) c) main() { int i, *p1, **p2; int array[6]; p1 = array; p2 = &p1; for (i = 0; i < 3; i++) { p1[i] = i; p1++; } *p2 = GetBlock(3 * sizeof (int)); *p1++ = 42; Draw a diagram indicating the contents of memory at this point. } main() { int i; double *kimball, branner[3]; for (i = 0; i < 3; i++) { branner[i] = 6 / (i + 1); } HousingDraw(&kimball, branner[2], branner); Draw a diagram indicating the contents of memory at this point. } void HousingDraw(double **flomo, int lamaison, double wilbur[]) { *flomo = GetBlock(lamaison * sizeof(double)); wilbur++; *(wilbur + 1) = 4; for (lamaison = 0; lamaison < *wilbur; lamaison++) { wilbur[lamaison] = wilbur[lamaison] / 2; } }

3- 2)Pointers and Arrays(Final Exam Question,Spring 98) a)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. b)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 that 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? c)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. void Init(int array[],int n) { int i; Randomize ()i for (i 0;i<n;i++)( array[i]RandomInteger(1,1000); main ( int numbers [1000]; int **odds,**evens; int *numodds,*numEvens; Init(numbers,1000); Partition(numbers,1000,evens,numEvens,odds,numodds); FreeBlock(evens); FreeBlock (odds);
- 3 - 2) Pointers and Arrays (Final Exam Question, Spring 98) a) 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. b) 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 that 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? c) 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. void Init(int array[], int n) { int i; Randomize(); for (i = 0; i < n; i++) { array[i] = RandomInteger(1, 1000); } } main() { int numbers[1000]; int **odds, **evens; int *numOdds, *numEvens; Init(numbers, 1000); Partition(numbers, 1000, evens, numEvens, odds, numOdds); FreeBlock(evens); FreeBlock(odds); }

-4- 3)Concat Now that you know how strings are really implemented,we want you to implement concat without using any functions from either "strlib.h"or the ANSI library string.h. a)First,as a warm-up,implement: int StringLength(string s); b)Now,implement concat.You are permitted to use stringLength,but no other functions from "strlib.h"or string.h.Here's the prototype: string Concat(string s1,string s2); 4)Common pointer mistakes a b) int *IndexArray (int n) int *IndexArray(int n) { { int array[n],i; int *array,i; for (i=0;i<n;i++){ for (i=0;i<n;i++)( array[i]i; array[i]i; } return (array)i return (array); } Each of these implementations is buggy in some way.Describe in English the error or errors that arise in each one
- 4 - 3) Concat Now that you know how strings are really implemented, we want you to implement Concat without using any functions from either "strlib.h" or the ANSI library string.h. a) First, as a warm-up, implement: int StringLength(string s); b) Now, implement Concat. You are permitted to use StringLength, but no other functions from "strlib.h" or string.h. Here's the prototype: string Concat(string s1, string s2); 4) Common pointer mistakes a) b) int *IndexArray(int n) int *IndexArray(int n) { { int array[n], i; int *array, i; for (i = 0; i < n; i++) { for (i = 0; i < n; i++) { array[i] = i; array[i] = i; } } return (array); return (array); } } Each of these implementations is buggy in some way. Describe in English the error or errors that arise in each one

-5- 5)Program Tracing:Meet the cast of Friends Draw the state of the computer's memory twice during code execution below.Trace through, starting from main,and stop at the first point marked before the call to Friends and draw the state of memory.Then execute the call to Friends and draw the state of memory afterwards. Note stack and heap,label variables and record fields,and indicate orphaned memory. typedef struct char monica [8]; string ross[3]; string *rachel; int **phoebe; int joey; moviestarRec; static void MyCopy(char *dst,char *src) int i; for(i=0;src[i]I=\0';1++){ dst[i]=src[i]; dst[i]src[i]; static void Friends(moviestarRec rosanne,moviestarRec *grace,int *ellen) int i; MyCopy (rosanne.ross [0],"bob"); *e11en-=2; grace->rachel--; grace->ross[0]SubString(*(rosanne.rachel -1),1,6); for (i=0;iphoebe [i]ellen; rosanne.ross[2]CopyString(rosanne.monica 3); main() { moviestarRec cast; MyCopy (cast.monica,"Joey"); cast.ross[0]cast.monica +3; cast.ross[1]Concat("Chandler","rules"); cast.rachel cast.ross 2; cast.joey cast.monica[2]-'a'; cast.phoebe (int **GetBlock(cast.joey sizeof(int *)) First draw the state of memory here Friends(cast,&cast,&cast.joey); And then again right here
- 5 - 5) Program Tracing: Meet the cast of Friends Draw the state of the computer’s memory twice during code execution below. Trace through, starting from main, and stop at the first point marked before the call to Friends and draw the state of memory. Then execute the call to Friends and draw the state of memory afterwards. Note stack and heap, label variables and record fields, and indicate orphaned memory. typedef struct { char monica[8]; string ross[3]; string *rachel; int **phoebe; int joey; } movieStarRec; static void MyCopy(char *dst, char *src) { int i; for (i = 0; src[i] != '\0'; i++) { dst[i]= src[i]; } dst[i] = src[i]; } static void Friends(movieStarRec rosanne, movieStarRec *grace, int *ellen) { int i; MyCopy(rosanne.ross[0], "bob"); *ellen -= 2; grace->rachel--; grace->ross[0] = SubString(*(rosanne.rachel - 1), 1, 6); for (i = 0; i phoebe[i] = ellen; } rosanne.ross[2] = CopyString(rosanne.monica + 3); } main() { movieStarRec cast; MyCopy(cast.monica, "Joey"); cast.ross[0] = cast.monica + 3; cast.ross[1] = Concat("Chandler","rules"); cast.rachel = cast.ross + 2; cast.joey = cast.monica[2] - 'a'; cast.phoebe = (int **) GetBlock(cast.joey * sizeof(int *)); ⇐ First draw the state of memory here Friends(cast, &cast, &cast.joey); ⇐ And then again right here
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review Solution.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter9 SQL in a server environment.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第一章 微机原理与接口技术绪论(朱兰娟).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(2/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(1/2).pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 2.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 3.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Solution for Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Solution for Practice Final 2.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_I.运算符重载.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_II.继承与派生.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_III.多态性.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_IV.异常处理与模板.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_V. 流库.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_VI.类与对象.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_VII.类与对象的高级操作.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_I.C语言概述.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_II.基本数据类型和运算.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_III.流程控制.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_IV.函数和程序结构.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_IX.文件.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_V.数组.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_VI.编译预处理.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_VII.指针.pdf