Can't change dynamically allocated array values inside a function call - c

I'm having some issues with C.
I was tasked with this assignment:
Create a dynamically allocated matrix (max 5x5), let the user fill in the values, then send it to a function along with an unallocated array (1D) which will be used to store all values of the matrix that are lower than the average of each row.
The 1D array should be defined in the main function and allocated inside the function that finds the desired elements.
This was going fine, I made my matrix, I sent it to the aforementioned function, but I could not change the values of the newly allocated 1D array.
#include<stdio.h>
#include<stdlib.h>
int lower_than_average(int **matrix, int **p, int m, int n) {
int i, j, sum;
float average;
int count = 0, counter = 0;
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++){
if (matrix[i][j] < average) count++;
}
}
*p = (int*)malloc(count * sizeof(int));
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++) {
if (matrix[i][j] < average) *p[counter++] = matrix[i][j];
}
}
printf("\n\n");
return(count);
}
void print_matrix(int **matrix, int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n\n");
}
}
int main(void) {
int m, n, **matrix, *elements_a = NULL;
int i, j, elements_a_size;
do {
printf("Enter the number of rows and columns [max 5x5]: ");
scanf("%d %d", &m, &n);
} while (n > 5 || n < 0 || m > 5 || m < 0);
matrix = (int**)malloc(m * sizeof(int*)); // Alokacija redaka
/*
X | <empty>
X | <empty>
X | <empty>
X | <empty>
*/
for (i = 0; i < m; i++) matrix[i] = (int*)calloc(n, sizeof(int)); // Alokacija stupaca
/*
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
*/
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Enter [%d][%d] element: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n\n");
print_matrix(matrix, m, n);
elements_a_size = lower_than_average(matrix, &elements_a, m, n);
printf("\nElements of rows smaller than the row average value: ");
for (i = 0; i < elements_a_size; i++) printf("%d ", elements_a[i]);
for (i = 0; i < m; i++) free(matrix[i]);
free(matrix);
return 0;
}
For the input: 3 3 then 1 2 3 4 5 6 7 8 9
It outputs literally nothing.
**p and *p[0]
instead of
*p[counter++]
it outputs this: 7 -2467754 -2467754
What am I doing wrong? I thought the allocation was correct and that I could access *(p + n) but it doesn't do anything...

You have to change the line
*p[counter++] = matrix[i][j];
by
(*p)[counter++] = matrix[i][j];
because [] take priority over *
See : Operator priorities in c/c++

The easiest way may be to create a one dimensional array that is big enough to store all the elements in an array, and then populate the 1D array as you go through each row of the 2D array. Once you have gone through the 2D array you copy the values that you need to an array that is just long enough and return.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int lower_than_average(int **matrix, int **p, int m, int n) {
int i, j, sum;
float average;
int count = 0, counter = 0;
int *buff;
buff = (int *)malloc(m * n * sizeof(int));
if (NULL == buff)
{
return (-1); //return negative value to show an error occurred
}
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++){
if (matrix[i][j] < average)
{
buff[count] = matrix[i][j];
count++;
}
}
}
*p = (int*)malloc(count * sizeof(int));
if(NULL == *p)
{
free(buff);
return (-1); //return negative value to show an error occurred
}
memcpy(*p, buff, count*sizeof(int));
free(buff);
printf("\n\n");
return(count);
}
void print_matrix(int **matrix, int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n\n");
}
}
int main(void) {
int m, n, **matrix, *elements_a = NULL;
int i, j, elements_a_size;
do {
printf("Enter the number of rows and columns [max 5x5]: ");
scanf("%d %d", &m, &n);
} while (n > 5 || n < 0 || m > 5 || m < 0);
matrix = (int**)malloc(m * sizeof(int*)); // Alokacija redaka
/*
X | <empty>
X | <empty>
X | <empty>
X | <empty>
*/
for (i = 0; i < m; i++) matrix[i] = (int*)calloc(n, sizeof(int)); // Alokacija stupaca
/*
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
*/
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Enter [%d][%d] element: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n\n");
print_matrix(matrix, m, n);
elements_a_size = lower_than_average(matrix, &elements_a, m, n);
printf("\nElements of rows smaller than the row average value: ");
for (i = 0; i < elements_a_size; i++) printf("%d ", elements_a[i]);
printf("\n"); //a new line so the prompt isn't on the same line as the output
for (i = 0; i < m; i++) free(matrix[i]);
free(matrix);
free(elements_a); //also free the memory dynamically allocated in the function!
return 0;
}
And the results:
Enter the number of rows and columns [max 5x5]: 3
3
Enter [0][0] element: 1
Enter [0][1] element: 2
Enter [0][2] element: 3
Enter [1][0] element: 4
Enter [1][1] element: 5
Enter [1][2] element: 80
Enter [2][0] element: 12
Enter [2][1] element: 2
Enter [2][2] element: 1
1 2 3
4 5 80
12 2 1
Elements of rows smaller than the row average value: 1 4 5 2 1

