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..
-
Can anyone explain when i try accessing this 2D array like this all the elements change.
-
Is there another way to access 2D array elements without using a loop.
Thank you all.
1
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 2×2 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 %dn", fly[0][0], fly[0][1], fly[1][0], fly[1][1]); // prints '1 2 3 4'
2
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]
.
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?
3