I wrote a programm for multiplying matrices together. For testing I entered:
A [1 0, 0 1] and B=[2 2, 2 2]
The result should be: [2 2, 2 2]. However, I got [2122123 2, 2 2]. Why does this happen? Maybe the value is an address in the storage?
#include <stdio.h>
int main(){
int A[10][10], B[10][10], C[10][10];
int n,m,r,s,k,sum,j,i=0;
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &n,&m);
getchar();
printf("Enter values:\n");
for(i=0; i<n;i++){
for(j=0; j<m; j++){
scanf("%d", &A[i][j]);
getchar();
}
}
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &r,&s);
getchar();
if ( m != r )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else{
printf("Enter values\n");
for(i=0; i<r;i++){
for(j=0; j<s; j++){
scanf("%d", &B[i][j]);
getchar();
}
}
for(i=0; i<n; i++){
for(j=0; j<s;j++){
for(k=0;k<r; k++){
sum=sum+ A[i][k]*B[k][j];
}
C[i][j]=sum;
sum=0;
}
}
printf("Product of entered matrices:-\n");
for(i=0; i<n;i++){
for(j=0; j<s; j++){
printf("%d\t",C[i][j] );
}
printf("\n");
}
}
return 0;
}
All in all I consider matrices of dimension nxm and rxs.
The line int n,m,r,s,k,sum,j,i=0; does not initialize sum to 0. It only sets i.
Set sum = 0; immediately before the loop over k, not after.
Related
I'm trying to do a code in C with simple functions that can add two given matrixes (bidimensional arrays),, in order to make a matrix calculator.
The thing is, for some reason the code keeps printing the second matrix instead of the final one.
We have three matrixes (1,2,3) and user gives the value of 1 and 2, to store it and add them to print matrix3. When executing, it will only give the values of the second matrix. I hope someone could tell me why or give any suggestions pls.
#include <stdio.h>
int r1,c1,r2,c2,i,j, matrix1[100][100], matrix2[100][100], finalmatrix[100][100]; //r stands for rows, c for columns and i,j are iterarions
void fillMatrix(){ //Function that creates two bidimensional arrays and asks for its parameters
printf("Enter the number of rows of first matrix: \n");
scanf("%d", &r1);
printf("Enter the number of columns of first matrix: \n");
scanf("%d", &c1);
printf(" First matrix sizes are:[%d][%d] \n", r1,c1); //This is only the print of rows and columns values of matrix1
int matrix1[r1][c1];
printf("\nEnter elements of 1st matrix:\n"); //asks for the values of the matrix
for (i = 0; i < r1; ++i)
for (j = 0; j < c1; ++j) {
printf("Enter element a%d%d: \n", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
printf("Enter the number of rows of second matrix: \n");
scanf("%d", &r2);
printf("Enter the number of columns of second matrix: \n");
scanf("%d", &c2);
printf("Second matrix sizes are:[%d][%d] \n", r2,c2); //This is only the print of rows and columns values of matrix1
printf("\n Enter elements of 2nd matrix: \n");
for (i=0; i< r2; i++)
for (j=0; j<c2; j++){
printf("Enter the element a%d%d: \n", i+1,j+1);
scanf("%d", &matrix2[i][j]);
}
}
void addMatrix()
{
//Initialize matrix3=0
for(i=0; i<r1; i++)
{
for (j=0; j<c2; ++j)
{
finalmatrix[i][j]=0;
}
}
//Add matrix1 to matrix2 and store in matrix3
for (i=0; i<r1; i++)
{
for (j=0; j<c2; j++)
{
finalmatrix[i][j]=matrix1[i][j] + matrix2[i][j];
}
}
//print matrix3
printf("The resulting matrix is: \n");
for (i=0; i<r1; i++)
{
for (j=0;j<c2; j++)
{
printf("%d ", finalmatrix[i][j]);
if (j==c2-1){
printf("\n \n");}
}
}
}
int main(){
fillMatrix();
addMatrix();
}
Remove the declaration of matrix1 in the fillMatrix function that hides the global matrix1.
int matrix1[r1][c1]; // remove this line
I'm a beginner into C and I've been stuck on this particular question for the past few hours now. I need to multiply 2 matrices but the code despite being correct as far as I know, just doesn't work with the first row of the resultant matrix
Please do not mark this as duplicate without going through this entire question atleast once; I genuinely need help.
e.g.
but my code outputs this instead
code:
// Multiply two matrices
# include <stdio.h>
int main()
{
// declare itertives and matrices
int i, j, k, rows0, cols0, rows1, cols1, matrix0[10][10], matrix1[10][10], matrix2[10][10];
printf("Number of Rows and columns shouldn't exceed 10\n");
printf("Enter number of rows and columns for first Matrix\n");
printf("Enter number of rows: ");
scanf("%d", &rows0);
printf("Enter number of columns: ");
scanf("%d", &cols0);
printf("Enter number of rows and columns for second Matrix\n");
printf("Enter number of rows: ");
scanf("%d", &rows1);
printf("Enter number of columns: ");
scanf("%d", &cols1);
if (cols0!=rows1)
{
printf("Number of columns in first matrix should be equal to number of rows in second matrix\n");
return 0;
}
printf("Input values into first Matrix\n");
for(i = 0; i < rows0; i++)
{
printf("Input values in row %d with each value separated with space\n", i);
for(j=0; j< cols0; j++)
{
scanf("%d", &matrix0[i][j]);
}
}
printf("Input values into second Matrix\n");
for(i = 0; i < rows1; i++)
{
printf("Input values in row %d with each value separated with space\n", i);
for(j=0; j< cols1; j++)
{
scanf("%d", &matrix1[i][j]);
}
}
// Matrix multiplication
for (i=0; i<rows0; i++)
{
matrix2[i][j]=0;
for (j=0; j< cols1; j++)
{
for(k=0;k<cols1;k++)
{
matrix2[i][j]+=(matrix0[i][k]*matrix1[k][j]);
}
}
}
printf("Resultant matrix after matrix multiplication is\n");
for(i=0; i < rows0; i++)
{
for(j=0; j<cols1; j++)
{
printf("%d ", matrix2[i][j]);
}
printf("\n");
}
}
output
matrix2[i][j]=0; out of place. You owe me 60 seconds of debugging :-)
See https://ideone.com/NS9vo6
I managed to do enter M, N, and accept its condition (0<N and M<=10), then create and apply desirable number into 2 matrices, which is called Matrix A and Matrix B, then the Matrix C is the plus of 2 Matrix A B.
The last one is the transpose, I have looked upon some sites, but since the ways they do are not like mine so I don't like to just "straightly" copy and paste it.
Here is my code:
#include <stdio.h>
int main()
{
int M, N, i, j;
printf ("Enter number M: ");
scanf("%d", &M);
printf ("Enter number N: ");
scanf("%d", &N);
if (0 < N && M <= 10) {
printf ("Accepted number M: %d\n", M);
printf("Accepted number N: %d\n", N);
}
else printf("Please enter valid number, which is 0 < N and M <= 10!");
int matrixA[M][N], matrixB[M][N], matrixC[M][N];
printf ("Enter matrix A: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
scanf("%d", &matrixA[i][j]);
}
}
printf ("Enter matrix B: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
scanf("%d", &matrixB[i][j]);
}
}
printf("Matrix A: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", matrixA[i][j]);
}
printf("\n");
}
printf("Matrix B: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", matrixB[i][j]);
}
printf("\n");
}
for (i=0; i<M; i++){
for (j=0; j<N; j++){
matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
printf("Sum of matrix A and matrix B => Matrix C is: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", matrixC[i][j]);
}
printf("\n");
}
int reverseMatrixC[N][M];
for (i=0; i<M; i++){
for (j=0; j<N; j++){
reverseMatrixC[j][i] = matrixC[i][j];
}
}
printf("Reverse columns and rows in matrix C: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", reverseMatrixC[i][j]);
}
printf("\n");
}
}
The last one "Reverse columns and rows" are wrong and I don't know why, from my way of thinking, I'm switching M and N, then apply the previous Matrix C into a new variable which is reverseMatrixC, and then reverse i and j.
For example, I choose M = 2, N = 3.
and then I choose matrixA and matrixB these value:
1 2 3
4 5 6
The matrixC will display:
2 4 6
8 10 12
But when I transpose it, it'll display:
2 8 4
4 10 6
What I want is something like this:
2 8
4 10
6 12
You're very close. In fact, you're transposing matrixC correctly, the only problem is with how you're printing it out:
printf("Reverse columns and rows in matrix C: \n");
// reverseMatrixC has its rows and columns dimensions swapped, so N specifies its number of rows
for (i=0; i<N; i++){
// and M specifies its number of columns
for (j=0; j<M; j++){
printf("%d ", reverseMatrixC[i][j]);
}
printf("\n");
}
Also highly recommend to fix your indentation and use an IDE that formats for you, it makes reading code much easier.
Demo
In the output, I get every addition right except the first one. In the output, it shows this :
enter the number of rows and columns of matrices to be added : 2 2
For matrix [a], Enter The Element at (0,0): 2
For matrix [a], Enter The Element at (0,1): 2
For matrix [a], Enter The Element at (1,0): 2
For matrix [a], Enter The Element at (1,1): 2
the matrix [a] is :
2 2
2 2
For matrix [b], Enter The Element at (0,0): 22
For matrix [b], Enter The Element at (0,1): 2
For matrix [b], Enter The Element at (1,0):
2
For matrix [b], Enter The Element at (1,1): 2
the matrix [b] is :
22 2
2 2
Resultant matrix [c] :
0 4
4 4
I don't know what caused the "0" in the position (0,0) of resultant matrix. I tried changing the i++ to ++i just to make sure its not the prefix and postfix problem but it doesn't seem to be it. I cannot figure out why I get 0 only in (0, 0). Please do help as I'm new to C. This is my code :
int add(int a[][20], int b[][20], int c[][20], int row, int col);
int row, col;
int a[100][20];
int b[100][20];
int c[100][20];
int main()
{
printf("enter the number of rows and columns of matrices to be added : ");
scanf("%d %d", &row, &col);
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("For matrix [a], Enter The Element at (%d,%d): ",i,j);
scanf("%d", &a[i][j]);
}
}
printf("\nthe matrix [a] is : \n");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("%d", a[i][j]);
printf(" ");
}
printf("\n");
}
printf("\n");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("For matrix [b], Enter The Element at (%d,%d): ",i,j);
scanf("%d", &b[i][j]);
}
}
printf("\nthe matrix [b] is : \n");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("%d", b[i][j]);
printf(" ");
}
printf("\n");
}
printf("\n");
int i, j;
c[i][j] = add(a, b, c, row, col);
printf("Resultant matrix [c] : \n");
for (i=0; i<row; i++){
for (j=0; j<col; j++){
printf("%d", c[i][j]);
printf (" ");
}
printf("\n");
}
return 0;
}
int add(int a[][20], int b[][20], int c[][20], int row, int col){
int i, j;
for (i=0; i<row; i++){
for (j=0; j<col; j++){
c[i][j] = a[i][j] + b[i][j];}}
return c[i][j];
}
As the mentioned comments you don't need to retrun anything for matrix sumation because you have already changed that matrix by reference. Remove int retrun value of add and try something like that:
#include "stdio.h"
void add(int a[][20], int b[][20], int c[][20], int row, int col);
int row, col;
int a[100][20];
int b[100][20];
int c[100][20];
int main()
{
printf("enter the number of rows and columns of matrices to be added : ");
scanf("%d %d", &row, &col);
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("For matrix [a], Enter The Element at (%d,%d): ",i,j);
scanf("%d", &a[i][j]);
}
}
printf("\nthe matrix [a] is : \n");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("%d", a[i][j]);
printf(" ");
}
printf("\n");
}
printf("\n");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("For matrix [b], Enter The Element at (%d,%d): ",i,j);
scanf("%d", &b[i][j]);
}
}
printf("\nthe matrix [b] is : \n");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("%d", b[i][j]);
printf(" ");
}
printf("\n");
}
printf("\n");
int i, j;
add(a, b, c, row, col);
printf("Resultant matrix [c] : \n");
for (i=0; i<row; i++){
for (j=0; j<col; j++){
printf("%d", c[i][j]);
printf (" ");
}
printf("\n");
}
return 0;
}
void add(int a[][20], int b[][20], int c[][20], int row, int col){
int i, j;
for (i=0; i<row; i++){
for (j=0; j<col; j++){
c[i][j] = a[i][j] + b[i][j];}}
}
I am trying to write a program where I have two matrices and I multiply the two matrices and store it in a resultant matrix named "carr." For some weird reason, the matrix multiplication is not getting executed properly. Tried to find the issue for quite a while but couldn't find the error. Can anyone help? TIA for your time!
Here is the ss of the issue: https://snipboard.io/s9ifP4.jpg
#include <stdio.h>
int main()
{
int row1, column1, row2, column2,i,j,k, sum=0;
//START OF THE 1ST ARRAY//
printf("How many rows do you want for the first matrix? Ans: ");
scanf("%d", &row1);
printf("How many columns do you want for the first matrix? Ans: ");
scanf("%d", &column1);
int arr[row1][column1];
printf("Enter the elements of the first array:\n");
for(i = 0; i <row1; i++){
for(j=0; j < column1; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the first array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column1; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
//END OF THE FIRST ARRAY//
printf("----------------------------------------\n");
//START OF THE 2ND ARRAY//
printf("\n**How many rows do you want for the second matrix?\n\nAlert: For matrix multiplication, the COLUMN of the 1st matrix MUST equal to the ROW of the 2nd matrix.\nAns: ");
scanf("%d", &row2);
printf("How many columns do you want for the second matrix? Ans: ");
scanf("%d", &column2);
int barr[row2][column2];
printf("Enter the elements of the second array:\n");
for(i = 0; i <row2; i++){
for(j=0; j < column2; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the second array are:\n");
for(i = 0; i <row2; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
//END OF THE 2ND ARRAY//
//Everything above this part is okay. The problem starts from the Matrix multiplication part//
//MATRIX MULTIPLICATION//
//The resultant matrix where the values of the multiplied matrix is being held has row = ROW1 and column = COLUMN2.//
int carr[row1][column2];
if(column1 == row2)
{
for(i = 0; i < row1; i++){
for(j=0; j < column2; j++){
for(k=0; k < row2; k++){
sum = sum + arr[i][k] * barr[k][j];
}
carr[i][j] = sum;
sum=0;
}
}
}
else
{
printf("Matrix multiplication is not possible");
}
printf("\n----------------------------------------\n");
printf("The elements of the resultant array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", carr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
return 0;
}
Changing arr to barr fixes the issue. Thanks to #M Oehm for pointing out the error.