2 dimensional array

Aug 18, 2010 at 3:22pm
Hi there
I've just started learning 2d arrays and I'm trying to assign
the 2nd vertical element and the 4th horizontal element
with values but it only assigns and displays the 2nd vertical
element (you know the first dimension):

// 2 dimensional arrays
#include <iostream>
using namespace std;

int main() {

int two_dimensional[3][5];
two_dimensional[1][3] = 46, 45;
cout << two_dimensional[1][3];
cin.get();
return 0;
}

Lyle
Aug 18, 2010 at 4:00pm
two_dimensional[1][3] = 46, 45;
What are you trying to do here? What do you think this line should do?
Aug 18, 2010 at 4:35pm
two_dimensional[1][3] can only hold one value.

two_dimensional[1][3] = 46, 45;
is like
1
2
two_dimensional[1][3] = 46;
45;
Aug 18, 2010 at 5:24pm
I think you lack basic knowledge of what a multi-dimensional array is. A 3 by 5 array of integers only stores 15 integers (3*5), in 3 rows and 5 columns. Each element does not store multiple integers.

To set the element at row a, column b:

two_dimensional[a][b] = 1;

To access the element at row a, column b:

int i = two_dimensional[a][b];
Aug 18, 2010 at 6:42pm

Hi there I seem to have gotten it right, what I wanted to do was:
assign row 1, column 3 with: 46
assign row 2, column 1 with: 56

// 2 dimensional arrays
#include <iostream>
using namespace std;

int main() {

int two_dimensional[3][5];
two_dimensional[1][3] = 46;
two_dimensional[2][1] = 56;
cout << two_dimensional[1][3] << endl << two_dimensional[2][1];
cin.get();
return 0;
}

Thanks for the posts:)
Topic archived. No new replies allowed.