Function ain't printing the correct matrix after adding - c

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

Related

First row doesnt work in matrix product using C

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

How To Make It Work For Any No Of Rows And Cols

Below is a code to display the n*m matrix where n & m are the number of rows and columns user want respectively
Ideally, It should work for any number of n and m, but my code only works for cases where n=m i.e n*n matrix
The Code:
int main(int argc, char const *argv[])
{
//define
int i,j; // iterators - rows, cols
int n,m; // number store
//input + array declaration
printf("Enter Number Of Rows You Want: ");
scanf("%d", &n);
printf("\nEnter Number Of Cols You Want: ");
scanf("%d", &m);
// user input no of elelments in m and n
int a[m][n];
for (i=0; i<n; i++){
for(j=0; j<n; j++){
printf("\nEnter Values For [%d][%d]: ", i,j);
scanf("%d", &a[i][j]);
}
}
// printing
for (i=0; i<n; i++){
for(j=0; j<n; j++){
printf("%d ", a[i][j]);
}
if(j==n){
printf("\n");
}
}
return 0;
}
Here I am taking the input using nested for loop in &arr[n][m] and printing it using the same concept but in a new loop
Which Returns:
Executed
So I want to know how I can adapt my code to work for any n*m matrix where n is no of rows and m is no of columns?
I am using windows 10 and the editor is VS Code
Any code blocks will be appreciated

Error in calculating resultant matrix in C programming language

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.

Array output difficulties

I have been practising with arrays for a bit and I have encountered a problem I can't seem to find the answer for. I am trying to display the numbers the user enters, however they are not turning out as I expected. It should be in a form of a column.
#include <stdio.h>
int main (void)
{
double A[5], B[5];
int i;
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
printf("A = (%f) B = (%f) \n", A[i], B[i]);
return 0;
}
The printf statement seems to be correct however numbers are not showing in the output.
You should ask yourself, what is the value of i, when printing the final output.
You should also ask yourself, what is in array A and B at index i.
Given these are understood, we can display the content of an array in the same fashion as it is filled.
#include <stdio.h>
int main (void)
{
double A[5], B[5];
int i;
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
for (i=0; i<=4; i++)
{
printf("A = (%f) B = (%f) \n", A[i], B[i]);
}
return 0;
}
As said by #Tsakiroglou Fotis, you forgot to add bracket after the main function and also you are not looping the final print statement to print all the elements. Try using editors that does take care of such mistakes. here is your corrected code
#include <stdio.h>
int main (void){
double A[5], B[5];
int i;
for(i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for(i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
for(i=0; i<5; i++){
printf("A = (%f) B = (%f) \n", A[i], B[i]);
}
return 0;
}

Create a basic matrix in C (input by user !)

I'm trying to ask the user to enter the number of columns and rows they want in a matrix, and then enter the values in the matrix... I'm going to let them insert numbers one row at a time.
How can I create such function ?
#include<stdio.h>
main(){
int mat[10][10],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++){
scanf("%d",&mat[i][j]);
}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%d",mat[i][j]);
}
This works for entering the numbers, but it displays them all in one line... The issue here is that I don't know how many columns or rows the user wants, so I cant print out %d %d %d in a matrix form...
Any thoughts?
Thanks :)
How about the following?
First ask the user for the number of rows and columns, store that in say, nrows and ncols (i.e. scanf("%d", &nrows);) and then allocate memory for a 2D array of size nrows x ncols. Thus you can have a matrix of a size specified by the user, and not fixed at some dimension you've hardcoded!
Then store the elements with for(i = 0;i < nrows; ++i) ... and display the elements in the same way except you throw in newlines after every row, i.e.
for(i = 0; i < nrows; ++i)
{
for(j = 0; j < ncols ; ++j)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
You need to dynamically allocate your matrix. For instance:
int* mat;
int dimx,dimy;
scanf("%d", &dimx);
scanf("%d", &dimy);
mat = malloc(dimx * dimy * sizeof(int));
This creates a linear array which can hold the matrix. At this point you can decide whether you want to access it column or row first. I would suggest making a quick macro which calculates the correct offset in the matrix.
need a
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d",mat[i][j]);
}
printf("\n");
}
#include<stdio.h>
int main(void)
{
int mat[10][10],i,j;
printf("Enter your matrix\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
scanf("%d",&mat[i][j]);
}
printf("\nHere is your matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",mat[i][j]);
}
printf("\n");
}
}
This is my answer
#include<stdio.h>
int main()
{int mat[100][100];
int row,column,i,j;
printf("enter how many row and colmn you want:\n \n");
scanf("%d",&row);
scanf("%d",&column);
printf("enter the matrix:");
for(i=0;i<row;i++){
for(j=0;j<column;j++){
scanf("%d",&mat[i][j]);
}
printf("\n");
}
for(i=0;i<row;i++){
for(j=0;j<column;j++){
printf("%d \t",mat[i][j]);}
printf("\n");}
}
I just choose an approximate value for the row and column. My selected row or column will not cross the value.and then I scan the matrix element then make it in matrix size.
int rows, cols , i, j;
printf("Enter number of rows and cols for the matrix: \n");
scanf("%d %d",&rows, &cols);
int mat[rows][cols];
printf("enter the matrix:");
for(i = 0; i < rows ; i++)
for(j = 0; j < cols; j++)
scanf("%d", &mat[i][j]);
printf("\nThe Matrix is:\n");
for(i = 0; i < rows ; i++)
{
for(j = 0; j < cols; j++)
{
printf("%d",mat[i][j]);
printf("\t");
}
printf("\n");
}
}
//R stands for ROW and C stands for COLUMN:
//i stands for ROW and j stands for COLUMN:
#include<stdio.h>
int main(){
int M[100][100];
int R,C,i,j;
printf("Please enter how many rows you want:\n");
scanf("%d",& R);
printf("Please enter how column you want:\n");
scanf("%d",& C);
printf("Please enter your matrix:\n");
for(i = 0; i < R; i++){
for(j = 0; j < C; j++){
scanf("%d", &M[i][j]);
}
printf("\n");
}
for(i = 0; i < R; i++){
for(j = 0; j < C; j++){
printf("%d\t", M[i][j]);
}
printf("\n");
}
getch();
return 0;
}

Resources