Error in calculating resultant matrix in C programming language - c

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.

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

Values in array getting altered yet they shouldn't

It's a program that requires the user to enter the values of two 3 by 3 matrix then finds the sum of both matrices and prints out the same together with the added matrices but for some reason after entering the values of both matrices the a value in matrix gets altered then it affects the sum but at times it doesn't
#include<stdio.h>
void main(){
int matrix1[2][2];
int matrix2[2][2];
int sumMatrix[2][2];
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("Matrix[%d][%d]\n", i, j);
printf("Enter matrix one's values> ");
scanf("%d", &matrix1[i][j]);
}
printf("\n");
}
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("Matrix[%d][%d]\n", i, j);
printf("Enter matrix two's values> ");
scanf("%d", &matrix2[i][j]);
}
printf("\n");
}
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("%d ", matrix1[i][j]);
}
printf("\n");}
printf("\n");
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("%d ", matrix2[i][j]);
}
printf("\n");}
printf("\n");
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("%d ", sumMatrix[i][j]);
}
printf("\n");
}
}
You're declaring matrixes of size 2x2 and access them as if they were 3x3. What's happening more precisely is a buffer overflow, you write somewhere you shouldn't, and by doing so you overwrite other variables.
Lad, that is array overflow.
int matrix1[2][2];
But in the loop, you may get matrix1[2][1], matrix1[2][2].
Ok so clearly I'm the idiot. I thought array sizes are set based on the number of indices they'll have, so instead of int matrix1[2][2] it should be int matrix [3][3]

how to print an array as a matrix in C

so I´m trying to print this 2d array as a matrix and it´s not working. any hints? no matter what i change i cant get to print an all 0 3x3 matrix
int main()
{
int i, j, m, n, primeira;
int matrix[10][20];
printf("Enter number of rows : ");
scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
/* first input */
printf("1 ou 0");
scanf("%d", &primeira);
if (primeira = 0) {
matrix [0][0]=0;
matrix [0][1]=0;
matrix [1][0]=0;
matrix [1][1]=0;}
/* Display the matrix */
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
return 0;
}
You would need to create a nested loop to display the matrix. If you want to display a 3x3 matrix you can run something like this.
int matrix[3][3] = { 0 };
/* Display the matrix */
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf ("%d\t", matrix[i][j]);
}
printf ("\n");
}
regarding:
printf("%d\t", matrix[i][j]);
The variables: i and j are not initialized!
Suggest:
for( int i =0; i<10; i++ )
{
for( int j=0; j<20; j++ )
{
printf( "%d ", matrix[i][j] );
}
puts( "" );
}
as that will print all the elements of the matrix and move to a new line after printing each row of the matrix.

Ascending Order for 2-D Array, Not working..?

I am trying to output my 2D array into an Ascending order.
I have already output it into the order which the user put in. But can not get it to print to Ascending Order.
Could anybody possibly help with this or know a solution? My code is below for any question regarding code.
int main() {
/* 2D array declaration and size of each Array in the Programme*/
int Array[2][3];
printf ("***** Bubble Sort Assessment 2 ***** \n");
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("Enter numeric values for each Array [%d][%d]: \n", i, j);
scanf("%d", &Array[i][j]);
}
}
/*Displaying array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , Array[i][j]);
if(j==2)
{
printf("\n");
}
}
}
printf("\n\nAscending : ");
for (int i = 0; i < 2; i++)
{
printf(" %d ", a[i]);
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[j] < a[i])
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
return 0;
}
First Use bubble sort algorithm
for(int i=0; i<size; ++i)
{
for(int j=i+1; j<size; ++j)
{
if(a[i]>a[j]))
{
int temp=a[i];
a[i]=array[j];
a[j]=temp;
}
}
}
Then print your array in an ascending order.
Try to apply your own way, you can use some features of arrays like arrays contiguity.
int *ptr=a;
same with
*(ptr+i), *(ptr+j)
To use a simple one dimensional bubble sort algorithm to sort 2D array you have to add another loop taking care of the rows.
In your case:
for(k =0; k< 2; k++) {
for (i = 0; i < 3; i++) {
for (j = i+1; j < 3; ++j) {
if (a[k][i] > a[k][j]) {
int swap = a[k][i];
a[k][i] = a[k][j];
a[k][j] = swap;
}
}
}
}
The program:
#include <stdio.h>
int main(void) {
/* 2D array declaration and size of each Array in the Programme*/
int a[2][3];
printf ("***** Bubble Sort Assessment 2 ***** \n");
/*Counter variables for the loop*/
int i, j, k;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("Enter numeric values for each Array [%d][%d]: \n", i, j);
scanf("%d", &a[i][j]);
}
}
/*Displaying array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , a[i][j]);
if(j==2)
{
printf("\n");
}
}
}
// SORT:
for( k = 0; k< 2; k++) {
for ( i = 0; i < 3; i++) {
for ( j = i+1; j < 3; ++j) {
if (a[k][i] > a[k][j]) {
int swap = a[k][i];
a[k][i] = a[k][j];
a[k][j] = swap;
}
}
}
}
printf("\n\nAscending : ");
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , a[i][j]);
if(j==2)
{
printf("\n");
}
}
}
return 0;
}
Output:
***** Bubble Sort Assessment 2 *****
Enter numeric values for each Array [0][0]:
1
Enter numeric values for each Array [0][1]:
0
Enter numeric values for each Array [0][2]:
2
Enter numeric values for each Array [1][0]:
5
Enter numeric values for each Array [1][1]:
4
Enter numeric values for each Array [1][2]:
3
The 2-D Array contains :
1 0 2
5 4 3
Ascending :
The 2-D Array contains :
0 1 2
3 4 5

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