Modify the program to do 3 x 3 matrix multiplication: #include <iostream> #inclu
ID: 3692714 • Letter: M
Question
Modify the program to do 3 x 3 matrix multiplication:
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
int A[3] [3] = {{1,2,5},{3,4,6},{6,7,8}};
int B[3] [3] = {{5,6},{7,8},{3,5,8}};
int C[3] [3];
struct Position{
int row;
int col;
};
void *CalculateElement(void *pos){
Position *p = (Position *)pos;
C[p->row][p->col] = 0;
for(int i = 0; i < 3; i++){
C[p->row][p->col] += A[p->row][i]*B[i][p->col];
}
pthread_exit(NULL);
}
const int NUM_THREADS = 4;
int main()
{
pthread_t threads[NUM_THREADS];
for(int i = 0; i < NUM_THREADS; i++){
Position *p = new Position;
p->row = i/3;
p->col = i%3;
pthread_create(&threads[i], NULL, CalculateElement, (void *)p);
}
for(int i = 0; i < NUM_THREADS; i++){
pthread_join (threads[i], NULL);
}
cout << "The result matrix is: ";
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
cout << C[i][j] << " ";
}
cout << endl;
}
}
Explanation / Answer
//easiest program to perform matrix multiplication
#include<iostream.h>
#include<conio.h>
using namespace std;
int main() {
//three 2 dimensional arrays for keeping matrix 1,matrix 2 and resultant matrices
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;
//read the values of the first matrix.
cout<<" Enter the value of First Matrix : ";
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cin>>a[i][j];
}
}
//read the value for second matrix 2
cout<<" Enter the value to the second Matrix: ";
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cin>>b[i][j];
}
}
//print the matrix 1
cout<<"The First Matrix is: ";
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cout<<a[i][j];
}
cout<<" ";
}
//print the second matrices with value
cout<<"The Second Matrix is : ";
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cout<< b[i][j];
}
cout<<" ";
}
//Multiplication Logic
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
sum = 0;
for (k = 0; k <= 2; k++) {
//multiplying takes place here beteen 2 matrices and summing of 2 cases for each positions
sum = sum + a[i][k] * b[k][j];
}
//assign the result of each position of matrix based on row index and column index positions
c[i][j] = sum;
}
}
//print the final matrix with calculated values
cout<<" Resultant matrix : ";
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cout<< c[i][j];
}
cout<<" ";
}
getch();
return (0);
}
#include<iostream.h>
#include<conio.h>
using namespace std;
int main() {
//three 2 dimensional arrays for keeping matrix 1,matrix 2 and resultant matrices
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;
//read the values of the first matrix.
cout<<" Enter the value of First Matrix : ";
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cin>>a[i][j];
}
}
//read the value for second matrix 2
cout<<" Enter the value to the second Matrix: ";
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cin>>b[i][j];
}
}
//print the matrix 1
cout<<"The First Matrix is: ";
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cout<<a[i][j];
}
cout<<" ";
}
//print the second matrices with value
cout<<"The Second Matrix is : ";
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cout<< b[i][j];
}
cout<<" ";
}
//Multiplication Logic
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
sum = 0;
for (k = 0; k <= 2; k++) {
//multiplying takes place here beteen 2 matrices and summing of 2 cases for each positions
sum = sum + a[i][k] * b[k][j];
}
//assign the result of each position of matrix based on row index and column index positions
c[i][j] = sum;
}
}
//print the final matrix with calculated values
cout<<" Resultant matrix : ";
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cout<< c[i][j];
}
cout<<" ";
}
getch();
return (0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.