计算方法 实验一 算法设计基础

一、问题提出

编程实现以下矩阵相乘:
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;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值