Weird wording in an exercise...?

Posted by Terry on Fri 25 Dec 2009 08:52 PM — 7 posts, 30,069 views.

USA #0
I'm going through Deitel's C++ How to Program, and I came across this rather strange exercise...
Quote:
7.16 Label the elements of a 3-by-5 one-dimensional array sales to indicate the order in which they are set to zero by the following program segment:
for( size_t row = 0; row < 3; row++ )
   for( size_t column = 0; column < 5; column++ )
      sales[row][column] = 0;


They're saying sales is a one-dimensional array, but it's written as T sales[3][5];. I'm kind of unsure what this is supposed to mean... Did it mean a one-dimensional array of arrays? Or was it just a typo? I wasn't sure what to make of it, and I was hoping someone might be able to shed some light on it. :D

Here's what I came up with:
int *firstArraySet  = sales[0];
int *secondArraySet = sales[1];
int *thirdArraySet  = sales[2];
USA #1
To test it, I wrote this little thing, and got the following output:
[brainfrz@li100-29 ~]$ cat eval.cpp
#include <cstdlib>
#include <iomanip>
#include <iostream>


int main()
{
    int t[3][5] = { { 0 }, { 0 }, { 0 } };

    const int *a1 = t[0];
    const int *a2 = t[1];
    const int *a3 = t[2];

    std::cout << "First pointer:  { " << a1[0] << ", " << a1[1] << ", " << a1[2] << " }\n";
    std::cout << "Second pointer: { " << a2[0] << ", " << a2[1] << ", " << a2[2] << " }\n";
    std::cout << "Third pointer:  { " << a3[0] << ", " << a3[1] << ", " << a3[2] << " }\n";

    return EXIT_SUCCESS;
}


Output:
[brainfrz@li100-29 ~]$ ./eval
First pointer:  { 0, 0, 0 }
Second pointer: { 0, 0, 0 }
Third pointer:  { 0, 0, 0 }


My thing works, but I'm not sure if it's answering the question...?


Oh, and Merry Christmas, all! :D
Amended on Fri 25 Dec 2009 09:04 PM by Terry
USA #2
A 2D array of dimensions (m,n) is represented in memory as a one-dimensional array of size m*n.
I think the question is to take the one-dimensional representation, and label each cell with the order in which it is set to zero.
USA #3
Hmm, in that case, would this be what it's probably looking for?

int &sale1  = sales[0][0], &sale2  = sales[0][1], &sale3  = sales[0][2], &sale4  = sales[0][3], &sale5  = sales[0][4];
int &sale6  = sales[1][0], &sale7  = sales[1][1], &sale8  = sales[1][2], &sale9  = sales[1][3], &sale10 = sales[1][4];
int &sale11 = sales[2][0], &sale12 = sales[2][1], &sale13 = sales[2][2], &sale14 = sales[2][3], &sale15 = sales[2][4];


By "label", I assumed it meant "reference", because references can be used as "aliases".
Amended on Fri 25 Dec 2009 10:29 PM by Terry
Australia Forum Administrator #4
I take the original question to be a typo, as it doesn't make sense to talk about a multi-dimensional (3x5), one-dimensional array. I presume the word "one" was left there by mistake.

As for the answer, I would say that as columns is the inner loop, they will be set more quickly than rows. So I would answer with a 2-dimensional graph, showing that 0,0 was set first, 0,1 second, and so on. And if I didn't get a good score I would complain. ;)

So more or less what Terry said in his post before mine, without all the extra stuff setting variables, eg.


sales[0][0], sales[0][1], sales[0][2], sales[0][3], sales[0][4];
sales[1][0], sales[1][1], sales[1][2], etc.


The question didn't say anything about assigning variables, nor did it say to "write some C code ...".
Amended on Sat 26 Dec 2009 02:26 AM by Nick Gammon
USA #5
By the way, I certainly hope that this isn't something being asked for homework or something like that... it kind of smells like it might be.

Quote:
By "label", I assumed it meant "reference"

Since the question didn't ask to write any code, I think 'label' just means 'label' as in literally put labels next to the array elements.
USA #6
David Haley said:
By the way, I certainly hope that this isn't something being asked for homework or something like that... it kind of smells like it might be.


Nah, it isn't. As I said, I'm going through Deitel's book. Anyway, thanks to everyone for helping me. It's much appreciated! :D