[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  Weird wording in an exercise...?
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Weird wording in an exercise...?

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Terry   USA  (87 posts)  [Biography] bio
Date Fri 25 Dec 2009 08:52 PM (UTC)
Message
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];
[Go to top] top

Posted by Terry   USA  (87 posts)  [Biography] bio
Date Reply #1 on Fri 25 Dec 2009 09:00 PM (UTC)

Amended on Fri 25 Dec 2009 09:04 PM (UTC) by Terry

Message
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
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #2 on Fri 25 Dec 2009 09:56 PM (UTC)
Message
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.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Terry   USA  (87 posts)  [Biography] bio
Date Reply #3 on Fri 25 Dec 2009 10:27 PM (UTC)

Amended on Fri 25 Dec 2009 10:29 PM (UTC) by Terry

Message
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".
[Go to top] top

Posted by Nick Gammon   Australia  (21,321 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sat 26 Dec 2009 02:24 AM (UTC)

Amended on Sat 26 Dec 2009 02:26 AM (UTC) by Nick Gammon

Message
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 ...".

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #5 on Sat 26 Dec 2009 07:05 AM (UTC)
Message
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.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Terry   USA  (87 posts)  [Biography] bio
Date Reply #6 on Sat 26 Dec 2009 04:55 PM (UTC)
Message
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
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


5,917 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]