Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Notes 13.26 and 13.27: Main Driver File: #define INSTRUCTOR_FILE #ifdef INSTRUCTOR_FILE #include #include extern const int DIM0, DIM1, DIM2, DIM3; extern float ***pointerArray4D[]; int main()
Notes 13.26 and 13.27:
Main Driver File:
#define INSTRUCTOR_FILE
#ifdef INSTRUCTOR_FILE
#include
#include
extern const int DIM0, DIM1, DIM2, DIM3;
extern float ***pointerArray4D[];
int main()
{
int value;
// Store all values.
value = 0;
for (int ix0 = 0; ix0
for (int ix1 = 0; ix1
for (int ix2 = 0; ix2
for (int ix3 = 0; ix3
pointerArray4D[ix0][ix1][ix2][ix3] = float(value++);
// Test all stored values.
value = 0;
for (int ix0 = 0; ix0
for (int ix1 = 0; ix1
for (int ix2 = 0; ix2
for (int ix3 = 0; ix3
if (pointerArray4D[ix0][ix1][ix2][ix3] != value++)
{
std::cerr
std::cerr
C2A4E3 (6 points - C++ program) Also file Exclude any existing source code files that may already be in your IDE project and add a new one. C2A4E3_main-Driver.cpp. Do not write a main function! main already exists in the instructor-supplied file and it will use the code you write. File C2A4E3_pointerArray4D.cpp must contain a 4-dimensional pointer array of type float named pointerArray4D that you create using the method illustrated in notes 13.26 and 13.27. Specify the dimension sizes by defining identifiers DIMO. DIM1, DIM2, and DIM3 (lett-to-right), which must have values of 2, 3, 4, and 5, respectively. pointerArray4D must be accessible to any other file (e.g., the instructor-supplied source code file) while the names of any other arrays must only be accessible within file C2A4E3 pointerArray4D.cpp (note 5.14). Do not write any functions or macros or create any files other than C2A4E3_pointerArray4D.cpp. Submitting your solution Send both source code files to the assignment checker with the subject line C2A4E3_JD. where ID is your 9-character UCSD student ID. See the course document titled "How to Prepare and Submit Assignments" for additional exercise formatting, submission, and assignment checker requirements. Hints: There is no quick and easy way to fully test a pointer array. As a minimal test a different value can be stored into each element then read back to verify that no memory violations occur and that each element contains its original value. However, this does not guarantee that more memory than necessary hasn't been used or that an out-of-bounds access hasn't occurred. NOTE 13.26 Multidimensional Arrays vs. Pointer Arrays The indices used in a multidimensional array reference are converted to a pointer to the desired element by what is known as a "storage map equation". Due to the multiplication operations these equations require, program efficiency can suffer. In addition, some environments may be unable to allocate the large contiguous block of memory required for large arrays, even though the total storage needed may be available in smaller blocks To avoid these problems, pointer arrays (arrays of pointers) may be used. Instead of storage map equations, pointer arrays use multiple levels of indirection that often improve efficiency. The tradeoffs are that pointer arrays require somewhat more storage, limit data caching for the array, and prevent some compiler optimizations A ragged array is a special case of a pointer array The following examples illustrate multidimensional array versus pointer array declaration syntax for a specific case. Although differing in declaration syntax, access syntex is identical for both types. Note that the generated code" for each multidimensional array is its storage map equation Type of Array Declaration(s) vald References Become Code Generated multidimensional (i+1)=((y + ))*(&y00j + i*4 + i) int (314): int a[4], b[4). [4] int/3] = {a,b,c} pointer same as above *((y +i) +1) The first example creates the true multidimensional array y containing 12 ints and allocated as a contiguous block of memory as shown by the connection from the end of one row to the beginning of the next: int Iyo yogol YOU VIO2) int yo Y I 7111 . Y10 yo ine YE112 int y[13] 7121 im y[20 int Y1221 int 712131 The second example creates the int arrays a, b, and c, and pointer array y. The 3 elements of y are initialized to point to the first element of the corresponding int arrays. Although cach of the three int arrays occupies its own contiguous memory block, the individual blocks may not be adjacent to cach other yo YE yo YOP 10 For the expression int Y ya YUR It MI1 10 1201 401 421 YRID NOTE 13.27 Three-Dimensional Pointer Arrays Pointer arrays permit multidimensional array syntax to be used when accessing elements but do not require storage map equations. This can often improve performance const int DIMO = 2, DIMI = 3, DIM2 = 4: Instead of declaring as intz[DIMO][DIMI||DIM2); 11 = UWIK) generates "&z[0][0][0] +i*DIMIDIM2 +j*DIM2 + k) the following declarations and initializations permit to still be referenced using multidimensional syntax int va|DIM2), xb[DIM2], xe[DIM21, xd[DIM20, xe[DIM2], xf|DIM2] int tyr[DIM1] = {xa, b, c, *ys[DIM1] = {xd, xe, xf}, int** [DIMO] = (yr ys}; // (01/k/ generates **(z+i) +1) + k) The following diagram illustrates the resulting storage layout: 2009 E3 OPIO al 101 O ZOT 12 M 19 [2] 2012 O 2 O Forte erpression 22 O 2011 101 STRO EUR 001 MOTU 021 41 Z 211212 001 22 12 221 22 23 21 131 Z! To access one of the int elements the same syntax is used for z regardless of how it was declared: z[index][index 1][index2] = someValue: scanf("%d", &z[index][index][index2]). Some parting notes on pointer arrays: In actual practice pointer arrays are often dynamically created, which generalizes the process, Although references to multidimensional arrays and pointer arrays use the same syntax, their data types are different. They must always be declared properly, not only initially but also when declared as function parameters, extemals, typecasts, etc. For the examples above, the first is int (DIMO/DIMIJ DIM2); while the second is int **-/DIMO) return EXIT_FAILURE;
}
std::cout
return EXIT_SUCCESS;
}
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started