2D array Help

So i have two 2d arrays and i need to add subtract and multiply them then display them. i have everything pretty much good but im not getting the correct answers and i cant seem to figure out why. also im having some trouble finding a good tutorial on how to multiply 2d arrays.
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

//****************************************************************************************************

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

void loadData(int matrix1 [4][4], int matrix2[4][4], int rows, int cols);
void addMatrices(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols);
void subtractMatrix(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols);
//void multiplyMatrix(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols);
void outputData(int matrix1 [4][4], int matrix2[4][4], int rows, int cols);
void outputResult(int result[4][4], int resultType, int rows, int cols);

int main ()
{
    int rows,
        cols,
        resultType = 1,
        matrix1[4][4],
        matrix2[4][4],
        result[4][4];
        ifstream DataFile;
        ifstream Data2File;
        fstream OutputFile;
    
    loadData(matrix1, matrix2, rows, cols);
    outputData(matrix1, matrix2, rows, cols);
    addMatrices(matrix1, matrix2, result, rows, cols);
    outputResult(result, resultType, rows, cols);
    resultType = 2;
    subtractMatrix(matrix1, matrix2, result, rows, cols);
    outputResult(result, resultType, rows, cols);
    resultType = 3;
    //multiplyMatrix(matrix1, matrix2, result, rows, cols);
    //outputResult(result, resultType, rows, cols);
    
    return 0;
}

//****************************************************************************************************

void loadData(int matrix1 [4][4], int matrix2[4][4], int rows, int cols)
{
    cout << "Enter Data for Matrix 1" << endl;
    
    for (int rows = 0; rows < 4; rows++)
    {
        for (int col = 0; col < 4; col++)
        {
            cout << "row - " << rows + 1 << " colummn - " << col + 1 << " =  ";
            cin >> matrix1[rows][col];
        }
    }
    
    cout << endl << "Enter Data for Matrix 2" << endl;

    for (int rows = 0; rows < 4; rows++)
    {
        for (int col = 0; col < 4; col++)
        {
            cout << "row - " << rows + 1 << " colummn - " << col + 1 << " =  ";
            cin >> matrix2[rows][col];
        }
    }
    cout << endl;
}

//****************************************************************************************************

void addMatrices(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols)
{
    for( int rows = 0; rows < 4; rows++)
    {
        for (int col = 0; col < 4; col++)
        {
            result[rows][col] = matrix1[rows][col] + matrix2[rows][cols];
        }
    }
}

//****************************************************************************************************

void subtractMatrix(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols)
{
    for( int rows = 0; rows < 4; rows++)
    {
        for (int col = 0; col < 4; col++)
        {
            result[rows][col] = matrix1[rows][col] - matrix2[rows][cols];
        }
    }
}

//****************************************************************************************************

void outputData(int matrix1 [4][4], int matrix2[4][4], int rows, int cols)
{
    cout << "Matrix 1:" << endl;
    for (int rows = 0; rows < 4; rows++)
    {
        for (int col = 0; col < 4; col++)
        {
            cout << setw(6) << matrix1[rows][col];
            if (col == 3)
            {
                cout << endl;
            }
        }
    }
    cout << "Matrix 2;" << endl;
    for (int rows = 0; rows < 4; rows++)
    {
        for (int col = 0; col < 4; col++)
        {
            cout << setw(6) << matrix2[rows][col];
            if (col == 3)
            {
                cout << endl;
            }
        }
    }
}

//****************************************************************************************************

void outputResult(int result[4][4], int resultType, int rows, int cols)
{
    if (resultType == 1) {
        cout << "Addition Results" << endl;
    }
    if (resultType == 2) {
        cout << "Subtraction Results" << endl;
    }
    if (resultType == 3) {
        cout << "Multiplication Results" << endl;
    }
    
    for (int rows = 0; rows < 4; rows++)
    {
        for (int col = 0; col < 4; col++)
        {
            cout << setw(6) << result[rows][col];
            if (col == 3)
            {
                cout << endl;
            }
        }
    }
}

//**************************************************************************************************** 

and this is my output.
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
Enter Data for Matrix 1
row - 1 colummn - 1 =  7
row - 1 colummn - 2 =  8
row - 1 colummn - 3 =  5
row - 1 colummn - 4 =  3
row - 2 colummn - 1 =  4
row - 2 colummn - 2 =  1
row - 2 colummn - 3 =  10
row - 2 colummn - 4 =  6
row - 3 colummn - 1 =  -2
row - 3 colummn - 2 =  12
row - 3 colummn - 3 =  2
row - 3 colummn - 4 =  9
row - 4 colummn - 1 =  1
row - 4 colummn - 2 =  5
row - 4 colummn - 3 =  4
row - 4 colummn - 4 =  6

Enter Data for Matrix 2
row - 1 colummn - 1 =  -3
row - 1 colummn - 2 =  6
row - 1 colummn - 3 =  4
row - 1 colummn - 4 =  7
row - 2 colummn - 1 =  2
row - 2 colummn - 2 =  1
row - 2 colummn - 3 =  8
row - 2 colummn - 4 =  3
row - 3 colummn - 1 =  5
row - 3 colummn - 2 =  -1
row - 3 colummn - 3 =  4
row - 3 colummn - 4 =  10
row - 4 colummn - 1 =  7
row - 4 colummn - 2 =  6
row - 4 colummn - 3 =  12
row - 4 colummn - 4 =  8

Matrix 1:
     7     8     5     3
     4     1    10     6
    -2    12     2     9
     1     5     4     6


Matrix 2;
    -3     6     4     7
     2     1     8     3
     5    -1     4    10
     7     6    12     8
Addition Results
     4     5     2     0
     6     3    12     8
     3    17     7    14
     8    12    11    13
Subtraction Results
    10    11     8     6
     2    -1     8     4
    -7     7    -3     4
    -6    -2    -3    -1
Program ended with exit code: 0
Any help would be greatly appreciated!!! Thank you
Get rid of the ", int rows, int cols" parameters for your functions. The values you pass through them are not valid. After you do that, the compiler will generate some errors; this is because you accidentally used those meaningless values in your function bodies. Fix those uses to the correct variables and run the program again.
Those were the function parameters that were given to me.
Maybe the rows/columns should be used instead of the arbitrary 4's? Otherwise it would be pointless. You might as well just create a temp variable like row/column or i/j?
Last edited on
I went and replaced all the col with cols that I mistakingly threw in there and it works like a charm now.

heres the multiplication function that I worked out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void multiplyMatrix(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols)
{
    int k;
    for (rows = 0; rows < 4; rows++)
    {
        for (cols = 0; cols < 4; cols++)
        {
            result[rows][cols] = 0;
            for (k = 0; k < 4; k++)
            result[rows][cols] = result[rows][cols] + (matrix1[rows][k]) * (matrix2[k][cols]);
        }
        cout<<endl;
    }
}
Topic archived. No new replies allowed.