Arrays/Literal Notation

G

Guest

int[, ,] array = { { { 1, 2, 3, 3 }}, { { 7, -1, 9, 0 } }, { { -1, 0, 7, 8 }
} }; // 3D

In the line of code above, am I using 'literal' syntax/notation to create
the array?

The rank of the array is 3, its dimensions are 3,1,4.

How would I change the code to produce

a. an array of dimension 3,2,4?
b. an array of rank 4?

Thanks.
 
B

Barry Kelly

AA2e72E said:
int[, ,] array = { { { 1, 2, 3, 3 }}, { { 7, -1, 9, 0 } }, { { -1, 0, 7, 8 }
} }; // 3D

In the line of code above, am I using 'literal' syntax/notation to create
the array?

The rank of the array is 3, its dimensions are 3,1,4.

Hint: count '3' '1' '4'. It's equal to 3.

Another hint:
* What is there 4 of in the top example?
* Given that you've gotten what there's 4 of, what is there only one of,
for its given level of nesting?
* Can you spot what there's 3 of now?
How would I change the code to produce

a. an array of dimension 3,2,4?
b. an array of rank 4?

Do you own homework - it's not hard!

-- Barry
 
G

Guest

Learn to read before jumping to prescribe.

My note says that I know the rank (number of dimensions) is 3; I go on to
tell you that the dimensions are 3,1,4. The question was how would I modify
the right hand side (RHS) such that the rank stayed as 3 but he dimensions
were 3,2,4 i.e how would I increase the second dimension to 1.

The second question was whether the RHS was a 'literal ' notation.

If you did not know, or cannot be bothered to understand what is written,
why do't you simply choose not to respond? Your answer adds nothing.

PS: I had bothered to research my own answers but came with nothing; hence
the post.


Barry Kelly said:
AA2e72E said:
int[, ,] array = { { { 1, 2, 3, 3 }}, { { 7, -1, 9, 0 } }, { { -1, 0, 7, 8 }
} }; // 3D

In the line of code above, am I using 'literal' syntax/notation to create
the array?

The rank of the array is 3, its dimensions are 3,1,4.

Hint: count '3' '1' '4'. It's equal to 3.

Another hint:
* What is there 4 of in the top example?
* Given that you've gotten what there's 4 of, what is there only one of,
for its given level of nesting?
* Can you spot what there's 3 of now?
How would I change the code to produce

a. an array of dimension 3,2,4?
b. an array of rank 4?

Do you own homework - it's not hard!

-- Barry
 
J

Jon Skeet [C# MVP]

AA2e72E said:
Learn to read before jumping to prescribe.

My note says that I know the rank (number of dimensions) is 3; I go on to
tell you that the dimensions are 3,1,4. The question was how would I modify
the right hand side (RHS) such that the rank stayed as 3 but he dimensions
were 3,2,4 i.e how would I increase the second dimension to 1.

The second question was whether the RHS was a 'literal ' notation.

If you did not know, or cannot be bothered to understand what is written,
why do't you simply choose not to respond? Your answer adds nothing.

Barry gave a very good answer, which led you through how to understand
the different dimensions.

I suggest you reread his answer, and work through it slowly - by the
end, you'll be able to understand how to answer the questions yourself.

What I would suggest is that you put each bracing level on a new line,
rather than the whole thing in one line. That might make it clearer.
 
P

Peter Bradley

Well, that's a pretty good way of ensuring that no-one will help you with
your homework. Congratulations. I hope the foot gets better soon.

Just for your info, in case you should ever post to a newsgroup again
seeking help with homework, you should ensure that you do the following:

* State clearly that you are looking for help with homework
* Explain what research you have carried out, what you have already tried
and what the results were.
* Assure the group that answers will be credited to the group or individual
giving the advice and that you will not try to pass off other people's work
as your own.

That's what I did when studying for my MSc recently. I got lots of help and
I was as good as my word in terms of crediting other people when they had
helped me out. I even posted my answers so that people could check it out
for themselves. This also got me a commendation from my tutor.

Oh! and finally. Remember that students can be dismissed for plagiarism,
and that professors also read newsgroups.



Peter


AA2e72E said:
Learn to read before jumping to prescribe.

My note says that I know the rank (number of dimensions) is 3; I go on to
tell you that the dimensions are 3,1,4. The question was how would I
modify
the right hand side (RHS) such that the rank stayed as 3 but he dimensions
were 3,2,4 i.e how would I increase the second dimension to 1.

The second question was whether the RHS was a 'literal ' notation.

If you did not know, or cannot be bothered to understand what is written,
why do't you simply choose not to respond? Your answer adds nothing.

PS: I had bothered to research my own answers but came with nothing; hence
the post.


Barry Kelly said:
AA2e72E said:
int[, ,] array = { { { 1, 2, 3, 3 }}, { { 7, -1, 9, 0 } }, { { -1, 0,
7, 8 }
} }; // 3D

In the line of code above, am I using 'literal' syntax/notation to
create
the array?

The rank of the array is 3, its dimensions are 3,1,4.

Hint: count '3' '1' '4'. It's equal to 3.

Another hint:
* What is there 4 of in the top example?
* Given that you've gotten what there's 4 of, what is there only one of,
for its given level of nesting?
* Can you spot what there's 3 of now?
How would I change the code to produce

a. an array of dimension 3,2,4?
b. an array of rank 4?

Do you own homework - it's not hard!

-- Barry
 
K

Kevin Spencer

int[, ,] array = { { { 1, 2, 3, 3 }}, { { 7, -1, 9, 0 } }, { { -1, 0, 7,
8 }
} }; // 3D

In the line of code above, am I using 'literal' syntax/notation to create
the array?

The rank of the array is 3, its dimensions are 3,1,4.

Since you already know what dimension and rank the first array declaration
is, it would be elementary for you to produce another according to the
specifications you've given, unless a teacher had given you this as a
problem, and you were simply trying to get someone to solve it for you. But
if that were the case, I would not be doing you any favor to solve it for
you, since programming is all problem-solving, and to solve your problem for
you would not give you any experience in problem-solving.

However, I will give you a link to a C# ECMA specification that you should
be able to use to figure it out for yourself:

http://www.jaggersoft.com/csharp_standard/toc.htm

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

I had the same problem once. Fixed it using the same solution.


AA2e72E said:
int[, ,] array = { { { 1, 2, 3, 3 }}, { { 7, -1, 9, 0 } }, { { -1, 0, 7,
8 }
} }; // 3D

In the line of code above, am I using 'literal' syntax/notation to create
the array?

The rank of the array is 3, its dimensions are 3,1,4.

How would I change the code to produce

a. an array of dimension 3,2,4?
b. an array of rank 4?

Thanks.
 
G

Guest

Peter, thanks for another <almost> useless answer; however, I now understand
the implication of 'homework' : I originally understood it to me 'do your own
research'

Let me assure you, this has nothing to do with homework; I am in my 50's
moving on from VB into C#. So far so good, but I do get stuck sometimes, as
in this situation.

I looked for the answers in "C# Language Reference" and did not find it;
search with Google and was not successful there either.

If you create a new project and put a button on it, and have this code in
its click event:

int[, ,] array = { { { 1, 2, 3, 3 }}, { { 7, -1, 9, 0 } }, { { -1,
0, 7, 8 } } }; // 3D
string xx="";
for (int i=0;i<array.Rank;i++)
{
xx += " " + (array.GetUpperBound(i)+1).ToString();
}
MessageBox.Show(xx);

You will have an insight and how I got the dimensions. I know that I need to
specify the elements of the next second dimension within the braces of the
RHS; tied several things and no success!

So, could someone just answer the questions, please?
 
G

Guest

Kevin, thanks; I try and find the answer using your link. I am sure it will
be there.

As you will see from a previous reply, I am not trying to get someone else
to solve it for me (I am not a student); in fact, for the context that I am
working on, I simply needed to establish that the array was square but got
side-tracked ( .. for something I need to do later).
I agree, that the best way to learn is to solve the problems oneself;
however, after a while, there is a tradeoff between simply asking and
persevering ...
 
M

Mythran

AA2e72E said:
int[, ,] array = { { { 1, 2, 3, 3 }}, { { 7, -1, 9, 0 } }, { { -1, 0, 7,
8 }
} }; // 3D

In the line of code above, am I using 'literal' syntax/notation to create
the array?

The rank of the array is 3, its dimensions are 3,1,4.

How would I change the code to produce

a. an array of dimension 3,2,4?
b. an array of rank 4?

Thanks.

When dealing with multi-dimensional arrays, it may be helpful to re-organize
the code of the array literals to help identify the different "layers" of
the array, such as:

int[, ,] array =
{
{
{ 1, 2, 3, 3 }
},
{
{ 7, -1, 9, 0 }
},
{
{ -1, 0, 7, 8 }
}
}; // 3D

Does this help you identify what your looking for? I didn't give it away,
but am trying to help you see what you need and come to the conclusion(s)
yourself. Try playing with it a bit...

HTH,
Mythran
 
G

Guest

Thanks, Mythran. I'll get there, looks like later rather than sooner!

I know that line breaks mean nothing in C# except when dealing with verbatim
strings and that splitting my expression as you suggest might help me think
this through.

I do not recall seeing any sample/example code in any book or on MSDN that
splits the expression for arrays over several lines. I wonder why?

My first language is APL; in case you do not know anything about it, it
excels at array handling ( for instance, the inverse of a square matrix is
returned in one line!).

Still, need to acquire new skills .....
Thanks everyone.
 
G

Guest

Figured out answers to my questions relating to dimension and rank; however,
whether or not my expression is using 'literal' notation, I don't know. The
espression 'literal notation' is used in JavaScript documentation when
arrays are created by an expression ([ instead of { in JavaScript) rather
than dimensioned and have each element populated via a loop. I'll assume this
carries through to C#.
 
B

Ben Voigt

AA2e72E said:
Figured out answers to my questions relating to dimension and rank;
however,
whether or not my expression is using 'literal' notation, I don't know.
The
espression 'literal notation' is used in JavaScript documentation when
arrays are created by an expression ([ instead of { in JavaScript) rather
than dimensioned and have each element populated via a loop. I'll assume
this
carries through to C#.

It's array initialization syntax, it cannot be used under any conditions
except to initialize an array. Unlike a numeric or string literal which
actually is useable as a number or string, the braces syntax cannot be used
in place of an array, hence it is not an "array literal". There is an array
literal syntax which is somewhat different and can be used anywhere an array
can, but that is not it.
 
M

Mark Wilden

Well, that's a pretty good way of ensuring that no-one will help you with
your homework. Congratulations. I hope the foot gets better soon.

Despite his protestations, I agree with you, Peter. This looks exactly like
a homework question to teach C#, rather than having the slightest practical
value (which is why he hasn't specified any).

Just my opinion, but I'm sticking with it.

///ark
 
G

Guest

Not only another self appointed high priest of PATRONISATION, or warden of
student morality but also an AUTHORITY on C#: take your stance to its natural
conclusion and tell Microsoft that arrays are of no practical value and that
they should take it out of the language and its documentation forthwith.
 
J

Jon Skeet [C# MVP]

AA2e72E said:
Thanks, Mythran. I'll get there, looks like later rather than sooner!

I know that line breaks mean nothing in C# except when dealing with verbatim
strings and that splitting my expression as you suggest might help me think
this through.

I do not recall seeing any sample/example code in any book or on MSDN that
splits the expression for arrays over several lines. I wonder why?

Because in real life code, 3-dimensional and 4-dimensional array
literals are pretty rare to start with.
 
G

Guest

They probably are.

The background to what I was doing was to use APL via Interop; APL is strong
on arrays permiting arithmetic on then (irrespective of dimensions or rank)
almost as if they were scalars if they were conformable.

When I have done this using Excel, I found that while APL arrays are row
major, Excel arrays are column major. I needed to figure out whether there
was a similar issue with C# arrays. Hence the need to create C# arrays to
pass into APL and vice versa in order to test. Fortunately, C# arrays are row
major too.

I needed to get to this conclusion quickly without the hurdle of lectures
and prescriptions from some quarters; this was a big surprise to me as I
normally see this on Microsoft basher sites. I refer to the Excel newsgroup
quite a lot: it is an absolute model for newsgroup: an objective for this
newgroup?
 
J

Jon Skeet [C# MVP]

I needed to get to this conclusion quickly without the hurdle of lectures
and prescriptions from some quarters; this was a big surprise to me as I
normally see this on Microsoft basher sites. I refer to the Excel newsgroup
quite a lot: it is an absolute model for newsgroup: an objective for this
newgroup?

You appeared to show no interest in learning how things worked, you
just snapped at someone who tried to help you. I bet that doesn't go
down well on the Excel newsgroup either.

Now, if you try to work *with* those who are trying to help you, you'll
find that there are lots of people who are prepared to put in a lot of
time helping you. Just remember that *they're* helping *you*, so you're
the one with something to lose from the conversation turning sour.
 
G

Guest

you just snapped at someone who tried to help you.

I assume you are refering to Barry's reply; it was valuable until his
statement "Do you own homework - it's not hard!" Originally, I took that to
mean do your own research rather than his suspicion that I might be a student
trying to get the forum to solve my problem.

You also invite me to realise that 'they' are helping 'me': If I did not
know that, I would not have posted the question and I don't see how "Do you
own homework - it's not hard!" was helping me.

I do not think newsgroup respondents have any business being prescriptive;
they can
1. ignore a post,
2. or resolve it,
3. or show a better resolution of a solution provided by another respondent,
4. or refer them to a link that has the solution,
5. or simply hint at a solution.

It becomes very interesting and a valuable source of learning when scenario
3 comes into play.

As far as I can see, that is how the Excel newsgroup works (have a look at
he responses from Tom Ogilvy, also an MVP). And, as I see it, none of us
actually invent solutions, we simply adopt or adapt what is there; we are all
learning, even MVPs are in the same boat albeit further downstream. So there
is little justification for claiming the high moral ground faced with
'simple' or 'naive' questions.
 
J

Jon Skeet [C# MVP]

AA2e72E said:
I assume you are refering to Barry's reply; it was valuable until his
statement "Do you own homework - it's not hard!"

And yet your reply included "Your answer adds nothing." Hardly a vote
of thanks, is it?
Originally, I took that to
mean do your own research rather than his suspicion that I might be a student
trying to get the forum to solve my problem.

No, it was saying: "I've given you all the tools you need to do your
homework, now come up with the answers yourselves."
You also invite me to realise that 'they' are helping 'me': If I did not
know that, I would not have posted the question and I don't see how "Do you
own homework - it's not hard!" was helping me.

You've already admitted that right until the last line, the response
was helpful.
I do not think newsgroup respondents have any business being prescriptive;

Have you noticed how you're being prescriptive yourself? You're trying
to tell the participants of this newsgroup how to behave. How is that
*not* prescriptive?
they can
1. ignore a post,
2. or resolve it,
3. or show a better resolution of a solution provided by another respondent,
4. or refer them to a link that has the solution,
5. or simply hint at a solution.

Similarly, you could have:
1) Ignored Barry's post
2) Acknowledged the value in it, but explained gently how it wasn't
homework in reply to the last line
3) Read the post carefully, several times, until you understood the
answer

Instead, your reply was basically rude.
It becomes very interesting and a valuable source of learning when scenario
3 comes into play.

As far as I can see, that is how the Excel newsgroup works (have a look at
he responses from Tom Ogilvy, also an MVP). And, as I see it, none of us
actually invent solutions, we simply adopt or adapt what is there; we are all
learning, even MVPs are in the same boat albeit further downstream. So there
is little justification for claiming the high moral ground faced with
'simple' or 'naive' questions.

Simple and naive questions are almost always answered kindly. Homework
questions (as yours *looked like*, even if it wasn't) are usually
answered in a way which would teach the student how to get the answer,
rather than by giving the answers themselves. From a few posts I've
just read on the Excel group (admittedly from 2004) it's an attitude
which is taken there as well.

No doubt it would have been better if Barry had put "We won't do your
homework, we'll just help you to do it yourself" instead of "Do you own
homework - it's not hard!" but I hardly think he deserved the reply you
gave. I suggest you reread that reply and consider how you might feel
if you'd received it from someone you'd been trying to help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top