using arrays

Hello,
I am learning how to use arrays in different ways.Can anyone please tell me what is wrong with this basic program.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;



int main()
{
	int matrix1[0][0]=1;
	int mattix2[1][0]=2;
	int matrix3[2][0]=3;
	int matrix4[0][1]=4;
	int matrix5[0][2]=5;
	int matrix6[1][1]=6;
	int matirx7[1][2]=7;
	int matrix8[2][1]=8;
	int matrix9[2][1]=9;

	cout << matrix1[0][0]<<mattix2[1][0]<< matrix3[2][0]<<endl;
    cout<<matrix4[0][1]<< matrix5[0][2]<<matrix6[1][1]<<endl;
	cout<<matirx7[1][2]<< matrix8[2][1]<< matrix9[2][1]<<endl;
    return 0;
}
You need to initialize your array correctly I.e

1
2
3
4
5
6
7
8
9
10
11
  int matrix[3][3];

  matrix[0][0] = 1;
  matrix[0][1] = 2;
  matrix[0][2] = 3;
  matrix[1][0] = 4;
  matrix[1][1] = 5;
  matrix[1][2] = 6;
  matrix[2][0] = 7;
  matrix[2][1] = 8;
  matrix[2][2] = 9;

try looking at this, I think it's where you were heading. Notice that you only need one variable, matrix, which store all your values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
	int matrix[3][3]; // this creates a 3 by 3 array named matrix;
	matrix[0][0]=1; // putting 1 in location 0, 0 of matrix;
	matrix[0][1]=2;
	matrix[0][2]=3;
	matrix[1][0]=4;
	matrix[1][1]=5;
	matrix[1][2]=6;
	matrix[2][0]=7;
	matrix[2][1]=8;
	matrix[2][2]=9;


	cout << matrix[0][0]<< matrix[0][1]<< matrix[0][2]<<endl; 
        cout << matrix[1][0]<< matrix[1][1]<< matrix[1][2]<<endl;
	cout << matrix[2][0]<< matrix[2][1]<< matrix[2][2]<<endl;
       return 0;
}
Last edited on
Thank you !
Can you please tell me how to make this program shorter by using any loop .I tried a lot ,It was so confusing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;



int main()
{
	int matrix[3][3];
	 matrix[0][0]=1;
	 matrix[1][0]=2;
	 matrix[2][0]=3;
	 matrix[0][1]=4;
	 matrix[0][2]=5;
	 matrix[1][1]=6;
	 matrix[1][2]=7;
	 matrix[2][1]=8;
	 matrix[2][1]=9;

	cout << matrix[0][0]<<matrix[1][0]<< matrix[2][0]<<endl;
    cout<<matrix[0][1]<< matrix[0][2]<<matrix[1][1]<<endl;
	cout<<matrix[1][2]<< matrix[2][1]<< matrix[2][1]<<endl;
    return 0;
}
blech @ multidimensional arrays

note that 2D arrays are generally [y][x], not [x][y]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
  int matrix[3][3] = {
    {1,2,3},
    {4,6,8},
    {5,7,9}
  };

  int x,y;
  for(y = 0; y < 3; ++y)
  {
    for(x = 0; x < 3; ++x)
      cout << matrix[y][x];
    cout << endl;
  }

  return 0;
}
Last edited on
Thank you ! this helps me understanding it in a different way.
I will appreciate if you tell me why is this program not showing the result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int main()
{
	int r1,r2,r3,r4,r5,r6,r7,r8,r9;
	cout<<"enter the matrix elements ";
	cin>>r1>>r2>>r3>>r4>>r5>>r6>>r7>>r8>>r9;
  int matrix[3][3] = {
    {r1,r2,r3},
    {r4,r6,r8},
    {r5,r7,r9}
  };

  int x,y;
  for(y = 0; y < 3; ++y)
  {
    for(x = 0; x < 3; ++x)
      cout << matrix[y][x];
    cout << endl;
  }

  return 0;
}
it worked for me. You need to enter the numbers with a space between them, because cin is extracting from the same stream of all your input. if you want it to input one at at time you'd have to have;
1
2
3
4
cin >> r1;
cin >> r2;
cin >> r3;
...
Thanks !
What should I do to enter the data in this program without spaces and without pressing enter .Like for this program if I enter without spaces It doesn't work and with spaces it works.So I will be real thankful if you tell me how to fix this problem.
Last edited on
Topic archived. No new replies allowed.