Related

Delete duplicate rows and columns of matrix

I need to delete (not skip while printing) the rows and columns of a matrix that appear more than once in program, and I should print only first row from the top that appears more than once or the first column from the left that appears more than once.
Example input:
1 2 3 2
4 5 6 5
1 2 3 2
7 8 9 8
After deleting:
1 2 3
4 5 6
7 8 9
Here's my code:
#include <stdio.h>
int main() {
int i, j, m, n,row,col, mat[200][200];
scanf("%d %d", &m, &n);
row = m; col = n;
for (i = 0; i < m; i++)
for (j = 0; j < m; j++)
scanf("%d", &mat[i][j]);
for (i = 0; i < m; i++)
for (j = 0; j < m; j++) {
if (mat[i][j] == mat[i++][j++])
row--;
if (mat[j][i] == mat[j++][i++])
col--;
}
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
Do you have any idea how to make the algorithm work for this task? Mine has mistakes.
Would you please try the following:
#include <stdio.h>
#define ROWS 200
#define COLS 200
#define TRUE 1
#define FALSE 0
/*
* delete k'th row from the m x n matrix
*/
void deleterow(int mat[ROWS][COLS], int m, int n, int k)
{
int i, j;
for (i = k; i < m - 1; i++) {
for (j = 0; j < n; j++) {
mat[i][j] = mat[i + 1][j];
}
}
}
/*
* delete k'th columns from the m x n matrix
*/
void deletecol(int mat[ROWS][COLS], int m, int n, int k)
{
int i, j;
for (j = k; j < n - 1; j++) {
for (i = 0; i < m; i++) {
mat[i][j] = mat[i][j + 1];
}
}
}
int main() {
int i, j, m, n,row,col, mat[ROWS][COLS];
int iref, jref; // reference indexes to compare
int match; // flag to show if the row/col duplicates
// read input matrix
scanf("%d %d", &m, &n);
row = m; col = n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &mat[i][j]);
// examine row by row
for (iref = 0; iref < m; iref++) {
// compare rows below iref and remove the row if duplicates
for (i = iref + 1; i < m; i++) {
match = TRUE;
for (j = 0; j < n; j++) {
if (mat[i][j] != mat[iref][j]) {
match = FALSE;
break;
}
}
if (match) {
deleterow(mat, m, n, i);
m--;
}
}
}
// examine column by column
for (jref = 0; jref < n; jref++) {
// compare columns more right than jref and remove the col if duplicates
for (j = jref + 1; j < n; j++) {
match = TRUE;
for (i = 0; i < m; i++) {
if (mat[i][j] != mat[i][jref]) {
match = FALSE;
break;
}
}
if (match) {
deletecol(mat, m, n, j);
n--;
}
}
}
// see the result
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%2d%s", mat[i][j], j == n - 1 ? "\n" : " ");
}
}
return 0;
}
Output with the provided example:
1 2 3
4 5 6
7 8 9
[Explanation]
As for the operations of rows:
First, focus on the top row as a "reference". The row is indexed by
the variable iref which is assigned to 0 at first.
Then compare the remaining rows with the reference, changing
the row index i from iref+1 (just below the reference row) to n-1
(the bottom row).
If a row duplicates with the reference, remove the row with the
deleterow() function and decrement the row size m by one.
The modification of m affects the for loops which compare the
loop variables with m, meaning the matrix size is updated immediately.
This is a preferable nature of the for loop (IMHO).
If the comparizon reaches the bottom row, increment iref and repeat
the comparisons again.
Finally every row has been compared to each other and the duplicates have
been deleted.
Then perform the similar operations with columns.

Creating a matrix that has its elements as the sum of the indices of that element

