Arrays are not them...

So our teacher asked us to do 2x2 matrix. This is my code for its interface.
However, arrays mat1[0][1] and mat1[1][0] are always equal. Whenever user input a value on mat1[1][0], it also changes the value for mat1[0][1].
How is that possible? Did I do something wrong?

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>

using namespace std;

int mat1[1][1];
string initial_choice;
void Matrix2x2();
void InitialInt();
void Err();

int main()
{
    InitialInt();

    return 0;
}

void Err()
{
    cout<< "\n\nINVALID INPUT!!\nPlease Try Again.\n\n";
}

void InitialInt()
{
    cout<< "This is a matrix displayer.\n"
        << "Please choose the matrix appearance below\n\n";
    cout<< "1. 2x2 Matrix\n"
        << "2. 3x3 Matrix";
    cout<< "\n\nYour input: ";
    cin >> initial_choice;

    if (initial_choice== "1")
    {
        Matrix2x2();
    }
    else if (initial_choice=="2")
    {

    }
    else
    {
        Err();
        InitialInt();
    }
}

void Matrix2x2()
{
    cout<< "\n\nPlease put your numbers to be put in the matrix.\n";
    cout<< "Digit 1: ";
    cin >> mat1[0][0];
    cout<< "Digit 2: ";
    cin >> mat1[0][1];
    cout<< "Digit 3: ";
    cin >> mat1[1][0];
    cout<< "Digit 4: ";
    cin >> mat1[1][1];
    cout<< "\n2x2 Matrix:\n\n";
    cout<< mat1[0][0] << "\t";
    cout<< mat1[0][1] << "\n\n";
    cout<< mat1[1][0] << "\t";
    cout<< mat1[1][1] << "\n\n";
}
> int mat1[1][1];
This is a 1x1 matrix.

Try it with
int mat1[2][2];

Dude, thank you so much,,,, I thought 0 is always gonna be equivalent to 1 if its not a value of math.... Thank you so much
wait, how about in here

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
38
int mat2[3][3]
void Matrix3x3()
{
    cout<< "\n\nPlease put your numbers to be put in the matrix.\n";
    cout<< "Digit 1: ";
    cin >> mat1[0][0];
    cout<< "Digit 2: ";
    cin >> mat1[0][1];
    cout<< "Digit 3: ";
    cin >> mat1[0][2];
    cout<< "Digit 4: ";
    
    cin >> mat1[1][0];
    cout<< "Digit 5: ";
    cin >> mat1[1][1];
    cout<< "Digit 6: ";
    cin >> mat1[1][2];
    
    cout<< "Digit 7: ";
    cin >> mat1[2][0];
    cout<< "Digit 8: ";
    cin >> mat1[2][1];
    cout<< "Digit 9: ";
    cin >> mat1[2][2];
    cout<< "\n3x3 Matrix:\n\n";

    cout<< mat1[0][0] << "\t";
    cout<< mat1[0][1] << "\t";
    cout<< mat1[0][2] << "\n";

    cout<< mat1[1][0] << "\t";
    cout<< mat1[1][1] << "\t";
    cout<< mat1[1][2] << "\n";

    cout<< mat1[2][0] << "\t";
    cout<< mat1[2][1] << "\t";
    cout<< mat1[2][2] << "\n\n";


the output will repeat the 4th and 7th input
Well L1 refers to mat2 and the other lines refer to mat1.....
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
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

int mat1[3][3];

void Matrix3x3()
{
	cout << "\n\nPlease put your numbers to be put in the matrix.\n";
	cout << "Digit 1: ";
	cin >> mat1[0][0];
	cout << "Digit 2: ";
	cin >> mat1[0][1];
	cout << "Digit 3: ";
	cin >> mat1[0][2];
	cout << "Digit 4: ";

	cin >> mat1[1][0];
	cout << "Digit 5: ";
	cin >> mat1[1][1];
	cout << "Digit 6: ";
	cin >> mat1[1][2];

	cout << "Digit 7: ";
	cin >> mat1[2][0];
	cout << "Digit 8: ";
	cin >> mat1[2][1];
	cout << "Digit 9: ";
	cin >> mat1[2][2];
	cout << "\n3x3 Matrix:\n\n";

	cout << mat1[0][0] << "\t";
	cout << mat1[0][1] << "\t";
	cout << mat1[0][2] << "\n";

	cout << mat1[1][0] << "\t";
	cout << mat1[1][1] << "\t";
	cout << mat1[1][2] << "\n";

	cout << mat1[2][0] << "\t";
	cout << mat1[2][1] << "\t";
	cout << mat1[2][2] << "\n\n";

}

int main()
{
	Matrix3x3();
}



Please put your numbers to be put in the matrix.
Digit 1: 1
Digit 2: 2
Digit 3: 3
Digit 4: 4
Digit 5: 5
Digit 6: 6
Digit 7: 7
Digit 8: 8
Digit 9: 9

3x3 Matrix:

1       2       3
4       5       6
7       8       9



Note that it's not good practice to define variables as global. It's better to pass to function(s) as parameters.
I actually tried doing functions for my arrays so that they can automatically just go one by one (if that's what you mean). Unfortunately, my tiny brain can't handle how it done hahaha.
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
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

void Matrix3x3(int mat1[3][3])
{
	cout << "\n\nPlease put your numbers to be put in the matrix.\n";
	cout << "Digit 1: ";
	cin >> mat1[0][0];
	cout << "Digit 2: ";
	cin >> mat1[0][1];
	cout << "Digit 3: ";
	cin >> mat1[0][2];
	cout << "Digit 4: ";

	cin >> mat1[1][0];
	cout << "Digit 5: ";
	cin >> mat1[1][1];
	cout << "Digit 6: ";
	cin >> mat1[1][2];

	cout << "Digit 7: ";
	cin >> mat1[2][0];
	cout << "Digit 8: ";
	cin >> mat1[2][1];
	cout << "Digit 9: ";
	cin >> mat1[2][2];
	cout << "\n3x3 Matrix:\n\n";

	cout << mat1[0][0] << "\t";
	cout << mat1[0][1] << "\t";
	cout << mat1[0][2] << "\n";

	cout << mat1[1][0] << "\t";
	cout << mat1[1][1] << "\t";
	cout << mat1[1][2] << "\n";

	cout << mat1[2][0] << "\t";
	cout << mat1[2][1] << "\t";
	cout << mat1[2][2] << "\n\n";
}

int main()
{
	int mat[3][3];

	Matrix3x3(mat);
}

Topic archived. No new replies allowed.