Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++) 1.What is the size of an Array? Give an example. 2.What is wrong with the

ID: 3801951 • Letter: #

Question

(C++)

1.What is the size of an Array? Give an example.

2.What is wrong with the following? Explain. Also give the corrected version.

int Test_Array[8];

cout << Test_Array;

3.What are global and local array elements initialized to? Give examples.

4.What happens with the following piece of code? Why? Explain in detail.

int tests[5] = {79,82,91,77,84};

for (int count = 0; count < 7; count++)

    {

    cout << tests[count] << endl;

    }

5.What is range-based for loop? Give an example.

6.Give an example of how you would initialize a two dimensional array.

7.Read page 426. Visualize a multi dimensional Array. Give an example where you would use a 4 dimensional array.

(PAGE 426)

7.10 Arrays with Three or More Dimensions

CONCEPT: C++ does not limit the number of dimensions that an array may have. It is possible to create arrays with multiple dimensions, to model data that occur in multiple sets.

C++ allows you to create arrays with virtually any number of dimensions. Here is an example of a three-dimensional array definition:

This array can be thought of as three sets of five rows, with each row containing eight elements. The array might be used to store the prices of seats in an auditorium, where there are eight seats in a row, five rows in a section, and a total of three sections.

Figure 7-18 illustrates the concept of a three-dimensional array as “pages” of two-dimensional arrays.

Figure 7-18

Arrays with more than three dimensions are difficult to visualize, but can be useful in some programming problems. For example, in a factory warehouse where cases of widgets are stacked on pallets, an array with four dimensions could be used to store a part number for each widget. The four subscripts of each element could represent the pallet number, case number, row number, and column number of each widget. Similarly, an array with five dimensions could be used if there were multiple warehouses.

When writing functions that accept multi-dimensional arrays as arguments, all but the first dimension must be explicitly stated in the parameter list.

Explanation / Answer

C++

1. Size of array is bound given during array declaration. for example int arr[5];

the size of arr is 5. Lower Bound =0 and Upper Bound=4.

------------------------------------------------------------------------------------------------------------------------------------------------

2.What is wrong with the following? Explain. Also, give the corrected version.

int Test_Array[8];

cout << Test_Array;

Ans: Here this cout will print the base address of Test_Array. To print element of this array, we have to specify the index.for example to print element at index 3rd of array (4th element)

cou<<Test_Array[3];

------------------------------------------------------------------------------------------------------------------------------------------------------

3.What are global and local array elements initialized to? Give examples.

Ans: By default global array will be initialized to zero and local array to garbage value.

----------------------------------------------------------------------------------------------------------------------------------------------------

4.What happens with the following piece of code? Why? Explain in detail.

int tests[5] = {79,82,91,77,84};

for (int count = 0; count < 7; count++)

    {

    cout << tests[count] << endl;

    }

Ans:it will give

79

82

91

77

54

34334

122

it will print garbage value for test[5] and test[6]. Because array Bound is 5. it has only location from 0 to 4. No memory location exists for index 5 and 6. so it will give garbage value.

------------------------------------------------------------------------------------------------------------------------------------------------------------

5.What is range-based for loop? Give an example.

int tests[5] = {79,82,91,77,84};

for (int count = 0; count < 5; count++)

{

cout << tests[count] << endl;

}

here range is for 0 to 4 for tests[5] array.

---------------------------------------------------------------------------------------------------------------------------------------------------------------

6.Give an example of how you would initialize a two-dimensional array.

int a[2][3]={2,1,3,4,2,10};

this array has 2 row and 3 column.

a[0][0] will be intilaized with 2

a[0][1] will be intilaized with 1;

a[0][2] will be intilaized with 3;

a[1][0] will be intilaized with 4;

a[1][1] will be intilaized with 2;

a[1][2] will be intilaized with 10;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------

7.Read page 426. Visualize a multi dimensional Array. Give an example where you would use a 4 dimensional array.

Ans 2D array can be visualized as Matrix consisting of rows and columns.

3 dimensions can be visualized as a cube,

4 dimensions can be visualized as a bunch of cubes in a line.

A practical use of a 4D array is to keep track of a 3d object, could keep track of [x-cord][y-cord][z-cord][time]. This 4D array would be a useful use of a 4D array. This could keep track of a range of cords and time, and the value in the array could say the speed of of the object.

That would record the speed of the object when it was at x cord 5, y cord 9, z cord 6, and say been alive for 1500 seconds.

---------------------------------------------------------------------------------------------------------------------------------------------------------------