int main() {
int n = 0;
int matrix[n][n];
printf("Insert the order of the matrix:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = i + j;
printf("The matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
}
for a 2x2 matrix the output should be 0 1 1 2, but it is 1 2 1 2, and for a 3x3 matrix it should be 0 1 2 1 2 3 2 3 4 but it shows 2 3 4 2 3 4 2 3 4
The issue is that my output is always just my first row of the matrix, repeated n times. Any help?
#include <stdio.h>
int main() {
int n = 0;
printf("Insert the order of the matrix:");
scanf("%d", &n);
int matrix[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = i + j;
printf("The matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
}
}
You declare the array with size n equal zero. Then the length of a row is zero, hence the offset for each consecutive row is zero, too. That means all rows are stored at the same place. As a final result, the last row's values overwrite all the previous rows.
Do input n before declaring matrix[n][n].

How to raise a matrix to a power with double pointers in C

I am trying to raise a matrix to a power, using pointers but there is a mistake in my code I can't find.
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
>
int **alloc(int r, int c) {
int **d;
d = (int **)malloc(r * sizeof(int *));
for (int i = 0; i < r; i++) {
d[i] = (int *)malloc(c * sizeof(int));
}
return d;
void input(int **A, int r, int c) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("[%d][%d]=", i, j);
scanf_s("%d", &A[i][j]);
}
}
}
void output(int **A, int r, int c) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d ", A[i][j]);
}
printf("\n");
}
}
void power(int **A,int**D, int r, int c,int p) {
int i, j,k;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
D[i][j] = A[i][j];
}
}
while (p) {
// this is the matrix multiplication, where I attempt to multiply my matrix A with itself, and store the result in D, which initially started as A's copy, and p is the power I'm raising it to.
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
for (k = 0; k < c; k++)
D[i][j] = D[i][j] + A[i][k] * D[k][j];
}
}
p--;
}
}
void main() {
int r, c;
int **A, **D;
printf("rows A: ");
scanf_s("%d", &r);
printf("columns A: ");
scanf_s("%d", &c);
A = alloc(r, c);
printf("\nValues of A:\n");
input(A, r, c);
printf("\nMatrIX A is:\n");
output(A, r, c);
D = alloc(r, c);
printf("input the value you want to raise your matrix to: ");
int p;
scanf_s("%d", &p);
power(A, D, r, c, p);
printf("\nMatrix raised to said power is:\n");
output(D, r, c);
_getch();
}
When I input the rows and columns as 2 each, and input all values in the matrix as 1 and raise it to the power of 3, my answer should be
4 4
4 4
instead of
72 72
232 232
What is wrong in my code? If I were to print the D matrix before the multiplication, it would print it correctly, as:
1 1
1 1
Your two-step allocation looks okay, except that you don't free the memory after you're done. Your problem is in how you multiply the matrix:
Raising a matrix to a power involves multiplying it to itself. You can only do that if the matrix is square. You can replace all occurrences of rows r and columns c with a single dimension n.
When you do the actual multiplication:
D[i][j] = D[i][j] + A[i][k] * D[k][j];
you assign to D and read from it at the same time. Subsequent calculations (of the same multiplication) will see a changed value of D[i][j]. You will need a temporary "scratch" matrix.
Your code multiplies once too many. Also, Raising a matrix to the power of zero should yield the identity matrix.
Here's how your power function could look like:
void power(int **A, int **D, int n, int p)
{
int i, j,k;
// assign identity matrix to result D
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
D[i][j] = (i == j);
}
}
// create a scratch matrix
int **tmp = alloc(n);
while (p-- > 0) {
// multiply [tmp] = [A] * [D]
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
tmp[i][j] = 0;
for (k = 0; k < n; k++)
tmp[i][j] += A[i][k] * D[k][j];
}
}
// copy [D] = [tmp]
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
D[i][j] = tmp[i][j];
}
}
}
// TODO: clean up the scratch matrix
}

I want to use scanf to store values in a 2D array in C language. But it doesn't store it like how i want it to

