2D Matrix not being copied correctly in addition of 2 matrices

Please find the below code for addition of two matrices


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
#include<iostream>
using namespace std; 

void input_mat(int x[][3],int m,int n)
{
	int i,j;
	for(i=0;i<m;i++)
	{	for(j=0;j<n;j++)
		cin>>x[i][j];	} }

void show_mat(int x[][3],int m,int n)
{
	int i,j;
	for (i=0;i<m;i++)
	{	for(j=0;j<n;j++)
		{
		cout<<x[i][j]<<" ";
		}	cout<<endl;	} }
		
void add_mat(int x[][3],int y[][3],int z[][3],int r1,int c1,int r2,int c2)
{int i,j;
			show_mat(x,r1,c1);
		show_mat(y,r1,c1);
	if(r1==r2&&c1==c2)
	{
		for(i=0;i<r1;i++)
		{
			for(j=0;j<c1;j++)
			z[i][j]=x[i][j]+y[i][j];
		}
		cout<<"Sum of matrices is given by the following matrix."<<endl;
		show_mat(z,r1,c1);
		cout<<endl;
	}
	else
	cout<<"Matrices can't be added.";
	cout<<endl;
}


int main()
{
	int p,q,r,s;
	cout<<"Enter number of rows and columns of matrix and all the elements."<<endl;
	cin>>p>>q;
	int a[][3]={0}; input_mat(a,p,q); show_mat(a,p,q);

	cout<<"Enter number of rows and columns of matrix and all elements."<<endl;
	cin>>r>>s;
	int b[][3]={0};	input_mat(b,r,s); show_mat(b,r,s);

	int c[][3]={0};
	add_mat(a,b,c,p,q,r,s);
	return 0;
}



My doubt is : On calling show_mat(a,p,q) in int main, I get the correct value of matrices, that I had input in the main function. However, the show_mat(x,r1,c1) being called in the add_mat() does not display the matrix "a"(or "x"), which was copied into it. On the other hand, it is showing some different matrix, different from "a". Ultimately, this leads to incorrect output. Why is it happening? What is the reason?
Last edited on
What are you entering as input?

Letting the user enter the number of rows and columns will only work properly if rows == 1, and columns <= 3. Otherwise you're going out of bounds of the array.

int a[][3]={0};
Do you know what this is doing? It's creating 1 row, 3 columns (3 elements total), because you're not specifying the # of rows.

Please edit your post and add code formatting by adding [code] and [/code] around your code.
Last edited on
Where are you allocating all the memory for those arrays?

What is the purpose of that magic number 3?

Having a "blank" array size is probably a mistake.

Example of using a dynamic array (vector) to allow the user to enter rows/columns

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
// Example program
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using Matrix = std::vector<int>;

void show_mat(const Matrix& mat, int rows, int columns)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            size_t index = i * columns + j;
            cout << mat[index] << ' ';
        }
        cout << '\n';
    }
}

int main()
{
    int num_rows = 0;
    int num_columns = 0;
    cout<<"Enter number of rows and columns of matrix and all the elements." << endl;
    cin >> num_rows >> num_columns;
    
    Matrix mat(num_rows * num_columns);
    
    int element;
    for (int i = 0; i < num_rows * num_columns; i++)
    {
        if (cin >> element)
            mat[i] = element;
        else
            break; // break if no more input (defaults to 0 as the value)
    }
    
    show_mat(mat, num_rows, num_columns);
}


A 2D vector could also be used but I dislike the discontiguous nature of those (horrible for the cache).
Last edited on
However, before passing the matrix to function, I am calling the input function,where I am giving the values of the elements. The values of the elements should decide its size automatically. Displaying the matrix in the int main() shows correct matrix. However, when I pass it to function, inside the function, the matrix shows incorrect elements. That implies, the problem occurred while copying the elements during function call. Why should there be a problem in copying from function call to function definition? Shouldn't it copy the same matrix as it is without changing? Why is the change occurring while passing it to function?
Last edited on
However, before passing the matrix to function, I am calling the input function,where I am giving the values of the elements.

No, you're passing garbage to the input function since your "matrix" has not had it's memory properly allocated. Therefore you have nowhere to place those "values" inside the function.

The values of the elements should decide its size automatically.

No, when dealing with arrays you need to manually allocate all the memory for that "matrix". Another alternative would be to use a std::vector<std::vector<int>> of the proper sizes.

Displaying the matrix in the int main() shows correct matrix.

Just because something appears to be working as desired doesn't mean it is working correctly. By the way you just got "unlucky" that something appears to be working as expected, even though it's wrong.

Why should there be a problem in copying from function call to function definition?

The problem stems from this line: int a[][3]={0}; this is not doing what you seem to expect. First off unless you enter 3 or less for 'q' (by the way this is a horrible variable name) you will be accessing the array out of bounds, to say nothing of the problem with having no size for the "outer" array.

Shouldn't it copy the same matrix as it is without changing?

Actually you don't "copy" an array you are passing a pointer into the function, not the data itself.



Thanks for the info and help
Topic archived. No new replies allowed.