Matrix addition in C getting 0 in the first addition only - c

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];}}
}

Related

Basically: Create a 2x2 multidimensional array using C that adds, subtracts, multiple and divides

I already finished creating the sum, difference, and product. What I am having trouble with is the quotient. because when I run it, It will just give me 0.0. I tried making the arrA and arrB a float which makes the quotient work but the others (sum, difference, and product) do not work. Please help me on how I could get the quotient to work.
#include <stdio.h>
int main()
{
int arrA[2][2];
int arrB[2][2];
float arrSum[2][2];
float arrDiff[2][2];
float arrProd[2][2];
float arrQuo[2][2];
int i,j;
//1st Matrix
printf("Enter Elements of 1st Matrix\n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d", &arrA[i][j]); // to get value and save to array[][]
}
//printf("\n");
}
//Second Matrix
printf("Enter Elements of 2nd Matrix\n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
printf("Enter b[%d][%d]: ",i,j);
scanf("%d", &arrB[i][j]); // to get value and save to array[][]
}
//printf("\n");
}
// Sum of Matrix
printf("Sum of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrSum[i][j] = arrA[i][j] + arrB[i][j];
printf("%.1f ", arrSum[i][j]);
}
printf("\n");
}
//Difference of Matrix
printf("Difference of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrDiff[i][j] = arrA[i][j] - arrB[i][j];
printf("%.1f ", arrDiff[i][j]);
}
printf("\n");
}
//Product of Matrix
printf("Product of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrProd[i][j] = arrA[i][j] * arrB[i][j];
printf("%.1f ", arrProd[i][j]);
}
printf("\n");
}
//Quotient of Matrix
printf("Quotient of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrQuo[i][j] = arrA[i][j] / arrB[i][j];
printf("%.2f ", arrQuo[i][j]);
}
printf("\n");
}
return 0;
}
here is the result:
The output
here is what it should look like:
The supposed output

Function ain't printing the correct matrix after adding

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

How can I transpose my Matrix to reverse columns and rows?

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

Multiplying matrices in general

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.

Why my program of Matrix multiplication using C doesn't work?

I wrote a program to calculate the matrix multiplication.I takes user input to define the size of the array and the elements contained.(My knowledge on programming should be considered as a beginner).When I execute the program it prints a null array. When I tested the code line by line. I found out that the program works correctly till the calculation of the matrix.(Taking user input and calling the function).
I couldn't find the source of the problem. I have included part of the code which contains the multiplication function.
#include <stdio.h>
#define MAX 100
void matrix_mul(int, int, int, int, int [MAX][MAX], int [MAX][MAX]);
int main()
{
int mat_1[MAX][MAX] ,mat_2[MAX][MAX];
int row1, row2, column1, column2;
printf("Input number of rows for the first matrix: ");
scanf("%d", &row1);
printf("Input number of columns for the first matrix: ");
scanf("%d", &column1);
printf("Input number of rows for the second matrix: ");
scanf("%d", &row2);
printf("Input number of columns for the second matrix: ");
scanf("%d", &column2);
if(column1 != row2 || column2 != row1)
{
printf("Incompatible matrices. Try Again! ");
return 0;
}
printf("Enter elements for matrix 1 of order %d x %d\n", row1, column1);
for(int i=0; i<row1; i++)
{
for(int j=0; j<column1; j++)
scanf("%d", &mat_1[i][j]);
}
printf("\n\nEnter elements for matrix 1 of order %d x %d\n", row2, column2);
for(int i=0; i<row2; i++)
{
for(int j=0; j<column2; j++)
scanf("%d", &mat_2[i][j]);
}
matrix_mul(row1, row2, column1, column2, mat_1, mat_2);
}
// for testing r1 = 3 c1 =2 r2 =2 c2 =3
void matrix_mul(int row1, int row2, int column1, int column2, int ar1[MAX][MAX], int ar2[MAX][MAX])
{
int arr [MAX][MAX];
for(int i=0 ; i<row1; i++)
{
for(int j=0; j<column2; j++)
{
int sum = 0;
for(int k=0; k<column1; k++)
sum += ar1[row1][k] * ar2[column1][row1];
printf("%d", sum);
arr[row1][column2] = sum;
}
}
for(int i=0; i<row1; i++)
{
for(int j=0; j<column2; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
}
You're mixing up your loop values with your boundaries. Here's the corrected versions of the relevant lines:
sum += ar1[i][k] * ar2[k][j];
arr[i][j] = sum;
You're seeing 88888 instead of 8 because you left a debugging statement in:
printf("%d", sum);
Remove that and you'll only see the correct output.

Resources