Cij = sum(Aik*Bkj) This is matrixmultiplication. Written out long hand, it looks
ID: 3615487 • Letter: C
Question
Cij = sum(Aik*Bkj) This is matrixmultiplication. Written out long hand, it looks like this(I’m not using C++ array locations here – Ididn’t start with zero, I started with 1).
C11 = A11*B11 + A12*B21 +A13*B31 Array C is thenew array.
C21 = A21*B11 + A22*B21 + A23*B31 Array A is the (5X3) array.
C31 = A31*B11 + A32*B21 + A33*B31 Array B is the (3X1) array.
C41 = A41*B11 + A42*B21 + A43*B31
C51 = A51*B11 + A52*B21 + A53*B31
Test your program by filling the two dimensional array,row by row using loops, with the even numbers 2 through30. Fill the one dimensional array with the numbers 1, 2, and3. You can hard code these values into your program ratherthan use user input. Output each array as it looks (ie. The5X3 array should be printed as a 5X3, etc.)
Explanation / Answer
#include<iostream.h> #include<conio.h> #include<stdlib.h> using namespace std; int main() { int a[5][3],b[3][1],c[5][1]; short i,k; clrscr(); int count=2; for(i=0;i<5;i++) { for(k=0;k<3;k++) { a[i][k]=count; count++; } } count=1; for(i=0;i<3;i++) { b[i][0]=count; count++; }cout<<" Array A: "; for(i=0;i<5;i++) { cout<<" "; for(k=0;k<3;k++) { cout<<a[i][k]<<""; } cout<<endl; }
cout<<" Array B: "; for(i=0;i<3;i++) cout<<b[i][0]<<""; cout<<endl;
for(i=0;i<5;i++) { for(k=0;k<3;k++) { for(short v=0;v<1;v++) c[i][v]=c[i][k]+(a[i][v]*b[v][k]); } }
cout<<" Array c: "; for(i=0;i<5;i++) { cout<<" "; for(k=0;k<1;k++) { cout<<c[i][k]<<" "; } cout<<endl; } system("pause); return 0; }
Hope you will find it helpfull
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.