Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit C Code with Explanation #5

Closed
ErSKS opened this issue Feb 5, 2020 · 47 comments
Closed

Submit C Code with Explanation #5

ErSKS opened this issue Feb 5, 2020 · 47 comments

Comments

@ErSKS
Copy link
Owner

ErSKS commented Feb 5, 2020

  • Q1: WAP to multiply two matrices (mn and pq) using the concept of pointer.
@Znis
Copy link

Znis commented Feb 5, 2020

matrixmult.txt

@AsimBhadra
Copy link

MatrixMultyPointer.txt

image
image

Ashim Bhadra
KCE076BCT010

@Arunpaneru
Copy link

/******************************************************************************

                        Online C Compiler.
            Code, Compile, Run and Debug C program online.

Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>
#include <conio.h>

void main()
{
int m,n,p,q,,i,j,k;
int A[20][20],B[20][20],M[20][20];
again: /asking for the size of matrices/
printf("enter the size of both matrices (mxn),(pxq)");
scanf("%dx%d/t%dx%d",&m,&n,&p,&q);
/checking whether the matrices of given dimension can be multiplied or not/
if (n!=p){
printf("/nmatrices cannot be multiplied due unmatched format/n");
goto again;
}
/reading array elements of matrix A given by user/
printf(" enter the elements of matrix A; ");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",(A+i)+j); / &A[i][j] is equivalent to (A+i)+j in pointer/

}} /reading second matrix/
printf("enter the elements of matrix B: ");
for(i=0;i<p;i++){
for(j=0;j<q;j++){
scanf("%d",(B+i)+j); / &B[i][j] is equivalent to (B+i)+j in pointer/
}}

 /*now multiplying the entered matrices*/
 printf("/nThe product of given matrices is= ");
 for(i=0;i<m;i++){
     for(j=0;j<q;j++){
         (*(*(M+i)+j))=0;    /* intializing the value of product= 0 initially */
        for(k=0;k<n;k++){
            (*(*(M+i)+j))+= (*(*(A+i)+k)*)* (*(*(B+k)+j)); /*multiplying matrices*/
            
        }
        /*displaying result*/
        printf("%d",(*(*(M+i)+j)))
     }printf("/n");
 }
//getch();

} //coded by Arun paneru (o76BCT009)

< b> Explanation: The above source code is to multiply two mxn and pxq matrices entered by user.
Here the concept of pointer has been used for the solving of the program. Each section of the program has been explained with the help of comment along with the code. Some equivalent formats are used here, for example : &A[i][j] is written as (A+i)+j while using pointer this indicates the location of array A. Similarly for getting value of the array element we write (((A+i)+j). Same concept is used for the other arrays too just array name is changed... ((*(M+i)+j))=0; this initialize the value of product initially 0. this has been done to make the product/output free from garbage value( i.e for proper output)._

@puzancozzu
Copy link

puzancozzu commented Feb 5, 2020

#include<stdio.h>
#include<conio.h>

void main()
{

int i,j,k,m,n,p,q;
error:
printf("\nEnter the value of m & n:");
scanf("%d%d",&m,&n);
printf("\nEnter the value of p & q:");
scanf("%d%d",&p,&q);
int (*a)[n],(*b)[q],(*pr)[q];

//checking matrix is possible or not..

if(n!=p){
    printf("\nMatrix multiplication is not possible for these matrix!");
    goto error;
}

//taking matrix

printf("Enter the Matrix A:\n");

for (i=0;i<m;i++)
{
    for (j=0; j< n;j++){
    scanf("%d",(*(a+i)+j));
    }
printf("\n");
}
printf("Enter the Matrix B:\n");
for (i=0;i<p;i++)
{
    for (j=0;j<q;j++){
    scanf("%d",(*(b+i)+j));
    }
printf("\n");
}

//MUltiplication....

printf(" Matrix A*B:\n");

for(i=0; i<m; i++)
{
    for(j=0; j<q; j++)
    {
        (*(*(pr+i)+j))=0;
        for(k=0; k<n; k++)
        {
            (*(*(pr+i)+j)) += (*(*(a+i)+k)) * (*(*(b+k)+j));
        }
        printf("%d\t",(*(*(pr+i)+j)));
    }
    printf("\n");

}
getch();

}

1

#Explanation...
the above program is to multiply a matrices of mn & pq where values of m,n,p&q are taken from keyboard using a pointer.
A pointer is a variable that contain memory address instead of values.
-as variable can hold one data type, pointer also can point only to one specific type (int,float,char,double).
just as pointer variable is defined as:
data_type *variable_name;

An array is itslef a internal pointer which represent base address of the array. i.e if x is a 1-D array then address of fist element is expressed as &x[0] either *x and for second element x[2] or (x+1) and so on.
so, Address of array and pointer for i th element is as:
&variable[i] -----> (variable+i)
And similarly , For valure of i th element
variable[i] -------> *(variable+i)
both represent the content of address that address.
And for 2-D array-pointer is decleared as:
data_type(variable)[size];
and value of i th row and j th coloumn;
&var[i][j]--------> (
(var+i)+j)
var[i][j]--------> ((var+i)+j)

so in above program pointer array a,band pr is declared( int (*a)(n).... which are equivalent to "int a[m][n].....).
while defining a arry pointer a num of row is not defined (i.e m) because in 2-D array pointer 'a' points to first n-element (i.e first row) and similarly, 'a+1' points to second n-elements (i.e second row ) and so on.

And the for printing and calculating equivalent array pointer value notation is used.
i.e *(*a+i)+j) which reperesents the value of variable 'a' of i th row and j th coloumn.

@rajat1222
Copy link

rajat1222 commented Feb 6, 2020

//WAP to multiply two matrix using pointer//

#include<stdio.h>
#include<conio.h>
#define N 3
void main(){
int p,q,m,n,i,j,k;
int (*a)[N],(*b)[N],(*c)[N];
printf("Enter the no of row and column of first matrix: ");
scanf("%d\t%d\t",&m,&n);
printf("\nEnter the no of row and column of second matrix: ");
scanf("%d\t%d\t",&p,&q);

if(p!=n){
printf("\nThe operation cannot be done. ");
}
else{
printf("\nEnter the element of first matrix: ");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d"\t,(a+i)+j);
}
printf("\n");
}
printf("\nEnter the element of second matrix:\n ");
for(i=0;i<p;i++){
for(j=0;j<q;j++){
scanf("%d\t",
(b+i)+j);
}
printf("\n");
}

for(i=0;i<m;i++){
for(j=0;j<m;j++){
mult=0;
for(k=0;k<p;k++){
mult=mult+ ((a+i)+k) * ((b+K)+j);
}
((c+i)+j)=mult;
}
printf("\n");
}
printf("\nThe multiplication of matrix: ");
for(i=0,i<m;i++){
for(j=0;j<q;j++){
printf("%d\t",((c+i)+j));
}
printf("\n");
}
}
getch();

}

