[关闭]
@chawuciren 2018-10-16T12:42:22.000000Z 字数 1426 阅读 651

Matrix

CSI


  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. void FreeMatrix(int** mat);//释放
  4. int** CreatMatrix(int row,int col); //建立
  5. int** Malloc(int row,int col);//申请空间
  6. void Free(int** a);
  7. int** CreatMatrix(int row,int col){
  8. int** matrix=(int **)malloc(sizeof(int*) * row);
  9. for(int i=0;i<row;i++){
  10. matrix[i]=(int *)malloc(sizeof(int) * col);
  11. }
  12. return matrix;
  13. }
  14. int** Malloc(int row,int col){
  15. int** ret=(int **)malloc(sizeof(int*) * row);
  16. int* p=(int *)malloc(sizeof(int)*row*col);
  17. int i=0;
  18. if(ret&&p){
  19. for(i=0;i<row;++i){
  20. ret[i]=(p+col*i);
  21. }
  22. }
  23. else{
  24. free(ret);
  25. free(p);
  26. return 0;
  27. }
  28. return ret;
  29. }
  30. void Free(int** a){
  31. free(a[0]);
  32. free(a);
  33. }
  34. void FreeMatrix(int** mat){
  35. free(mat[0]);
  36. free(mat);
  37. }
  38. int main(){
  39. int row=5,col=5;
  40. int** matrixa=CreatMatrix(row,col);//建立矩阵a
  41. int** matrixb=CreatMatrix(row,col);//建立矩阵b
  42. int** matrixc=CreatMatrix(row,col);//建立矩阵c
  43. for(int i=0;i<row;i++){ //输入第一个矩阵
  44. for(int j=0;j<row;j++){
  45. printf("input a[%d][%d]:\n",i,j);
  46. scanf("%d",&matrixa[i][j]);
  47. }
  48. }
  49. for(int i=0;i<row;i++){ //输入第二个矩阵
  50. for(int j=0;j<row;j++){
  51. printf("input b[%d][%d]:\n",i,j);
  52. scanf("%d",&matrixb[i][j]);
  53. }
  54. }
  55. for(int i=0;i<row;i++){ // 两个矩阵相乗等于c
  56. for(int j=0;j<row;j++){
  57. for(int k=0;k<row;k++){
  58. matrixc[i][j]=matrixa[i][k]*matrixb[k][j];
  59. }
  60. }
  61. }
  62. for(int j=0;j<row;j++) //输出
  63. printf("%d",matrixc[0][j]);
  64. for(int j=0;j<row;j++)
  65. printf("%d",matrixc[1][j]);
  66. for(int j=0;j<row;j++)
  67. printf("%d",matrixc[2][j]);
  68. for(int j=0;j<row;j++)
  69. printf("%d",matrixc[3][j]);
  70. for(int j=0;j<row;j++)
  71. printf("%d",matrixc[4][j]);
  72. FreeMatrix(matrixa);//释放
  73. FreeMatrix(matrixb);
  74. FreeMatrix(matrixc);
  75. return 0;
  76. }

在此输入正文

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注