#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=0;
int y=0;
int matrix[x][y];
printf("no. of rows \n");
scanf("%d",&x);
printf("no. of columns \n");
scanf("%d",&y);
printf("co-efficient of matrix \n");
for(int i = 0 ; i < x ; i++)
{
for(int j = 0 ; j < y ; j++)
{
scanf("%d",&matrix[i][j]);
};
};
for(int i = 0 ; i < x ; i++)
{
for(int j = 0 ; j < y ; j++)
{
printf("%d",matrix[i][j]);
};
printf("\n");
};
printf("%d",matrix[0][0]);
}
Output looks like this.
I input the values:
1 2 3 4 5 6
but then the in the output is:
4 5 6 4 5 6
moving
int matrix[x][y];
below
scanf("%d",&y);
have fixed the issue. My array had [0][0] dimensions as I was declaring the array right after declaring x and y which were initialized with 0 value. Hence declaring the array after getting the values of x and y fixed the problem.
I prefer malloc to allocate memory.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x,y;
printf("no. of rows \n");
scanf("%d",&x);
printf("no. of columns \n");
scanf("%d",&y);
int **matrix= (int **)malloc(sizeof(int)*x*y);
printf("co-efficient of matrix \n");
for(int i = 0 ; i < x ; i++)
{
for(int j = 0 ; j < y ; j++)
{
scanf("%d",&matrix[i][j]);
};
};
for(int i = 0 ; i < x ; i++)
{
for(int j = 0 ; j < y ; j++)
{
printf("%d",matrix[i][j]);
};
printf("\n");
};
printf("%d",matrix[0][0]);
free(matrix);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 0;
int y = 0;
printf("no. of columns \n"); scanf("%d", &x);
printf("no. of rows \n"); scanf("%d", &y);
printf("co-efficient of matrix \n");
int* p = (int*)malloc(x * y * sizeof(int));
//memset(p,0 , x*y*sizeof(int));
int **matrix=(int**)malloc(y*sizeof(int*));
for (int a = 0; a < y; a++)
{
matrix[a] = p + a * x; //matrix[a]=&p[a*x]
}
for (int j = 0; j < y; j++)
for (int i = 0; i < x; i++)
scanf("%d", &matrix[j][i]);
for (int j = 0; j < y; j++)
{
for (int i = 0; i < x; i++)
printf("%d", matrix[j][i]);
printf("\n");
}
free(matrix);
free(p);
}

Multiplication of 2D arrays, using pointers to pointers