The above program is about multiplication of two matrix mn and pq using pointer. For this we initialize two pointer array a and b ,then we ask the number of row and column of the matrix and if the column of first matrix is not equal to the row of second matrix then we cannot continue the operation ,else we ask the element of first matrix and second matrix.
The syntax for pointer variable is:
data_type *variable_name;

Like in the simple matrix multiplication ,we can write
a[i][j] = ((a+i)+j),
and for address
&a[i][j] = *(a+i)+j.

The main operation for the matrix multiplication using pointer is
mult=mult+ ((a+i)+k) * ((b+k)+j);

In conclusion,we can multiply two matrix form above code.
screenshot

@AxD99999
Copy link

AxD99999 commented Feb 6, 2020

Matprog.txt

Explanation: &a[i][j]= *(a+i)+j and a[i][j]= *(&a[i][j]) = ((a+i)+j) is the main logic behind this code. Other than this, the matrix multiplication has been carried out in the standard way(rows of first matrix multiplied to the columns of the second matrix). The matrix is multiplied and displayed in the same nested loop by initializing the matrix (in which the result is stored) multiple times at the beginning of every loop.

Created by: Asutosh K. Sedai, KCE076BCT011

@josephthapaa
Copy link

part1
part2
ss

This program multiplies two matrices whose order is gives by the user in the form of (Mxn) and (pxq).
If the number of columns of first matrix doesnt match the number of rows of second matrix then the user is asked for input again.
The first and second matrix are inputted in normal way except for the scanf part which uses pointer.
The multiplication part is also similar to the program without using pointer. The only difference is that this program uses pointer to carry out the same operation.
The core logic behind this program is:
for(i=0;i<m;i++){
for(j=0;j<q;j++){
((mult+i)+j) = 0 ;
for(k=0;k<n;k++){
((mult+i)+j) += ((a+i)+k)((*(b+k)+j)) ;
}
printf ("%d\t", ((mult+i)+j));
}
printf("\n");

This is equivalent way for the multiplication of two given matrices using pointer.
the expected output is obtained which has been attached with this comment.

@SKarmaa
Copy link

SKarmaa commented Feb 6, 2020

Submitted by: Shopnil Shrestha

Screenshot (49)
Screenshot (50)

Screenshot (51)

code.zip

@ShresthaPrajwal
Copy link

ShresthaPrajwal commented Feb 7, 2020

IMG-dbc5a6b87f56bd3aa044790d44e9e030-V
IMG-8fc4643f05054c9adbefff20cac4504b-V
20200207-181023
20200207-181034
Submitted by; Prajwal Shrestha

Explanation;
Initially,dimensions of matrices(mn and pq) are checked for the condition if(n==p) .Only when the condition is true we could process matrix multiplication.
After the condition is true, we ask for data to be stored in 2D pointers and did the matrix multiplication with following logic;
for(i=0;i<m;i++){
for(j=0;j<q;j++){
((c+i)+j)=0;
for(k=0;k<n;k++){
((c+i)+j)+=*( *(a+i)+k) * * ( * (b+k)+j);
}
printf("%d\t" , * ( * (c+i)+j));
}
printf("\n");
}

Conclusion;
Therefore,we were able to multiply two matrices(mn and pq) using pointers.

@Prajwal-debug
Copy link

Prajwal-debug commented Feb 7, 2020

[BY PRAJWAL ACHARYA]
#include<stdio.h>
#include<conio.h>

void main()
{

int i,j,k,m,n,p,q;
int (a)[10],(b)[10],(multiply)[10];
//Enter matrix order
clrscr();
re:
printf("\nEnter the order of first matrix m
n:");
scanf("%d%d",&m,&n);
printf("\nEnter the order of second matrix p
q:");
scanf("%d%d",&p,&q);
//checking the validity for multiplication
if(n!=p){
printf("\matrix multiplication is not possible for these matrix!");
printf("enter matrix order again ");
goto re;
}
// entering first matrix
printf("enter the first matrix:\n");
for (i=0;i<m;i++)
{
for (j=0; j< n;j++){
scanf("%d",(
(a+i)+j));
}
printf("\n");
}
//entering second matrix
printf("enter the second matrix :\n");
for (i=0;i<p;i++)
{
for (j=0;j<q;j++){
scanf("%d",((b+i)+j));
}
printf("\n");
}
printf(" matrix multiplication is \n");
//matrix multiplication core program
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
{
(
((multiply+i)+j))=0;
for(k=0; k<n; k++)
{
(
((multiply+i)+j)) += (((a+i)+k)) * (((b+k)+j));
}
//printing the matrix of order m
q
printf("%d\t",(((multiply+i)+j)));
}
printf("\n");

}
getch();
}
Screenshot (7)
Explanation:
The above program is the example of 2d array pointer.the program multiplies the two matrix of any order but valid for multiplication ie. the column of first matrix is equal to the row of second matrix.
the core logic of the program is multiply row elements of first matrix with column elements of another matrix of theri respective order and the outcome is assigned to row of the matrix and and so on for other rows and columns.Using this logic we are able to build the multiplication of matrix program.In this way the above program was coded.

@valar-morghulis-end
Copy link

matrix multiplication.txt

Explanation:
We used pointer to multiply the matrix. Here we asked user to input two matrices and then check the matrices, if it can be multiplied .Then multiplication is done . Array is used to read the matrix.
here,
m1[ i][ j]=((m1+i)+j)
Array is internal pointer, so this form can be used.
The matrix is multiplied and displayed

BY: AAJU PRAJAPATI

@Anuj-Gaida
Copy link

Anuj-Gaida commented Feb 7, 2020

Screenshot (2)
Screenshot (3)

#include<stdio.h>
#include<conio.h>
void main(){
int i,j,k,m,n,p,q;

int *(a)[10],*(b)[10],*c[10];
clrscr();
  re:printf("\n***please enter correct form**\n");
printf("Enter the row and colunm for first matrix: [m*n] ");
scanf("%d    %d" ,&m,&n);
printf("\nEnter the row and colunm for second matrix: [m*n] ");
scanf("%d    %d" ,&p,&q);
 if(n!=p){
goto re;
}
  printf("\nenter the elements for the first matrix:\n");
  for(i=0;i<m;i++){
   for(j=0;j<n;j++){
    scanf("%d" ,*(a+i)+j);
    }
   }
   printf("\nenter the elements for the second  matrix:\n");
  for(i=0;i<p;i++){
   for(j=0;j<q;j++){
    scanf("%d" ,*(b+i)+j);
    }

}
printf("the product of the matrices is:\n");
for(i=0;i<m;i++){
for(j=0;j<q;j++){
((c+i)+j)=0;
for(k=0;k<n;k++){
((c+i)+j)+=((a+i)+k) * ((b+k)+j);
}
printf("%d\t" ,((c+i)+j));
}
printf("\n");
}
getch();
}

EXPALANATION:-
1.The above code is to multiply two matrix (mn and pq) using pointers.
2.The program multiplies to matrix only if the number of column of first matrix is equal to the number of row in second matrix.
3.The core logic behind this program is to multiply the row elements of first matrix with colunm elements of second matrix of their respective order.
i:e for(i=0;i<m;i++){
for(j=0;j<q;j++){
((c+i)+j)=0;
for(k=0;k<n;k++){
((c+i)+j)+=((a+i)+k) * ((b+k)+j);
}
printf("%d\t" ,((c+i)+j));
}
printf("\n");
}
4.Here syntax such as (a+i)+j was used as equivalent for &a[i][j] and similarly ((a+i)+k ) was used as equivalent for a[i][j].
5.Lastly for loop were used for reading matrix elements.
i:e printf("\nenter the elements for the first matrix:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d" ,
(a+i)+j);
}
}
Therefore in this way the given two matrix were multiplied and required output was shown to console screen by reading matrix elements for user.

@bijay191
Copy link

bijay191 commented Feb 7, 2020

Screenshot (7)
Screenshot (6)
Screenshot (5)

Explanation :
In the above program we have calculated the product of two matrices of order input by the user using pointers. At first the condition for matrix multiplication is tested then the elements are taken as input from the user and after performing the calculation the product is displayed as shown in output above. Here the pointers are used which stores the value of element of matrices as ((a+i)+j) = &a[i][j] which is the main logic behind this program.
Bijay Kadariya
KCE 076 BCT 013

@ISxRocky
Copy link

ISxRocky commented Feb 7, 2020

matrix1
matrix2
matrix
Output

This is the matrices multiplication using pointer.
where &a[i][j] in array is equivalent to (*(a+i)+j) in pointer
and a[i][j] in array is equivalent to ((a+i)+j)) in pointer.
since to multiply matrices number of column of first matrix must be equal to number of rows of second matrix. So there is condition of if(p!=n){ in the program and re asking the data from user using goto. In this way matrix multiplication using pointer is done.
Name:Rocky Suwal
Roll No: 31

@romikgosai
Copy link

romikgosai commented Feb 7, 2020

MatMul1
MatMul2
MatMul3
The above program is about multiplication of two matrix mxn and pxq using pointer. For this we initialize two pointer array a and b ,then we ask the number of row and column of the matrix and if the column of first matrix is not equal to the row of second matrix then we cannot continue the operation, else we ask the element of first matrix and second matrix.
The syntax for pointer variable is:
data_type *variable_name;

Like in the simple matrix multiplication ,we can write
a[i][j] = ((a+i)+j),
and for address
&a[i][j] = (*(a+i)+j).

The main operation for the matrix multiplication using pointer is
mult=mult+ (((a+i)+k)) * (((b+k)+j));

In conclusion,we can multiply two matrix form above code.
73874211-2bb9de00-487b-11ea-9f4e-d9267d4303a7

@Safalm74
Copy link

Safalm74 commented Feb 8, 2020

Source code:

matrix_mul

Output:
result

Explanation:
loop

@Bishal77
Copy link

Bishal77 commented Feb 8, 2020

Capture1
Capture2
Capture3

Here, matrix multiplication using pointer is shown in simple method.

@rahulkc12
Copy link

rahulkc12 commented Feb 8, 2020

IMG_20200205_165433
IMG_20200205_165449
IMG_20200205_165424

Here,we have use concept of pointer.
At first we have asked the value of m and n and p and q.
The we have asked the value of matrices.which is stored in addresses
*(a+i)+j. Then at last ((c+i)+j) gives contents stored in addresses

@DivasRegmi
Copy link

Screenshot (165)
Screenshot (166)
Screenshot (167)
Screenshot (164)

@Arogyadh
Copy link

Arogyadh commented Feb 8, 2020

#include<stdio.h>
#include <conio.h>

int main(){
int m,n,o,p,i,j,k;
int m1[10][10],m2[10][10],M[10][10];
printf("Enter the size of matrix A(MxN)");
scanf("%dx%d",&m,&n);
printf("Enter the size of matrix (PxQ)");
scanf("%dx%d",&o,&p);
//checking order of matrices
if(o!=n){
printf("The given matrices cannot be multipiled");
}
else{
//scanning using internal pointer.
printf("Enter the conetents of 1st matrix:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",((m1+i)+j));
}
}
printf("Enter the conetents of 2nd matrix");
for(i=0;i<o;i++){
for(j=0;j<p;j++){
scanf("%d",(
(m2+i)+j));
}
}
//multiplying matrices using pointer
printf("The product of the matrices is:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
(((M+i)+j))=0;
for(k=0;k<m;k++){
(((M+i)+j))+= (((m1+i)+j)) * (((m2+i)+j)); //equivalent as M[i][j] +=m1[i][j] * m2[i][j]
}
printf("%d\t",(((M+i)+j)));
}
printf("\n");
}
}
return 0;
}


Explanation:
We first checked the matrices if they can be multiplied or not by cheking if m=n(MxN)(PxQ) matrices
Then contents were inputted using internal pointer (&a[i][j] is euqivalent to ((a+i)+j) and
value of a[i][j] is equal to (
((a+i)+j))
Then prdouct matrix was made zero using loop and then multipled by using the following code
(
((M+i)+j))+= (((m1+i)+j)) * ((*(m2+i)+j)); //equivalent as M[i][j] +=m1[i][j] * m2[i][j]
then it was printed to the use.


1

@Niru477
Copy link

Niru477 commented Feb 8, 2020

IMG20200208142732
IMG20200208140244
IMG20200208140452
Screenshot_2020-02-08-14-22-23-07

@Pragya024
Copy link

Pragya024 commented Feb 8, 2020

received_1703094386481938
received_508799039777534

received_632057794273675
Above program is to perform multiplication of two matrices of order m×n and p×q using pointer. The values of m,n,p and q are given by user.
Since the value of n and p should be equal in order to multiply these two matrices so firstly we check whether the multiplication between them is possible or not. Then the elements of two matrices are given by user.
Here,
&matrix1[i][j] is equivalent to *(matrix1+i)+j and matrix1[i][j] is equivalent to ((matrix1+i)+j).
The core logic for multiplication of two matrices:
Product[i][j]+=M1[i][k]*M2[k][j] has been used in above program using pointer.

@Pragya024
Copy link

received_187492539007873

@sagarsuwal10
Copy link

sagarsuwal10 commented Feb 8, 2020

ss1
ss2
ss3
ss4

//Explanation :
This program is about the multiplication of two matrix m n and p q using pointer where we initalize two pointer array a and b, then we ask for the number of row and column of the matrix and if the column of first matrix is not equal to the row of second matrix then we cannot continue the operation, else we ask the element of first matrix and second matrix.The syntax for pointer variable is
data_type*variable _name;
&a[i][j] in array is equivalent to *(a+i)+j in pointer And a[i][j] in array is equivalent to ((a+i)+j).
The main operation for the matrix multipication using pointer is mult=mult+(((a+i)+k)) * (((b+k)+j));
Atlast ,we can multiply matrix form in above program.

@shreyas-dhakal
Copy link

Source Code:
image
image

Output:
image
Here, the program is to multiply matrices of given dimensions. Firstly, we input the rows and the columns of the input matrices. The matrices can only be multiplied if the column of the first matrix is equal to row of the second matrix. If not equal we prompt the user to input the data again.
Now we input the contents of the matrices and then creating 3 nested loops, we multiply the matrics by using a logical expression:
image
Then after using this expression, we give the output to the user

@SameerCrestha
Copy link

SameerCrestha commented Feb 8, 2020

#include <stdio.h>
#include <conio.h>


int main(){
int i,j,k,m,n,p,q;
int a[20][20],b[20][20],mul[20][20];

re:
printf("Enter dimension of first matrix:");
scanf("%d%d",&m,&n);
printf("Enter dimension of second matrix:");
scanf("%d%d",&p,&q);

//Checks validity of dimension
if(n!=p){
    printf("Invalid dimension\n");
    goto re;       //Again asks dimension
}

 //Input section
printf("\nEnter first matrix:\n");
for(i=0;i<m;i++){
    for(j=0;j<n;j++){
        scanf("%d",(*(a+i)+j));
    }
}
printf("\nEnter second matrix:\n");
for(i=0;i<p;i++){
    for(j=0;j<q;j++){
        scanf("%d",(*(b+i)+j));
    }
}

    //Builds product matrix”mul” by multiplying matrix a & b
for(i=0;i<m;i++){
    for(j=0;j<q;j++){
        *(*(mul+i)+j)=0;
        for(k=0;k<n;k++){
            *(*(mul+i)+j)+=*(*(a+i)+k)**(*(b+k)+j);
        }
    }
}

//Displays product matrix
printf("Product matrix:\n");
for(i=0;i<m;i++){
    for(j=0;j<q;j++){
        printf("%d\t",*(*(mul+i)+j));
    }
    printf("\n");
}

return 0;
}

20200208_145059

The above program multiplies two matrices using the concept of pointer.

At first three 2D int arrays and seven normal integers. The dimension for matrix a and b are read and fed into integers m,n and p,q respectively. If n is not equal to p then it displays "Invalid message" and the user is asked to re-enter the dimensions.
After validation of dimension,the matrix a and b are read. For matrix a, the outer loop runs for m times and inner loop runs for n times, similar case for matrix b.
Now, the matrix a and b are multiplied using a triple nested loop where outer most loop runs up to m,mid loop runs to q and innermost runs to n or p.The multiplied matrix is stores in array "mul".

And then matrix mul is displayed.

In this program,
 *(*(a+i)+k) is equivalent to a[i][k], 
 *(*(b+k)+j) is equivalent to b[k][j] &
 *(*(mul+i)+j) is equivalent to mul[i][j].

This is because array itself is an
internal pointer. Here,double pointer is
used, as the arrays used are 2D i.e.
a[i][j]= *(a[i]+j) = *( *(a+i)+j).
In *( *(a+i)+j), *(a+i) points to ith row
while 'a' points to base address.

Thus, matrix multiplication can be done using these concept of pointer and arrays.

Submitted by:
Samir Shrestha (076BCT037)

@samshrita
Copy link

  • Q1: WAP to multiply two matrices (m_n and p_q) using the concept of pointer.

image
image
image

here for the matrix multiplication first of all the size of matrix is asked from user.
we have condition that for a matrix multiplication first matrix column must equal rows of secong matrix.
so in case of mismatched size it again asks for the size to be entered again.
then using pointer it is further coded.
in pointer :
The syntax for pointer variable is:
data_type *variable_name;

as inthe simple matrix multiplication ,
we have

a[i][j] = ((a+i)+j),
and for address ,
&a[i][j] = (a+i)+j.
the main control line is;
((product+i)+j)+= ((matrix1+i)+k)**(
(matrix2+k)+j);

thus matrix multiplication is done using concept of pointer.

@Sushil010
Copy link

program
program1
output
explanation:
the above program was done for the matrix multiplication using the pointers. It is similar as the matrix multiplication using the arrays. As array is considered as internal pointers,the multi dimensional array can also be represented with equivalent pointer notation like single dimensional array. some of the general notations used in pointers in the above multiplication program are
&a[i][j]=(a+i)+j and &b[i][j]=(b+i)+j ,these two pointer statements were used to access the matrix elements used in the program of two matrices. similarly multiplication was performed by using the formula ((product+i)+j)=((a+i)+k)**(*b+k)+j)

@shristikoju1
Copy link

shristikoju1 commented Feb 8, 2020

2020-02-08 (16)
2020-02-08 (17)
85138543_781961975621860_7460021325982072832_n

Discussion:In the above program, multiplication of two matrices is done using pointers. Here we used following general notations of pointers in the above program.
a[i][j]=((a+i)+j)
b[i][j]=((b+i)+j)
prod[i][j]=((prod+i)+j)
We have the formula for matrix multiplication:
(prod+i)+j)=(*(a+i)+k) * *(b+k)+j)
Hence,matrix multiplication is done by using pointers.

@samanchaugathi10
Copy link

#include<stdio.h>
#include<conio.h>

void main()
{

int i,j,k,m,n,p,q;
int (*x)[10],(*y)[10],(*multi)[10];
go:
printf("\nEnter the oder of first matrix ,m & n:");
scanf("%d%d",&m,&n);
printf("\nEnter the oder of second matrix ,p & q:");
scanf("%d%d",&p,&q);

if(n!=p){
printf("\nThese oder Matrix multiplication is not possible");
goto go;
}
printf("Enter the Matrix A:\n");

for (i=0;i<m;i++)
{
for (j=0; j< n;j++){
scanf("%d",((x+i)+j));
}
printf("\n");
}
printf("Enter the Matrix B:\n");
for (i=0;i<p;i++)
{
for (j=0;j<q;j++){
scanf("%d",(
(y+i)+j));
}
printf("\n");
}
printf(" Matrix A*B:\n");

for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
{
(((multi+i)+j))=0;
for(k=0; k<n; k++)
{
(((multi+i)+j)) += (((x+i)+k)) * (((y+k)+j));
}
printf("%d\t",(((multi+i)+j)));
}
printf("\n");

}
getch();
}
11

Explanation:
the above program is for multiplication of matrix using pointer .
In which equivalent pointer array notations are used to intialize pointer 2-D array and pointer array address and pointer array value noataions are used .
In pointer ,
&x[i][j] ====> ((x+i)+j) -for address
&y[i][j] =====> ((y+i)+j)
prod[i][j] =====>((prod+i)+j) for value
these are the evivalent 2-D arry pointer notations.
And as mulitplication of matrix is multiplication of row element and coloumn element and sum .
(((multi+i)+j)) += (((x+i)+k)) * (((y+k)+j))
it is general syntax for matrix multiplication.

Name: Saman Chaugathi

@Amit250
Copy link

Amit250 commented Feb 8, 2020

1
2
4

Explanation : Above code performs multiplication of two matrices where the order of matrices is to be given by user. The matrix is represented by an pointer representation of array.ie. array is written in its pointer notation.

The program proceeds with reading order and elements of first matrix and then it reads second matrix , using if condition .i.e. if(p!=q).... the order of matrices are compared. In case the column of first matrix isn't equal to row of second goto statement is executed which returns control to the line where user is again asked to enter order of second matrix , else program proceeds.

Here the key component of the code is logic of matrix multiplication and the pointer notation of array .i.e.
&a[i][j]=(a+i)+j
a[i][j]=
(*(a+i)+j)

Logic :
matrix is multiplied using iteration ; nested for loop run by variable i and j are related to row and column of first and second matrices respectively. the third for loop inside of loop run by i and j contains the core part where first and second matrix elements are multiplied and product is added to obtain the required result.
5

@ankit-11
Copy link

ankit-11 commented Feb 8, 2020

Screenshot (40)
Screenshot (41)
Screenshot (42)
Screenshot (45)
KCE076BCT006
ANKIT KAYASTHA

@samratpradhan
Copy link

Capture
Capture1

@abhishek-hyangol
Copy link

matrix1
matrix2

The above program helps to multiply a matrices of mn & pq where values of 'm','n','p' and 'q' are taken from keyboard using scanf function.

A pointer is a variable that contain memory address instead of values.

syntax of pointer:

data_type *variable_name;

array declaration ------------> equivalent pointer declaration
variable[i] ------------> (variable+i) [case of 1D array pointer]
var[i][j] ------------> ((var+i)+j) [case of 2 D array pointer]
&variable[i] ------------> *(variable+i) [assecing contents of 1D array pointer]
&var[i][j] ------------> ((var+i)+j) [assecing contents of 2D array pointer]

And the for printing and calculating equivalent array pointer value notation is used.
i.e *(*A+i)+j) which reperesents the value of variable 'A' of ith row and jth coloumn.
The above source code is to multiply two mxn and pxq matrices entered by user.
Here the concept of pointer has been used for the solving of the program.
Each section of the program has been explained with the help of comment along with the code.

The main formula used to multiply is:
(((mult+i)+j))+=((A+i)+k)((*(B+k)+j));_

@JayanSharma
Copy link

JayanSharma commented Feb 8, 2020

#include <stdio.h>
void main(){
int a[10][10],b[10][10],i,j,mult[10][10],m,n,p,q,k;
printf ("Enter the size of the martices a ");
scanf ("%d%d",&m,&n);
printf("Enter the size of the martices b");
scanf ("%d%d",&p,&q);
if (n=p){
printf("Enter the first martix: ");
for (i=0;i<m;i++){
for (j=0;j<n;j++){
printf ("a[%d][%d]",i,j);
scanf ("%d",*(a+i)+j);
}
}

for (i=0;i<p;i++){
    for (j=0;j<q;j++){
        printf ("b[%d][%d]",i,j);
        scanf ("%d",*(b+i)+j);
        
    }
}
for (i=0;i<m;i++){
    for (j=0;j<q;j++){
        *(*(mult+i)+j)=0;
        for (k=0;k<p;k++){
            *(*(mult+i)+j)+= *(*(a+i)+k)* *(*(b+k)+j);
        }
    }
}
printf ("The multiplied matrix is: ");
for (i=0;i<m;i++){
    for (j=0;j<q;j++){
        printf ("%d\t",*(*(mult+i)+j));
    }

    printf("\n");
}
}
else{
    printf ("Dimensions are not valid");
}

}

Explanation
In the above program we multiply the two matrices of which size and the matix elements are given by the user and to proceed further the column of first matrix must be same as the row of the second.
The core logic is as follows-
for (i=0;i<m;i++){
for (j=0;j<q;j++){
((mult+i)+j)=0;
for (k=0;k<p;k++){
((mult+i)+j)+= ((a+i)+k)* ((b+k)+j);

@bikeshmdr
Copy link

#include<stdio.h>
#include<conio.h>

void main() {
int m, n, p, q, i, j, k;
clrscr();
printf("Enter the size of matrix A(mn); ");
re:
scanf("%d
%d", &m, &n);
printf("Enter the size of matrix B(pq);" );
scanf("%d
%d", &p, &q);
int(*a)[n], (*b)[q],(*c)[q];

if(n != p) {
printf("Enter the size of matrix A again; ");
goto re;
} else {
printf("Enter the element of matrix A; ");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", ((a + i) +j));
}
}
printf("Enter the element of matrix B; ");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", (
(b + i) +j));
}
}
printf("The multiplication of matrix is; ");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
((c + 0) + 0) = 0;
for(k = 0; k < n; k++) {
((c + i) + j) += ((a + i) + j) * ((b + k) + i);
}
printf("%d", ((c + i) + j));
}
printf("\n");
}

}

getch();
}

Explanation
The above program is to multiply the two matrices of mn and pq where the size of matrix mn and pq are entered by the user.

  1. For the matrix multiplication the column of first matrix must be equal to the row of the second matrix so it is checked by the code "if(n != p)" if they are not equal user is send back to re-enter the size of matrices until the condition is satisfied.
  2. Then the elements of the both matrices are taken from the user.
    syntax:
    for(i = 0; i < m; i++) {
    for(j = 0; j < n; j++) {
    scanf("%d", (*(a + i) +j));
    }
    }
  3. Then the entered matrices are multipled and printed at same time.
    syntax:
    for(i = 0; i < m; i++) {
    for(j = 0; j < n; j++) {
    ((c + i) + j) = 0; /* initilization of array element to zero */
    for(k = 0; k < n; k++) {
    ((c + i) + j) += ((a + i) + j) * ((b + k) + i);
    }
    printf("%d", ((c + i) + j));
    }
    printf("\n");
    }

@Marshall315
Copy link

#include <stdio.h>
#include<conio.h>
void main()
{
int m,n,p,q,i,j,k;
int a[20][20], b[20][20], multy[20][20];
re:
printf("\nEnter dimentions of first matrix(mXn) :");
scanf("%dX%d",&m,&n);
printf("\nEnter dimention of second matrix(pXq) :");
scanf("%dX%d",&p,&q);
if(n!=p)
{
printf("\nNot possible! Enter again!");
goto re; /* Goto top if matrix multiplication is not possible /
}
printf("\nEnter elements first matrix :\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d",(
(a+i)+j)); /* (a+i)+j = &a[i][j] /
}
}
printf("\nEnter elements of second matrix :\n");
for(i=0; i<p; i++)
{
for(j=0; j<q; j++)
{
scanf("%d",(
(b+i)+j)); /
(a+i)+j = &a[i][j] /
}
}
printf("The product is:\n"); /
Multiply and display the matrix in the same nested loop /
for(i=0; i<m; i++)
{
for(j = 0; j<q; j++)
{
(
(
(multy+i)+j))=0; /* ((multy+i)+j) = (&multy[i][j]) = multy[i][j] /
for(k=0; k<p; k++)
{
(
(
(multy+i)+j))+=(((a+i)+k))(((b+k)+j));
}
printf("%d\t",(
(*(multy+i)+j)));
}
printf("\n");
}
}

/*
Created by: sumit Nehmafuki
roll no: KCE076BCT0461
*/

Explanation:
This program is to multiply the two matrices of size mxn and pxq .the size of matrix is given by user .
here pointer is used instead of simple variables.the core logic of the program is
for(i=0; i<m; i++)
{
for(j = 0; j<q; j++)
{
(((multy+i)+j))=0; /* ((multy+i)+j) = (&multy[i][j]) = multy[i][j] /
for(k=0; k<p; k++)
{
(
(
(multy+i)+j))+=(((a+i)+k))((*(b+k)+j)); .

@pawanparichit
Copy link

#include <stdio.h>
#include <conio.h>

void main(){
int M,N,O,P,i,j,k,a=0;
int m1[20][20];
int m2[20][20];
int mult[20][20];
clrscr();

printf("Enter the order of matrix 1 :");
scanf("%d%d",&M,&N);

printf("\nEnter first matrix of %dx%d order :\n", M, N);
for (i = 0; i < M; i++)
{
    for (j = 0; j < N; j++)

        scanf("%d", (*(m1 + i) + j));

}
printf("\nThe matrix 1 is : \n");
for (i = 0; i < M; i++)
{
    for (j = 0; j < N; j++)

        printf(" %d", *(*(m1 + i) + j));
        printf("\n");

}

printf("\nEnter the order of matrix 2 :");
scanf("%d%d",&O,&P);

if(O!=N){
printf("\n!!THE ORDER OF MATRIX 2 IS INVALID FOR MULTIPLICATION!!");
}else{
printf("\nEnter second matrix of %dx%d order :\n", O, P);

for (i = 0; i < O; i++)
{
for (j = 0; j < P; j++)

        scanf("%d", (*(m2 + i) + j));

}
printf("\nThe matrix 2 is : \n");
for (i = 0; i < O; i++)
{
    for (j = 0; j < P; j++)

        printf(" %d", *(*(m2 + i) + j));
        printf("\n");

}

//multiplication part

for (i = 0; i < M; i++)
{
    for (j = 0; j < P; j++)
    {
    for (k = 0; k < N; k++){
    a = a + (*(*(m1 + i) + k)) * (*(*(m2 + k) + j));
        *(*(mult + i) + j) = a;
        a = 0;
    }
}

//display result
printf("\n\nMultiplication of given matrices (order of %dx%d) is : \n",M,P);
for (i = 0; i < M; i++)
{
for (j = 0; j < P; j++)

        printf(" %d", *(*(mult + i) + j));


    printf("\n");
}

}
getch();
}

Explanations:

  1. Stdio.h and conio.h are header files used.
  2. Int data type is used.
  3. Pointers are used.
  4. Nested for loops are used.

@Anish713
Copy link

Anish713 commented Feb 8, 2020

1581178165131

@ghost
Copy link

ghost commented Feb 8, 2020

image
image
image
image

//Explanation

  • The purpose of this program is to show the product of two matrices of size mxn and pxq
  • This program is based on the law of matrix multiplication that matrix multiplication is only possible
    when the number of columns of first matrix is equal to number of rows of second matrix (n=p)
  • In the product, The number of rows is equal to m and number of columns is equal to q (which is
    why multi[i][j] += A[i[][k] * B[k][j] )
  • The core logic used in this prgram with the use of pointer is 👍
    ((multi+i) + j) += ((A+i)+k) * ((B+k)+j);
    In the form of array,
    multi[i][j] += A[i[][k] * B[k][j] )

//Suyan Shrestha
//KCE076BCT048

@Anish713
Copy link

Anish713 commented Feb 8, 2020

1581178346460

@Samik010
Copy link

Samik010 commented Feb 8, 2020

  • Q1: WAP to multiply two matrices (m_n and p_q) using the concept of pointer.
    By: Samik Bhattarai
    code1
    code2
    code3
    Explanation:
    -Two matrices are multiplied using the concept of pointer
    -Matrices are scanned using the syntax:
    scanf("%d",*(a+i)+j);
    -The core logic of this multiplication is:
    ((mult+i)+j)= ((mult+i)+j) + ((a+i)+k) * ((b+k)+j);

@indi2244
Copy link

indi2244 commented Feb 8, 2020

received_661538704616479
received_2288922581209063
received_794560764345938
received_790777028106457
received_178702066743113
By :-Indira kasichhwa
The above code is the sample of simple multiplication of two matrix of size mn and pq where that data are inputted by users
The user when opens the program , asks for the size of the matrices and compares if the matrices can be multiplied or not
The above program is done by the use of pointer the matrix are scanned using
Scanf("%d",(A+i)+j);
Also the main logic of this program is
((PRODUCT+i)+j ) += ((A+i)+k)
((B+k)+j);
And the output is taken out in matrix form

@Aayush-002
Copy link

matrixproduct.txt

@siddhartha1010
Copy link

#include<stdio.h>

#include<conio.h>

void main(){
int m,n,p,q,i,j,k;
clrscr();
printf("enter the size of matrix A(mn);");
re:
scanf("%d%d",&m,&n);
printf(enter the size of matrix B(pq);");
scanf("%d%d",&p,&q);
int(*a)[n],(*b)[q],(*c)[q];
if (n!=p){
printf("enter the size of matrix A:");
goto re;
}else{

printf("enter the element of matrix A:");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",((a+i)+j));
}
}
printf("enter the element of matrix B:");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",((b+i)+j));
}
}
printf("The multiplication of matrix is:");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
((c+0)+0)=0;
for(K=0;k<n;k++){
((c+i)+j)+=((a+i)+j)*((b+k)+i);
}
printf("%d",((c+i)+j));
}
printf("\n");
}

}
getch();
}

EXPLANATION:
This program helps to multiply the matrix of size entered by the user.It checks wheather the column of first matrix is equal to row of second matrix or not.The number of both matrix are taken from user .pointer is used instead of simple varaiable.for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",(*(a+i)+j)0;
} and then multiplied and printed

@RabitKhaitu
Copy link

Screenshot (8)
Screenshot (7)
Screenshot (4)

first of all the integer variables are defined.
Variables for matrix A,B and C are defined as pointer variables.
Variables i,j and k are defines for the no of loops for matrix.
Again the variables m,n,p and q are defined for the order of variables as (mn) and (pq).

Then the user is asked to enter the row and column of the two matrices.
Here if n!=q then the user is again asked to enter the order of the matrices A and B.
After that the user is asked for entering the row and element of the matrices.
Then the further processing is done and the output is displayed.

@0Pratik0
Copy link

0Pratik0 commented Feb 8, 2020

multiplication.txt
roll no-27
Pratik Tamrakar

@ErSKS ErSKS closed this as completed Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests