Guest User

Untitled

a guest
Jan 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. void MatrixMulplication(const MATRIXTYPE *first, const MATRIXTYPE *second, MATRIXTYPE *result, int fR, int mid, int lC){
  2. int sum=0,i=0,j=0,k=0,flagK,flagJ=0;
  3.  
  4. for(k=0;k<lC;k++){
  5. flagK=k;
  6. for(i=0,sum=0; i<fR*mid ;i++){
  7. sum+=first[i]*second[k];
  8. k+=lC;
  9. if( (i%mid) == mid-1 ){ //first 換行
  10. result[j]=sum;
  11. if( (j%fR) == fR-1 ){
  12. flagJ+=1;
  13. j=flagJ; //result 換行
  14. break;
  15. }
  16. j+=lC;
  17. k=flagK;
  18. }
  19. }
  20. }
  21. }
  22. int main(){
  23. MATRIXTYPE *arrayFirst;
  24. MATRIXTYPE *arraySecond;
  25. MATRIXTYPE *arrayResult;
  26. int *firstMatrix;
  27. int *secondMatrix;
  28. int *resultMatrix;
  29. int i,m,n,p;
  30.  
  31. while( scanf("%d%d%d",&m,&n,&p)==3 ){
  32.  
  33. /*CreatMatrix*/
  34.  
  35. int *arrayFirst=(int*)malloc(m*sizeof(int));
  36. for(i = 0; i < m; i++)
  37. arrayFirst[i]=(int)malloc(n*sizeof(int)); // Assign some value to array1[i][j]…
  38. firstMatrix=arrayFirst;
  39.  
  40. int *arraySecond=(int*)malloc(n*sizeof(int));
  41. for(i = 0; i < n; i++)
  42. arraySecond[i]=(int)malloc(p*sizeof(int)); // Assign some value to array1[i][j]…
  43. secondMatrix=arraySecond;
  44.  
  45. int *arrayResult=(int*)malloc(m*sizeof(int));
  46. for(i = 0; i < m; i++)
  47. arrayResult[i]=(int)malloc(p*sizeof(int)); // Assign some value to array1[i][j]…
  48. resultMatrix=arrayResult;
  49.  
  50. /*inputMatrix*/
  51. for(i=0;i<m*n;i++){
  52. scanf("%d",firstMatrix[i]);
  53. }
  54. for(i=0;i<n*p;i++){
  55. scanf("%d",secondMatrix[i]);
  56. }
  57. /*MatrixMulplication*/
  58. MatrixMulplication(firstMatrix, secondMatrix, resultMatrix, m, n, p);
  59.  
  60. /*printMatrix*/
  61. for(i=0;i<m*p;i++){
  62. printf("%d%c",resultMatrix[i],( (i%p)==p-1 ) ? '\n':' ' );
  63. }
  64.  
  65. /*freeMatrix*/
  66. free(firstMatrix);
  67. free(secondMatrix);
  68. free(resultMatrix);
  69.  
  70. }
  71.  
  72. return 0;
  73. }
Add Comment
Please, Sign In to add comment