I've been trying to implement a program in C, where i have two 2D arrays (created with pointers) and i have to multiply them (like multiplying matrices) and store the results to a third array. The dimensions of the arrays are given by the user. I've done the code but i'm getting wrong results. Is there something wrong with my formula? I wanted to do the reading and printing with functions.
For example, when i input n = 2, m = 2 and k =2. For each element of the matrix i input
A(0)(0) = 1, A(0)(1) = 2, A(1)(0) = 3, A(1)(1) = 4
and
B(0)(0) = 1,> B(0)(1) = 2, B(1)(0) = 3, B(1)(1) = 4.
The output should've been
C(0)(0) = 7, C(0)(1) = 10, C(1)(0) = 15 and C(1)(1) = 22.
Instead the output is
C(0)(0) = 7, C(0)(1) = 10, C(1)(0) = 33, C(1)(1) = 46.
I hope it's not hard to understand, i'm not allowed to post an image yet :(
#include <stdio.h>
#include <stdlib.h>
int **read(int **x, int i, int j);
int **prod(int **x, int **y, int n, int m, int k);
void print(int **x, int r, int c);
int main()
{
int n, m, k, i, j;
printf("Give n, m and k: ");
scanf("%d%d%d", &n, &m, &k);
int **A, **B, **C;
A = (int **)malloc(n*sizeof(int *));
for (i = 0; i < n; i++)
*(A+i) = (int *)malloc(k*sizeof(int));
B = (int **)malloc(k*sizeof(int *));
for (i = 0; i < k; i++)
*(B+i) = (int *)malloc(m*sizeof(int));
C = (int **)malloc(n*sizeof(int *));
for (i = 0; i < n; i++)
*(C+i) = (int *)malloc(m*sizeof(int)
for (i = 0; i < n; i++)
for (j = 0; j < k; j++)
A = read(A, i, j);
for (i = 0; i < k; i++)
for (j = 0; j < m; j++)
B = read(B, i, j);
C = prod(A, B, n, m, k);
print(C, n, m);
}
int **read(int **x, int i, int j)
{
printf("Give value to store in cell [%d][%d]: ", i, j);
scanf("%d", &x[i][j]);
return x;
}
int **prod(int **x, int **y, int n, int m, int k)
{
int i, j, l, sum;
int **res;
for (i = 0; i < n; i++)
for (j = 0; j < k; j++)
{
sum = 0;
for (l = 0; l < m; l++)
sum = sum + (x[i][l]*y[l][j]);
res[i][j] = sum;
}
return res;
}
void print(int **x, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j ++)
printf("C[%d][%d]: %d\t", i, j, x[i][j]);
printf("\n");
}
}
first understand the matrix without pointers
Matrix multiplication without pointers
int r1,r2,c1,c2;
int i,j,sum,k;
void matrixmul()
{
printf("Getting values for matrices");
printf("\n---------------------------");
printf("\n Enter the row for Matrix A: ");
scanf("%d",&r1);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c1);
printf("\n Enter the row for Matrix B: ");
scanf("%d",&r2);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c2);
int a[r1][c1];
int b[r2][c2];
int c[r1][c2];
if (!((c1==r2)&&(r1==c2)))
{
printf("Row column miss matching so cannot do matrix multiplication");
}
else
{
printf("\n A Matrix");
printf("\n ---------");
for (i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\n Enter %d and %d value: ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n B Matrix");
printf("\n ---------");
for (i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\n Enter %d and %d value: ",i,j);
scanf("%d",&b[i][j]);
}
}
}
for(i=0;i<c1;i++)
{
for(j=0;j<r2;j++)
{
for (k=0;k<r2;k++)
{
sum = sum + a[i][k]*b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
printf("\nResultant Matrix");
printf("\n----------------\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
void main()
{
matrixmul();
}
Output
Enter the row for Matrix A: 2
Enter the column for Matrix A: 3
Enter the row for Matrix B: 3
Enter the column for Matrix B: 2
A Matrix
Enter the 0 and 0 value:1
Enter the 0 and 1 value:2
Enter the 0 and 2 value:3
Enter the 1 and 0 value:3
Enter the 1 and 1 value:2
Enter the 1 and 2 value:11
B Matrix
Enter the 0 and 0 value:4
Enter the 0 and 1 value:5
Enter the 1 and 0 value:7
Enter the 1 and 1 value:8
Enter the 1 and 0 value:9
Enter the 1 and 1 value:10
Resultant Matrix
45 51
125 141
Matrix with pointers-in this program 2D is declared using pointer variable and get the values for the 2D array and display the values
void main()
{
int r = 2, c = 3, i, j, count;
int *a[r];//while declaring the array as pointer its number of rows are specified
for (i=0; i<r; i++)
{
a[i] = (int *)malloc(c * sizeof(int));//Allocate memory space for each row for 'c' columns
}
count = 0;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d",&a[i][j] );
}
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t ", a[i][j]);
}
printf("\n");
}
}
Output
3
2
1
1
2
3
3 2 1
1 2 3
The following is the final complete matrix multiplication using pointers
void input()
{
int r1, c1,r2,c2, i, j,k,sum;
printf("\n Enter the row for Matrix A: ");
scanf("%d",&r1);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c1);
printf("\n Enter the row for Matrix B: ");
scanf("%d",&r2);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c2);
if (!((c1==r2)&&(r1==c2)))
{
printf("Row column miss matching so cannot do matrix multiplication");
}
else
{
int *a[r1];
int *b[r2];
int *c[r1];
for (i=0; i<r1; i++)
{
a[i] = (int *)malloc(c1 * sizeof(int));
}
for (i=0; i<r2; i++)
{
b[i] = (int *)malloc(c2 * sizeof(int));
}
for (i=0; i<r1; i++)
{
c[i] = (int *)malloc(c2 * sizeof(int));
}
printf("\n A Matrix");
printf("\n ----------");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
printf("\n Enter the %d and %d value:",i,j);
scanf("%d",&a[i][j] );
}
}
printf("\n B Matrix");
printf("\n ----------");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
printf("\n Enter the %d and %d value:",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<c1;i++)
{
for(j=0;j<r2;j++)
{
for (k=0;k<r2;k++)
{
sum = sum + a[i][k]*b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
printf("\n Resultant Matrix");
printf("\n------------------");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
printf("%d\t ", c[i][j]);
}
printf("\n");
}
}
}
void main()
{
input();
}
Output
Enter the row for Matrix A: 2
Enter the column for Matrix A: 3
Enter the row for Matrix B: 3
Enter the column for Matrix B: 2
A Matrix
Enter the 0 and 0 value:1
Enter the 0 and 1 value:2
Enter the 0 and 2 value:3
Enter the 1 and 0 value:3
Enter the 1 and 1 value:2
Enter the 1 and 2 value:11
B Matrix
Enter the 0 and 0 value:4
Enter the 0 and 1 value:5
Enter the 1 and 0 value:7
Enter the 1 and 1 value:8
Enter the 2 and 0 value:9
Enter the 2 and 1 value:10
Resultant Matrix
45 51
125 141
run the above program one by one understand it then convert the final program into function as per your required,Thank you

Resources