sum and product of two matrix in C (with functions)

I have to write a program in C which finds the sum and the product of two matrix.

I wrote the functions but I get stucked at calling them in main .I don't know which variable is for rows and columns of result matrix.

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
  #include <stdio.h>
void enterMatrix(int a[][10], int rows, int columns)
{
	int i,j;
	for(i=0;i<rows;i++)
	{
		for(j=0;j<columns;j++)
		{
			printf("a(%d,%d)=",i,j);
			scanf("%d",&a[i][j]);
		}
	}
}
void displayMatrix(int a[][10], int rows, int columns)
{
	int i,j;
	for(i=0;i<rows;i++)
	{
		for(j=0;j<columns;j++)
		{
			printf("%d", a[i][j]);
			printf(" ");
		}
		printf("\n");
	}
}
void matrixSum(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
	int i,j;
	for(i=0;i<rows;i++)
	{
		for(j=0;j<columns;j++)
		{
			c[i][j]=a[i][j]+b[i][j];
		}
	}
}
void matrixProduct(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
	int i,j,k;
	for(i=0;i<rows;i++)
	{
		for(j=0;j<columns;j++)
		{
			c[i][j]=0;
			for(k=0;k<columns;k++)
			{
				c[i][j]+=a[i][k]*b[k][j];
			}

		}
	}
}
int main(void)
{
	int a[10][10], b[10][10], sum[10][10], product[10][10];
	int rowsA,columnsA,rowsB,columnsB;
	printf("Number of rows for matrix A: \n");
	scanf("%d",&rowsA);
	printf("Number of columns for matrix A: \n");
	scanf("%d",&columnsA);
	printf("Number of rows for matrix B: \n");
	scanf("%d",&rowsB);
	printf("Number of columns for matrix B: \n");
	scanf("%d",&columnsB);
	printf("Enter first matrix: \n");
	enterMatrix(a,rowsA,columnsA);
	printf("Show first matrix: \n");
	displayMatrix(a,rowsA,columnsA);
	printf("Enter second matrix: \n");
	enterMatrix(b,rowsB,columnsB);
	printf("Show second matrix: \n");
	displayMatrix(b,rowsB,columnsB);

	if((rowsA==rowsB) && (columnsA==columnsB))
	{
		matrixSum(a,b,sum, ???, ???);
		printf("The sum matrix is: \n");
		displayMatrix(sum, ???, ???);
	}
	else
	{
		printf("Wrong information.");
	}
	if((rowsA==columnsB) && (rowsB==columnsA))
    {
        matrixProduct(a,b,product,???,???);
        printf("The product matrix is \n");
        displayMatrix(product,???,???);
    }
	return 0;
}
c++ is row major, so 2-d is stored [rows][cols] but remember that matrix multiply needs a new matrix that uses the rows of one input and the cols of the other input as the new size … review the algorithm/math before you try it.

perhaps if you labeled rows and cols or r and c instead of j and k or the like it would be easier to follow and read...
Last edited on
You haven't got enough arguments to your matrix product routine. You would need
rowsA
columnsA (which must equal rowsB)
columnsB

Alternatively, you could pass rows and columns of both and test them for compatability within the matrixProduct routine.

Alternatively you could use std::vector instead of arrays and not have to worry about passing sizes (or the magic number 10).

Your condition on line 85 is wrong: you only need rowsB==columnsA. The final product will have dimensions rowsA x columnsB.

Your code is more C than C++.
Last edited on
Topic archived. No new replies allowed.