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.
Related
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
We must use functions, loops, data types, and pointers. I am honestly confused about pointers and how to use them in this context. I know they point to the memory address of a data type? I was wondering if maybe it would be possible to make the getElement function return a pointer to the array and then make the printArray function take a pointer as a parameter? would that work or is there a better way to do this.
Anyways this is what I have so far:
# include <stdio.h>
void getElement(int[][]arr, int row, int column){
for(int i=0; i< row;i++){
for(int k = 0; k< column; i++){
printf("Enter value for index [%i] [%i]: \n", i,k);
scanf("%i", arr[i][k]);
}
}
}
void printArray(int **arr, int row, int column){
printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", arr[i][j]);
if (j == column - 1)
printf("\n");
}
}
int main(){
int mat1[10][10],mat2[10][10],result[10][10], r1,r2,c1,c2;
printf("Enter rows and colums for first array: ");
scanf("%i %i",r1,c1);
printf("Enter rows and colums for second array: ");
scanf("%i %i",r2,c2);
while (r1 != c2){
printf("not doable, Enter value for rows and colums of first array ");
scanf("%i %i",r1,c1);
printf("Enter rows and colums for second array: ");
scanf("%i %i",r2,c2)
}
}
return 0;
}
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.
I made a program for making a pascal triangle and for the input of numbers ( rows ) > 5 , there is an alignment problem i.e for ncr > 10. Help me out please.
I have included the images for output of the program.
Output Image
#include<stdio.h>
int factorial(int number)
{
int fact=1;
for(int i=1; i<=number; ++i )
{
fact*=i;
}
return fact;
}
int ncr(int n, int r)
{
int ncr;
int fact1=factorial(n);
int fact2=factorial(n-r);
int fact3=factorial(r);
ncr = fact1 /(fact2 * fact3);
return ncr;
}
int main()
{
int rows;
printf("enter the number of rows :\n");
scanf("%d",&rows);
for(int n=0; n<rows; n++)
{
for(int i=1; i<=rows-n; i++)
{
printf(" ");
}
for(int r=0; r<=n; r++)
{
printf("%d ",ncr(n,r));
}
printf("\n");
}
return 0;
}
You can change the inner loop like this
for(int i=1; i<=rows-n; i++)
{
printf(" "); // Note the extra space
}
for(int r=0; r<=n; r++)
{
printf("%3d ",ncr(n,r)); // Changed to %3d
}
This will work upto 9 rows. If you want it to work for more rows, you can add another space in the first printf and change the second printf to %5d
printf can take a precision before the formatter. Change printf("%d ",ncr(n,r)); to printf("%3d ",ncr(n,r)); to make the numbers 3 characters wide. Also change printf(" "); to printf(" ");.
If you use
printf ("Width trick: %*d \n", 5, 10);
this will add 5 more spaces before the digit value.
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;
}