Matrix multiplication - KSU Faculty Member websites

GE 211 Programming in C++
Array &Matrix
Dr. Ahmed Telba
Initializing an Array
• DataType ArrayName[dimension] = { element1, element2, …, elementn};
• examples of declaring an initializing arrays:
• int number[] = {18, 42, 25, 12, 34, 15, 63, 72, 92, 26, 26, 12};
• double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
#include <iostream>
using namespace std;
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
cout << "2nd member = " << distance[1] << endl;
cout << "5th member = " << distance[4] << endl;
return 0;
}
• This would produce:
• 2nd member = 720.52
• 5th member = 6.28
#include <iostream>
using namespace std;
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
cout << "Distance 1: " << distance[0] << endl;
cout << "Distance 2: " << distance[1] << endl;
cout << "Distance 3: " << distance[2] << endl;
cout << "Distance 4: " << distance[3] << endl;
cout << "Distance 5: " << distance[4] << endl;
return 0;
}
•
•
•
•
•
•
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 468.78
Distance 5: 6.28
This would produce:
// You can use such a constant in a for loop to scan the array and access each of its members. Here is an
example:
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
cout << "Members of the array\n";
for(int i = 0; i < numberOfItems; ++i)
cout << "Distance " << i + 1 << ": " << distance[i] << endl;
return 0;
}
In both cases, this would produce:
Members of the array
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 468.78
Distance 5: 6.28
Filling Up an Array
When you declare an array without initializing it, we have mentioned that the compiler reserves an amount of memory
space for the members of the array. But that is only what the compiler does. Each part of such reserved space is
filled with garbage. Therefore, you must make sure that you know the value held by a member of the array before
making any attempt to process the value held by that member of the array. Consider the following example:
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems];
cout << "Distance 1: " << distance[0] << endl;
cout << "Distance 2: " << distance[1] << endl;
cout << "Distance 3: " << distance[2] << endl;
cout << "Distance 4: " << distance[3] << endl;
cout << "Distance 5: " << distance[4] << endl;
return 0;
}
This would produce:
Distance 1: -9.25596e+061
Distance 2: -9.25596e+061
Distance 3: -9.25596e+061
Distance 4: -9.25596e+061
Distance 5: -9.25596e+061
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems] = {44.14, 720.52, 96.08};
cout << "Distance 1: " << distance[0] << endl;
cout << "Distance 2: " << distance[1] << endl;
cout << "Distance 3: " << distance[2] << endl;
cout << "Distance 4: " << distance[3] << endl;
cout << "Distance 5: " << distance[4] << endl;
return 0;
}
•
•
•
•
•
•
This would produce:
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 0
Distance 5: 0
#include <iostream>
using namespace std;
int main()
{
const int NumberOfItems = 5;
double distance[NumberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
cout << "Distance 1: " << distance[0] << endl;
cout << "Distance 2: " << distance[1] << endl;
cout << "Distance 3: " << distance[2] << endl;
cout << "Distance 4: " << distance[3] << endl;
cout << "Distance 5: " << distance[4] << endl;
cout << "Distance 6: " << distance[5] << endl;
cout << "Distance 7: " << distance[6] << endl;
cout << "Distance 8: " << distance[7] << endl;
return 0;
}
This would produce:
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 468.78
Distance 5: 6.28
Distance 6: 2.64214e-308
Distance 7: 2.12414e-314
Distance 8: 1.00532e-307
Operations on Arrays
#include <iostream>
using namespace std;
int main()
{
// We know that we need a constant number of elements
const int max = 10;
int number[max];
// We will calculate their sum
int sum = 0;
cout << "Please type 10 integers.\n";
for( int i = 0; i < max; i++ )
{
cout << "Number " << i + 1 << ": ";
cin >> number[i];
sum += number[i];
}
cout << "\n\nThe sum of these numbers is " << Sum << "\n\n";
return 0;
}
This would produce:Please type 10 integers.
Number 1: 120
Number 2: 42
Number 3: 75
Number 4: 38
Number 5: 904
Number 6: 6
Number 7: 26
Number 8: 55
Number 9: 92
Number 10: 20
The sum of these numbers is 1378
// A 2-Dimensional array
double distance[][4] = {
{ 44.14, 720.52, 96.08, 468.78 },
{ 6.28, 68.04, 364.55, 6234.12 }
};
using namespace std;
int main()
{
// A 2-Dimensional array
double distance[2][4] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04, 364.55, 6234.12};
// Scan the array from the 3rd to the 7th member
cout << "Members of the array";
cout << "\nDistance [0][0]" << ": " << distance[0][0];
cout << "\nDistance [0][1]" << ": " << distance[0][1];
cout << "\nDistance [0][2]" << ": " << distance[0][2];
cout << "\nDistance [0][3]" << ": " << distance[0][3];
cout << "\nDistance [1][0]" << ": " << distance[1][0];
cout << "\nDistance [1][1]" << ": " << distance[1][1];
cout << "\nDistance [1][2]" << ": " << distance[1][2];
cout << "\nDistance [1][3]" << ": " << distance[1][3];
cout << endl;
return 0;
}
This would produce:
Members of the array
Distance [0][0]: 44.14
Distance [0][1]: 720.52
Distance [0][2]: 96.08
Distance [0][3]: 468.78
Distance [1][0]: 6.28
Distance [1][1]: 68.04
Distance [1][2]: 364.55
Distance [1][3]: 6234.12
Multidimensional Arrays
[0][0] [0][1] [0][2] [0][3] [0][4]
[1][0] [1][1] [1][2] [1][3] [1][4]
[2][0] [2][1] [2][2] [2][3] [2][4]
int anArray[3][5] ={
{ 1, 2, 3, 4, 5, }, // row 0
{ 6, 7, 8, 9, 10, }, // row 1
{ 11, 12, 13, 14, 15 } // row 2
};
Matrix addition code
#include <iostream>
void mult_matrices(int a[][3], int b[][3], int result[][3]);
void print_matrix(int a[][3]);
void main(void)
{
int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} };
int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} };
int r[3][3];
mult_matrices(p, q, r);
cout<<_matrix(r);
}
void mult_matrices(int a[][3], int b[][3], int result[][3])
{
int i, j, k;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
for(k=0; k<3; k++)
{
result[i][j] = a[i][k] + b[k][j];
}
}
}
}
void print_matrix(int a[][3])
{
int i, j;
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}}
#include <stdio.h>
void add_matrices(int a[][3], int b[][3], int result[][3]);
void print_matrix(int a[][3]);
void main(void)
{
int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} };
int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} };
int r[3][3];
add_matrices(p, q, r);
printf("\nMatrix 1:\n");
print_matrix(p);
printf("\nMatrix 2:\n");
print_matrix(q);
printf("\nResult:\n");
print_matrix(r);
}
void add_matrices(int a[][3], int b[][3], int result[][3])
{
int i, j;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
}
void print_matrix(int a[][3])
{
int i, j;
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
Matrix addition
initialization of Two-Dimensional Array
• An two-dimensional array can be initialized
along with declaration. For two-dimensional
array initialization, elements of each row are
enclosed within curly braces and separated
by commas. All rows are enclosed within curly
braces.
Referring to Array Elements
• To access the elements of a two-dimensional array, we need a
pair of indices: one for
the row position and one for the column position. The format
is as simple as:
name[rowIndex][columnIndex]
Arrays example
#include <iostream>
using namespace std;
int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main () {
for ( n=0 ; n<5 ; n++ ) { result += billy[n];
}
cout << result;
system("pause");
return 0; }
Multidimensional arrays
• Multidimensional arrays can be described as
"arrays of arrays". For example, a
bidimensional array can be imagined as a
bidimensional table made of elements, all of
them of a same uniform data type.
• int jimmy [3][5];
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT][WIDTH];
int n,m; int main () {
for (n=0;n<HEIGHT;n++) for (m=0;m<WIDTH;m++)
{ jimmy[n][m]=(n+1)*(m+1);
}
return 0;
}
• jimmy[1][3]
•
#define HEIGHT 3
• #define HEIGHT 4
// arrays as parameters 5 10 15 2 4 6 8 10
#include <iostream>
using namespace std;
void printarray (int arg[], int length)
{ for (int n=0; n<length; n++) cout << arg[n] << " ";
cout << "\n";
} int main () { int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
return 0; }
• char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
/ null-terminated sequences of characters
#include <iostream>
using namespace std;
int main ()
{
char question[] = "Please, enter your first name: ";
char greeting[] = "Hello, ";
char yourname [80];
cout << question;
cin >> yourname;
cout << greeting << yourname << "!";
return 0;
}
declaring an initializing arrays:
•
Here are examples of declaring an initializing arrays:
•
•
•
•
int number[12] = {18, 42, 25, 12, 34, 15, 63, 72, 92, 26, 26, 12};
double distance[5] = {44.14, 720.52, 96.08, 468.78, 6.28};
int number[] = {18, 42, 25, 12, 34, 15, 63, 72, 92, 26, 26, 12};
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
• int n, m; double x[5][6];
• FILE *inp; inp = fopen("IN.DAT","r");
• for (n = 0; n < 5; ++n) { for (m = 0; m < 6; ++m)
fscanf (inp, "%lf", &x[n][m]);
• } fclose (inp);
#include <iostream>
using namespace std;
void MultiplyWithOutAMP() {
int aMatrix[3][2] = {{1, 4}, {2, 5}, {3, 6}};
int bMatrix[2][3] = {{7, 8, 9}, {10, 11, 12}};
int product[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
// Multiply the row of A by the column of B to get the row, column of product.
for (int inner = 0; inner < 2; inner++) {
product[row][col] += aMatrix[row][inner] * bMatrix[inner][col];
}
cout << product[row][col] << " ";
}
cout << "\n";
}
}
int main() {
// cout << bMatrix[3][2] << "\t";
// cout << aMatrix[3][2] << "\t";
MultiplyWithOutAMP();
getchar();
system("pause");
return 0;
}
Using pointer(Multiplier)
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int **mat1;
int **mat2;
int **result;
int row,col;
cout<<"Please enter row/col"<<endl;
cin>>row>>col;
mat1 = new int *[row];
mat2 = new int *[row];
result = new int *[row];
int k,i,j;
for (k=0; i<row; k++)
mat1[k] = new int[col];
mat2[k] = new int[col];
result[k] = new int[col];
for (i=0; i<row; i++)
for (j=0; j<col; j++)
for (j=0; j<row; j++)
for(i=0; i<col; i++)
result[i][k] += (mat1 [i][k] * mat2[k][j]);
cout<<setw(4)<<result[i][k];
#include <iomanip> // pointer Multiplier
#include <iostream>
using namespace std;
int main()
{
int **mat1;
int **mat2;
int **result;
int row,col;
cout<<"Please enter row/col"<<endl;
cin>>row>>col;
mat1 = new int *[row];
mat2 = new int *[row];
result = new int *[row];
int k,i,j;
for (k=0; k<row; k++)
mat1[k] = new int[col];
mat2[k] = new int[col];
result[k] = new int[col];
for (i=0; i<row; i++)
for (j=0; j<col; j++)
mat1[k][i] = k + i;
for (j=0; j<row; j++)
mat2[i][k] = j + k;
for(i=0; i<col; i++)
result[i][k] += (mat1 [i][k] * mat2[k][j]);
cout<<setw(4)<<result[i][k];
//adding Matrix
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10],m,n,i,j;
cout<<"Enter number of rows: ";
cin>>m;
cout<<"Enter number of coloumns: ";
cin>>n;
cout<<endl<<"Enter elements of matrix A: "<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"Enter element a"<<i+1<<j+1<<": ";
cin>>a[i][j];
}
}
cout<<endl<<"Enter elements of matrix B: "<<endl;
//Coding by: Snehil Khanor
//http://WapCPP.blogspot.com
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"Enter element b"<<i+1<<j+1<<": ";
cin>>b[i][j];
}
}
cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl<<endl;
}
cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl<<endl;
}
cout<<endl<<"Matrix A + Matrix B = Matrix C: "<<endl<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]+b[i][j]<<" ";
}
cout<<endl<<endl;
}
getch();
}
Adding matrix: c++ program to add two matrices
#include<iostream>
using namespace std;
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];
cout << "Enter the elements of second matrix\n";
for ( c = 0 ; c < m ;c++ )
for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
cout << "Sum of entered matrices:-\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";
cout << endl;
}
system("pause");
return 0;
}
A number is taken from the user number with reverse digits is displayed
#include<iostream>
using namespace std;
int main()
{
//clrscr();
long int n,rev=0,m;
cout<<"please enter a five digit no.: ";
cin>>n;
while(n>0)
//Coding by: Snehil Khanor
//http://WapCPP.blogspot.com
{
m=n%10;
rev=rev*10+m;
n=n/10;
}
cout<<rev;
//
system("pause");
return 0;
}
Address in memory
1: #include <iostream>
2:
3: int main()
4: {
5: short age[4];
6: age[0]=23;
7: age[1]=34;
8: age[2]=65;
9: age[3]=74;
10:
11: std::cout << age << std::endl;
12: return 0;
13: }
Character in Array
1: char letter[4];
2: letter[0]='M';
3: letter[1]='a';
4: letter[2]='r';
5: letter[3]='k';
6: ... *(letter+2) ...
• Question (4)
•
• Using the switch or if statement, write a C program that takes in an
integer numerical grade num_grade from the keyboard and
returns to the screen a letter_grade according to
• the following scale:
• letter_grade is A if num_grade ≥ 90
• letter_grade is B if 80 ≤ num_grade < 90
• letter_grade is C if 70 ≤ num_grade < 80
• letter_grade is D if 60 ≤ num_grade < 70
• letter_grade is F if num_grade < 60
• Check the correctness of your program and print down the outputs
of your program for a
• student with a score of 100 and another with a score of 65.
include <iostream>
using namespace std;
int main()
{
This would produce:
2nd member = 720.52
5th member = 6.28
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
cout << "2nd member = " << distance[1] << endl;
cout << "5th member = " << distance[4] << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
cout << "Distance 1: " << distance[0] << endl;
cout << "Distance 2: " << distance[1] << endl;
cout << "Distance 3: " << distance[2] << endl;
cout << "Distance 4: " << distance[3] << endl;
cout << "Distance 5: " << distance[4] << endl;
return 0;
}
•
This would produce:
•
•
•
•
•
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 468.78
Distance 5: 6.28
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
cout << "Distance 1: " << distance[0] << endl;
cout << "Distance 2: " << distance[1] << endl;
cout << "Distance 3: " << distance[2] << endl;
cout << "Distance 4: " << distance[3] << endl;
cout << "Distance 5: " << distance[4] << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
// We know that we need a constant number of elements
const int max = 10;
int number[max];
// We will calculate their sum
int sum = 0;
cout << "Please type 10 integers.\n";
for( int i = 0; i < max; i++ )
{
cout << "Number " << i + 1 << ": ";
cin >> number[i];
sum += number[i];
}
cout << "\n\nThe sum of these numbers is " << Sum << "\n\n";
return 0;
}
#include <iostream>
using namespace std;
void DisplayTheArray(double member[5]);
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
void DisplayTheArray(double member[5])
{
for(int i = 0; i < 5; ++i)
cout << "\nDistance " << i + 1 << ": " << member[i];
cout << endl;
}
Declaring and Initializing a 2-Dimensional Array
#include <iostream>
using namespace std;
int main()
{
// A 2-Dimensional array
double distance[2][4] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04, 364.55, 6234.12};
// Scan the array from the 3rd to the 7th member
cout << "Members of the array";
cout << "\nDistance [0][0]" << ": " << distance[0][0];
cout << "\nDistance [0][1]" << ": " << distance[0][1];
cout << "\nDistance [0][2]" << ": " << distance[0][2];
cout << "\nDistance [0][3]" << ": " << distance[0][3];
cout << "\nDistance [1][0]" << ": " << distance[1][0];
cout << "\nDistance [1][1]" << ": " << distance[1][1];
cout << "\nDistance [1][2]" << ": " << distance[1][2];
cout << "\nDistance [1][3]" << ": " << distance[1][3];
cout << endl;
return 0;
}
2-Dimensional array
#include <iostream>
using namespace std;
int main()
{
// A 2-Dimensional array
double distance[][4] = {
{ 44.14, 720.52, 96.08, 468.78 },
{ 6.28, 68.04, 364.55, 6234.12 }
};
// Scan the array from the 3rd to the 7th member
cout << "Members of the array";
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 4; ++j)
cout << "\nDistance [" << i << "][" << j << "]: " << distance[i][j];
cout << endl;
return 0;
}
Multidimensional Arrays
•
•
#include <iostream>
using namespace std;
•
void MultiplyWithOutAMP() {
•
•
•
int aMatrix[3][2] = {{1, 4}, {2, 5}, {3, 6}};
int bMatrix[2][3] = {{7, 8, 9}, {10, 11, 12}};
int product[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
•
•
•
•
•
•
•
•
•
•
•
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
// Multiply the row of A by the column of B to get the row, column of product.
for (int inner = 0; inner < 2; inner++) {
product[row][col] += aMatrix[row][inner] * bMatrix[inner][col];
}
std::cout << product[row][col] << " ";
}
std::cout << "\n";
}
}
•
•
•
•
•
main() {
MultiplyWithOutAMP();
getchar();
system("pause");
}
#include <iostream>
using namespace std;
#define MAX 50
void mulmatvec(int m, int n, double a[MAX][MAX], double x[MAX], double b[MAX]){
int i,j;
for(i = 0; i < m; i++){
b[i] = 0.;
for(j = 0; j < n; j++)
b[i] += a[i][j]*x[j];
}
return;
}
int main(){
int i,j,m,n;
double a[MAX][MAX], x[MAX], b[MAX];
cout << "Enter the number of rows in the matrix \n";
cin >> m;
cout << "Enter the number of columns in the matrix \n";
cin >> n;
cout << "Enter the matrix by rows\n";
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
cin >> a[i][j];
cout << "Enter the vector\n";
for(j = 0; j < n; j++)
cin >> x[j];
mulmatvec(m,n,a,x,b);
cout << "\nA*x = \n";
for(i = 0; i < m; i++)
cout << b[i] << "\n";
}
•
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
int i,j;
int arr[6][6];
for(i=0;i<=5;i++){
for(j=5;j>=0;j--){
if(i+j==5) arr[i][j]=0; 'diagonal
else if(i+j>5) arr[i][j]=-1; 'lower-right side
else arr[i][j]=1; 'upper-left side
}
}
for(i=0;i<=5;i++){
for(j=0;j<=5;j++)
cout<<arr[i][j]<<"\t";
cout<<endl;
}
getch();
return 0;
}
• Initializing array
• To assign values to the array, you can write those
values as below.
•
int i[]= {1, 2, 3, 4, 5};//The array got values
or
•
int i[5];
•
i[0]=1;
•
i[1]=2;
•
i[2]=3;
•
i[3]=4;
•
i[4]=5;
Adding two Matrix
#include<iostream>
using namespace std;
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];
cout << "Enter the elements of second matrix\n";
for ( c = 0 ; c < m ;c++ )
for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
cout << "Sum of entered matrices:-\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";
cout << endl;
}
return 0;
}
Matrix multiplication in C++
• Matrices can be used to perform transformations. For example, in
2D and 3D space. Transformations include, but are not limited
to, rotating, scaling and translating. Transformations can be
combined by multiplying the matrices with each other.
• For example, you might want to:
• scale an object with a factor of 2
• rotate it 45 degrees around the Y-axis
• translate it to (5,5,5)
• Each of the above steps can be represented with a four by four
matrix (four rows and four columns). Multiplication of matrices is
done by multiplying each row of the first matrix with each column
of the second matrix and summing up the results. The number of
columns of the first matrix needs to be equal to the number of rows
of the second matrix. For example (4 x 4) x (4 x 4) is allowed.
However, (4 x 3) x (4 x 3) is not allowed.
Matrix multiplication
[[1x1 + 2x0 + 3x0 + 4x0] = 1, [1x0 + 2x1 + 3x0 + 4x0] = 2, [1x0 + 2x0 + 3x1 + 4x0] = 3,
[1x0 + 2x0 + 3x0 + 4x1] = 4]]
Code
#include <iostream>
void MultiplyWithOutAMP() {
int aMatrix[3][2] = {{1, 4}, {2, 5}, {3, 6}};
int bMatrix[2][3] = {{7, 8, 9}, {10, 11, 12}};
int product[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
// Multiply the row of A by the column of B to get the row, column of product.
for (int inner = 0; inner < 2; inner++) {
product[row][col] += aMatrix[row][inner] * bMatrix[inner][col];
}
std::cout << product[row][col] << " ";
}
std::cout << "\n";
}
}
void main() {
MultiplyWithOutAMP();
getchar();
}
// C++ multiplication of the matrix x and matrix y and stores the result in matrix z
#include <iostream>
using namespace std;
#define m 3
#define c 2
#define n 4
int main(void)
{
int i, j, k;
// first matrix
int x[m][c] = {{1,2},{3,4},{5,6}};
// second matrix
int y[c][n] = {{7,8,9,10},{11,12,13,14}};
// for storing the matrix product result
int z[m][n];
for(i=0; i<m; i++)
for(j=0; j<n; j++)
{
z[i][j] = 0;
for(k=0; k<c; k++)
// same as z[i][j] = z[i][j] + x[i][k] * y[k][j];
z[i][j] += x[i][k] * y[k][j];
}
cout<<"\nMultiply matrix x and matrix y,";
cout<<"\nThen store the result in matrix z.";
cout<<"\nMatrix x is 3x2, and matrix y is 2x4,";
cout<<"\nso, the result, z should be matrix 3x4\n";
cout<<"\nThe matrix product is: \n";
for (i=0; i<m; i++)
{
cout<<"\n";
for(j=0; j<n; j++)
// display the result...
cout<<" "<<z[i][j];
}
cout<<endl;
return 0;
}
Output example:
Multiply matrix x and matrix y,
Then store the result in matrix z.
Matrix x is 3x2, and matrix y is 2x4,
so, the result, z should be matrix 3x4
The matrix product is:
29 32 35 38
65 72 79 86
101 112 123 134
Press any key to continue . . .