一、问题提出
编程实现以下矩阵相乘:
3
4
2
5
7
3
8
2
1
3
3
2.9
6
2
4
8
5
4
10
5
4
二、要求
1,编制一个程序进行运算,最后打印出结果。
2,将编程结果与手工演算结果进行比较。
3,原始数据使用ASCII码数据文件的形式访问。
4,最好能将程序结果输出到ASCII码数据文件。
三、目的和意义
1,通过实验进一步熟悉矩阵运算和多重循环编程;
2,熟悉数据文件访问操作;
实验代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=100;
const int row1=4;
const int col1=3;
const int row2=3;
const int col2=3;
int main()
{
FILE *f;
double a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];
f=fopen("Array.dat","r");
memset(c,0,sizeof(c));
for(int i=1; i<=row1; i++)
for(int j=1; j<=col1; j++)
fscanf(f,"%lf",&a[i][j]);
for(int i=1; i<=row2; i++)
for(int j=1; j<=col2; j++)
fscanf(f,"%lf",&b[i][j]);
fclose(f);
for(int i=1; i<=row1; i++)
for(int j=1; j<=col1; j++)
for(int k=1; k<=col2; k++)
{
c[i][j]+=(a[i][k]*b[k][j]);
}
f=fopen("Array_out.dat","w");
for(int i=1; i<=row1; i++)
{
for(int j=1; j<=col2; j++)
{
fprintf(f,"%10.2lf ",c[i][j]);
}
fprintf(f,"\n");
}
fclose(f);
return 0;
}