Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Addition for first question A multi-dimensional array consists of a collection of elements organized into mul- tiple dimensions. Individual elements are referenced by specifying an

image text in transcribed

Addition for first question

  1. A multi-dimensional array consists of a collection of elements organized into mul- tiple dimensions. Individual elements are referenced by specifying an n-tuple or a subscript of multiple components, (i1,i2,...in), one for each dimension of the array. All indices of the n-tuple start at zero.

    • MultiArray( d1, d2, . . . dn ): Creates a multi-dimensional array of elements or- ganized into n-dimensions with each element initially set to None. The number of dimensions, which is specified by the number of arguments, must be greater than 1. The individual arguments, all of which must be greater than zero, indicate the lengths of the corresponding array dimensions. The dimensions are specified from highest to lowest, where d1 is the highest possible dimension and dn is the lowest.

    • dims(): Returns the number of dimensions in the multi-dimensional array.

    • length( dim ): Returns the length of the given array dimension. The individ- ual dimensions are numbered starting from 1, where 1 represents the first, or highest, dimension possible in the array. Thus, in an array with three dimen- sions, 1 indicates the number of tables in the box, 2 is the number of rows, and 3 is the number of columns.

    • clear( value ): Clears the array by setting each element to the given value.

    • getitem ( i1, i2, . . . in ): Returns the value stored in the array at the element position indicated by the n-tuple (i1, i2, . . . in). All of the specified indices must be given and they must be within the valid range of the corresponding array dimensions. Accessed using the element operator: y = x[ 1, 2 ].

    • setitem ( i1, i2, . . . in, value ): Modifies the contents of the specified array element to contain the given value. The element is specified by the n-tuple (i1,i2,...in). All of the subscript components must be given and they must be within the valid range of the corresponding array dimensions. Accessed using the element operator: x[ 1, 2 ] = y.

Addition for second question

Next, suppose we want to compute the total sales for a given month that includes the sales figures of all items in all stores sold during that month. This value can be computed using the following function:

# Compute the total sales of all items in all stores for a given month.

def totalSalesByMonth( salesData, month ): # The month number must be offset by 1. m = month - 1 # Accumulate the total sales for the given month. total = 0.0

# Iterate over each store. 
salesData = MultiArray( 8, 100, 12 ) 

for s in range( salesData.length(1) ): # Iterate over each item of the s store. for i in range( salesData.length(2) ):

total += salesData[s, i, m] return total

This time, the two nested loops have to iterate over every row of every spread- sheet for the single column representing the given month. If we use this function to compute the total sales for the month of January, the elements of the 3-D array that will be accessed are shown by the shaded area in Figure 3.13(a).

Total Sales by Item

Another value that we can compute from the sales data in the 3-D array is the total sales for a given item, which includes the sales figures for all 12 months and from all 8 stores. This is computed by the following function:

# Compute the total sales of a single item in all stores over all months.

def totalSalesByItem( salesData, item ): # The item number must be offset by 1.

i = item - 1

 # Accumulate the total sales for the given month. 

total = 0.0

 # Iterate over each store. 

for s in range( salesData.length(1) ):

 # Iterate over each month of the s store. 

for m in range( salesData.length(3) ): total += salesData[s, i, m]

return total

The cells of the array that would be accessed when using this function to compute the total sales for item number 5 are shown by the shaded area in Fig- ure 3.13(b). Remember, the sales for each item are stored in a specific row of the array and the index of that row is one less than the item number since the indices start at 0.

Get the MultiArray ADT implementation in Section 3.3.1 to work as specified. Note that you need to implement the internal method named _computeFactors (). Demonstrate with sufficient examples of different dimensional arrays that your implementation works as specified. # Your answer goes here # Create as many CODE CELLS as you need Question 6 Show that all the function implemented in Section 3.4 (Application: Sales Reports) work as specified with sufficient examples. Using the random module, create an input file for n stores and m items. You need not use the same input file format as the textbook. For example, you can use the csv module to read the data into memory, or you can write your own method to read the input. Add the following functions to your implementation: total_yearly_sales ( sales_data ) should return an Array of size n that stores total sales for the whole year for each of the n stores in your system. total_monthly_sales( sales_data ) should return an Array of size 12 that stores total sales for all stores per month for a whole year. 1 Get the MultiArray ADT implementation in Section 3.3.1 to work as specified. Note that you need to implement the internal method named _computeFactors (). Demonstrate with sufficient examples of different dimensional arrays that your implementation works as specified. # Your answer goes here # Create as many CODE CELLS as you need Question 6 Show that all the function implemented in Section 3.4 (Application: Sales Reports) work as specified with sufficient examples. Using the random module, create an input file for n stores and m items. You need not use the same input file format as the textbook. For example, you can use the csv module to read the data into memory, or you can write your own method to read the input. Add the following functions to your implementation: total_yearly_sales ( sales_data ) should return an Array of size n that stores total sales for the whole year for each of the n stores in your system. total_monthly_sales( sales_data ) should return an Array of size 12 that stores total sales for all stores per month for a whole year. 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions