From e8b05038c1a78febfef3b5e40032408cc770333c Mon Sep 17 00:00:00 2001 From: Jayesh Jackson <115567183+Jayesh0006@users.noreply.github.com> Date: Tue, 11 Oct 2022 22:40:01 +0530 Subject: [PATCH] Create ProductOf2Matirx.cpp Product of 2 matrixes using cpp. --- ProductOf2Matirx.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 ProductOf2Matirx.cpp diff --git a/ProductOf2Matirx.cpp b/ProductOf2Matirx.cpp new file mode 100644 index 00000000..3cdbd74d --- /dev/null +++ b/ProductOf2Matirx.cpp @@ -0,0 +1,103 @@ +#include +#include + +struct Matrix{ + int Dimention; + int *A; +}; + +//? for frist Matrix +void create0(struct Matrix *p){ + p->A = (int *)malloc(2*p->Dimention*sizeof(int)); + printf("\nEnter each elements of first Matrix: \n"); + int terms = 0; + for (int i = 0; i < p->Dimention; i++){ + for (int j = 0; j < p->Dimention; j++){ + printf("%d.%d ", i+1,j+1); + scanf("%d", &p->A[terms]); + terms++; + } + printf("\n"); + } +} + +void display0(struct Matrix p){ + printf("\nThe first matrix is: \n"); + int terms = 0; + for (int i = 0; i < p.Dimention; i++){ + for (int j =0; j < p.Dimention; j++){ + printf("%d\t",p.A[terms]); + terms++; + } + printf("\n"); + } +} + +//? For second matrix +void create1(struct Matrix *q){ + q->A = (int *)malloc(2*q->Dimention*sizeof(int)); + printf("\nEnter each elements of second Matrix: \n"); + int terms = 0; + for (int i = 0; i < q->Dimention; i++){ + for (int j = 0; j < q->Dimention; j++){ + printf("%d.%d ", i+1,j+1); + scanf("%d", &q->A[terms]); + terms++; + } + printf("\n"); + } +} + +void display1(struct Matrix q){ + printf("\nThe second matrix is: \n"); + int terms = 0; + for (int i = 0; i < q.Dimention; i++){ + for (int j =0; j < q.Dimention; j++){ + printf("%d\t",q.A[terms]); + terms++; + } + printf("\n"); + } +} + +//! Product of two matrix; +void product(struct Matrix *p, struct Matrix *q){ + struct Matrix *multi; + int terms = 0; + multi->Dimention = p->Dimention; + multi->A = (int *)malloc(multi->Dimention*multi->Dimention*sizeof(int)); + multi->A[terms] = 0; + printf("\nThe product of both the matrix is: "); + for (int i = 0; i < multi->Dimention; i++){ + for (int j = 0; j < multi->Dimention; j++){ + multi->A[terms] += p->A[terms] * q->A[terms]; + terms ++; + } + } + // for printing result; + for (int i =0; i < multi->Dimention; i++){ + for (int j = 0; j < multi->Dimention; j ++){ + printf("%d\t", multi->A[terms]); + terms ++; + } + printf("\n"); + } + printf("\n\n"); +} + +int main(){ + struct Matrix p, q; + printf("\nEnter the Dimention of first Square Matrix: "); + scanf("%d", &p.Dimention); + create0(&p); + display0(p); + printf("\n"); + printf("\nEnter the Dimention of second Square Matrix: "); + scanf("%d", &q.Dimention); + create1(&q); + display1(q); + printf("\n"); + + product(&p, &q); + return 0; +}