Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I started learning arrays in C++ and came over a little side note in the book talking about 2D arrays in breif.

I tested it out and i was amazed that it could give the programmer the ability to store data or information and lay it out infront of him in a spread sheet format.

In a normal array to access elements i would simply do this:

int matrix[2] = { 1, 15};

But in 2D arrays :The only way it tells me to actually acces elements is by using a loop:

int fly[2][2];
int i = 0;
int n=0;
for(i=0; i<2; i++){
    for (n=0; n<2; n++){
        fly[i][n] =0;
    }
}
for(i=0; i<2; i++){
    for (n =0; n<2; n++){
        cout << fly[i][n]<< endl;
    }
}

I have tried accessing elements the old way:

int fly[2][2] = { 0};

but i noticed that this changes all the elements to 0

So..

  1. Can anyone explain when i try accessing this 2D array like this all the elements change.

  2. Is there another way to access 2D array elements without using a loop.

Thank you all.

share|improve this question
    
Not actually an answer, but I've found maintaining code that makes heavy use of multidimensional arrays is a horrible pain, especially if you didn't write it. It might make sense to make an object that represents a point with attributes (no idea what the dimensions in fly represent here. X and Y coordinates? Rows and Columns?) and create an array of those data objects. Simply accessing unnamed dimensions can be a mess for comprehensibility down the line. –  KChaloux Sep 5 '12 at 14:55

4 Answers 4

up vote 13 down vote accepted

In a normal array to access elements i would simply do this:

int matrix[2] = { 1, 15};

You're declaring an array with that code. You're also assigning '1' to the int at index 0 and '15' to the int at index 1.

I have tried accessing elements the old way:

int fly[2][2] = { 0};

but i noticed that this changes all the elements to 0

That code doesn't assign '0' to the int at coordinates 2, 2... it creates a 2x2 array and tries to initialize it with the array that you've provided. Since the array is smaller than the size of the array that you're assigning to, the rest of the elements are simply initialized to 0. If you'd used '{15}' instead, you'd get a '15' in the first position, and '0' everywhere else. Try this instead:

int fly[2][2] = {{1, 2}, {3, 4}};

or even just:

int fly[2][2] = {1, 2, 3, 4};

In those cases, enough values are provided for every element of fly. If you this, though:

int fly[2][2] = {1, 2, 3};

you'll find that fly[1][1] is 0 because you didn't provide enough data.

Can anyone explain when i try accessing this 2D array like this all the elements change.

You can't really say that they "changed". You're only just creating the array, so other than the initialization you don't know what values are in the array. At any point after you create the array, you can access the array using the two subscripts:

int fly[2][2] = {{1, 2}, {3, 4}};
printf("%d %d %d %d\n", fly[0][0], fly[0][1], fly[1][0], fly[1][1]);   // prints '1 2 3 4'
share|improve this answer
    
The style of the OP was NOT using printf. I should have been more precise in my statement as my objection is entirely to do with your style. Printf is valid C++, it's purely for backwards compatibility with last centuries coders. You choose to pollute the OP's example code with an obsolete programming style? –  mattnz Jul 23 '12 at 4:03
8  
@mattnz -- so everybody should abandon the compact, flexible and easy to use "printf()" for the verbosity and obscurity of "cout <<". –  James Anderson Jul 23 '12 at 6:37

This is an implementation question, so it should therefore be posted on StackOverflow.com , not Programmers.StackExchange.com.

int fly[2][2] = { 0}; is declaring an array (hence why it has int at the front) and assigning it (to {0}). It's been years since I've done any C++, but I'm somewhat surprised it actually compiles (I'll take your word for it), seeing as how you are assigning a one-dimensional array to a two-dimensional array.

Accessing the array (and outputting it to the screen) would be done by cout << fly[2][2].

share|improve this answer

You could have made your code for user friendly and generic by declaring constants first and then writing or declaring them in array.Like

    const int ROW=2;
    const int COL=2;
    int data_Array[ROW][Col];
share|improve this answer

int fly[2][2]={0} ; allocates and initialises an new array - it is not an accessor to an existing array. Study up in C++ Initialises and partial intialisation.

Question 2 : As far as acessing the array elements - you could use

 cout << fly[0][0]<< endl;
 cout << fly[0][1]<< endl;
 cout << fly[1][0]<< endl;
 cout << fly[1][1]<< endl;

This does not use a loop, the quesiton clear but probably not the question you want answered. - what part of using a loop do you have a problem with?

share|improve this answer
1  
Should probably not use endl as this causes an unnecessary flush "\n" is perfectly fine. If we want pedantic about style they should all be prefixed with std:: –  Loki Astari Jul 27 '12 at 19:02
    
@Loki : The OP used endl, and did not use std::. To be pedantic, your suggestions materially changes the meaning of the code the OP posted. –  mattnz Dec 14 '13 at 3:44
    
I am confused - can someone (down voters or otherwise) Please explain the votes, what is the problem with this answer, and why, after 16 months its suddenly a problem. –  mattnz Dec 15 '13 at 1:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.