2d arrays

Hi I am a beginner and I want to make a 2d array and when I type in the coordinates it shows me the values of the array but I just can't put the values of the arrays into it. This is what I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int main()
{
    int tga;
    int m,n;
    int ary[3][3];
    ary[0][0]=3;
    ary[0][1]=5;
    ary[0][2]=7;
    ary[1][0]=3;
    ary[1][1]=16;
    ary[1][2]=23;
    ary[1][3]=32;
    ary[1][1]=21;
    ary[1][2]=23;
    ary[2][0]=45;
    ary[2][1]=56;
    ary[2][2]=64;
    cin>>tga;
    if (tga=00){cout<<ary[0][0];}
    else if (tga=01){cout<<ary[0][1];}
    else if (tga=02){cout<<ary[0][2];}
    else if (tga=03){cout<<ary[0][3];}
    else if (tga=10){cout<<ary[1][0];}
    else if (tga=11){cout<<ary[1][1];}
    else if (tga=12){cout<<ary[1][2];}
    else if (tga=20){cout<<ary[2][0];}
    else if (tga=21){cout<<ary[2][1];}
    else if (tga=22){cout<<ary[2][2];}


    return 0;
}


thank!!!
I just can't put the values of the arrays into it.

I'm sorry, I don't understand what you mean by that.

BTW, you can simplify lines 23-32:
1
2
3
4
  int r, c;
  r = tga / 10;
  c = tga % 10;
  cout << ary[r][c];

Try changing the = to == in the ifs and else ifs.
And line 16 is out of bounds.
Looks like trying to squeeze a quart into a pint pot.
storing 12 values into 9 spaces, something has to give.

line 26 is out of bounds too:
else if (tga=03){cout<<ary[0][3];}

Maybe something like this is what you wanted:
1
2
3
4
5
    int ary[4][3] = 
        {  3,  5,  7,
           3, 16, 23,
          32, 21, 23,
          45, 56, 64  };
Last edited on
Topic archived. No new replies allowed.