简体   繁体   中英

2-D Array Processes?

I'm sure this is a pretty dumb question, but one of the questions on my homework is throwing me off (not looking for the answer, just some clarification on what the question might be asking). My professor tends to word things in a way that I've never heard things phrased before, so this question is kind of confusing me.

The questions goes like this:

Consider the following declarations:
final int carTypes = 5;
final int colorTypes = 6;
double [][] sales = new double[carTypes][colorTypes];

To sum the sales by carTypes, what kind of processing is required?
To sum the sales by colorTypes, what kind of processing is required?

Again, I'm not asking for the answer, I've just never come across a question worded this way so was hoping someone could point me in the right direction as to what this is asking. Thanks in advance.

Think of it like accessing a spreadsheet. The rows are car types, and the columns are color types.

To get all of the values of either one, you're iterating over some portion of the 2D array - either in a row-major or column-major fashion.

For example, if I wanted to add up all of the sales of a particular car type, I would iterate across the structure given an anchor value for the car type I want.

The loop would look something like this:

double sum = 0;
int carTypeDesired = 2;
for(int i = 0; i < sales[carTypeDesired].length; i++) {
    sum += sales[carTypeDesired][i];
}

It's a similar operation to do the summation by a specific color type, as well.

If you wanted a grand total of all cars, independent of make or color, then you would loop over all elements in the array and add them together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM