1) Explain how two-dimensional arrays are passed to functions. In your explanati
ID: 3645588 • Letter: 1
Question
1) Explain how two-dimensional arrays are passed to functions. In your explanation, include a two-dimensional array declaration and initialization, a function prototype that receives a two-dimensional array, a function header with an empty body that matches the function prototype, and demonstrate how a two-dimensional array is passed to the function as an argument.2)Compare and contrast constructors and destructors. Provide an example class that includes both a constructor and a destructor function.
Explanation / Answer
1.As with one-dimensional arrays, a two-dimensional array is automatically passed as a pass-by-reference parameter. Consider the function heading above. The following function heading also could have been used: void ReadScores (int labScores[][MAX_LABS], //OUT: student labs int& numStudents, //OUT: Number of students int& numLabs, //OUT: Number of labs ifstream& myin); //IN: Input file stream Notice the difference between these two function headings. For the labsScores array, the second heading does not include the number of rows required by the array but does include the number of columns. The explanation is simple -- when we pass an array the compiler need only know the size of its elements, not how many elements it has. As explained above, a two-dimensional array can be thought of as a one-dimensional array of elements (which just happen to be arrays themselves). Thus the compiler needs to know the size of one of the elements. Each element is a row of values so the compiler needs to know the type of elements and how many values (the number of columns) are in each row. In the labScores array, the number of columns is MAX_LABS. If one wants to print the entire set of scores, a nested loop similar to that above can be used. 2.a constructor allows you to execute some code immediately after an object is created, and a destructor allows you to execute code just before an object is destroyed. These methods are called automatically by REALbasic.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.