数组及其应用.

数组及其应用

一、实验目的

1、掌握数组的声明,数组元素的输入,输出和引用;
2、应用数据解决与数组有关的常用算法;

二、实验要求

1、定义以三元组表示的矩阵;
2、要求从键盘输入两个矩阵,并完成该某个矩阵的转置,两个矩阵的加法及减法;
3、实验结果以矩阵的形式输出。

三、实验环境

硬件设备:微型计算机系统
软件环境:操作系统Windows,开发工具VC6.0或VS

四、测试数据

输入:从键盘循环输入整数,建立矩阵。
输出:将转置,两矩阵相加、相减的结果输出。

五、程序代码

#include <iostream>
using namespace std;
constexpr auto MAX = 100;
struct matrix {
    int value;
    int i, j;
};
void show(matrix A[], int row, int column) {
    int count = 0;
    for (int it = 1; it <= row; it++) {
        for (int jt = 1; jt <= column; jt++) {
            if (A[count].j == jt && A[count].i == it) {
                printf("%5d", A[count].value);
                count++;
            }
            else
                printf("%5d", 0);
        }
        cout << endl;
    }
}
void input(matrix A[], int max, int column) {
    int buffer, count = 0;;
    for (int i = 1, j = 1, num = 0; num < max; num++) {
        cin >> buffer;
        if (buffer != 0) {
            A[count].value = buffer;
            A[count].j = j;
            A[count].i = i;
            count++;
        }
        j++;
        if (j > column) {
            j = 1;
            i++;
        }
    }
}
void Create(matrix A[], int& row, int& column, int& max, char a) {
    cout << "...Creating matrix " << a << "..." << endl;
    fflush(stdin);
    cout << "Enter the number of rows and columns in the matrix (note the order of entries and separate them with spaces):";
    cin >> row >> column;
    max = row * column;
    if (max > MAX) {
        cout << "The size of the matrix is out of the default range. Please change the default size of the array or re-enter a smaller matrix!" << endl;
        exit(0);
    }
    input(A, max, column);
}
//矩阵转置
void Transposition(matrix A[], int row, int column, int max) {
    int num = row;
    row = column;
    column = num;
    num = 0;
    matrix B[MAX];
    for (int i = 0; i <= row; ++i)
        for (int j = 0; j <= max; ++j) {
            if (A[j].j == i) {
                B[num].value = A[j].value;
                B[num].i = A[j].j;
                B[num].j = A[j].i;
                ++num;
            }
        }
    show(B, row, column);
    num = row;
    row = column;
    column = num;
}
//矩阵加法
void addition(matrix A[], matrix B[], int row, int column) {
    matrix C[MAX];
    int a = 0, b = 0, c = 0;
    for (int i = 0;i <= row;++i)
        for (int j = 0; j <= column; ++j) {
            if (A[a].i == i && A[a].j == j) {
                C[c] = A[a];
                if (B[b].i == i && B[b].j == j) {
                    C[c].value = A[a].value + B[b].value;
                    b++;
                }
                a++;
                c++;
            }
            else if (B[b].i == i && B[b].j == j) {
                C[c] = B[b];
                C[c].value = C[c].value;
                b++;
                c++;
            }
        }
    show(C, row, column);
}
//矩阵减法
void subtraction(matrix A[], matrix B[], int row, int column) {
    matrix C[MAX];
    int a = 0, b = 0, c = 0;
    for (int i = 0; i <= row; ++i)
        for (int j = 0; j <= column; ++j) {
            if (A[a].i == i && A[a].j == j) {
                C[c] = A[a];
                if (B[b].i == i && B[b].j == j) {
                    C[c].value = A[a].value - B[b].value;
                    b++;
                }
                a++;
                c++;
            }
            else if (B[b].i == i && B[b].j == j) {
                C[c] = B[b];
                C[c].value = -C[c].value;
                b++;
                c++;
            }
        }
    show(C, row, column);
}
int main()
{
    int Arow, Acolumn, Amax, Brow, Bcolumn, Bmax;
    matrix A[MAX];
    Create(A, Arow, Acolumn, Amax, 'A');
    matrix B[MAX];
    Create(B, Brow, Bcolumn, Bmax, 'B');
    cout << "...Matrix A..." << endl;
    show(A, Arow, Acolumn);
    cout << "...Matrix B..." << endl;
    show(B, Brow, Bcolumn);
    while (1) {
        cout << "Please enter the matrix you want to transpose:";
        fflush(stdin);
        char choice;
        cin >> choice;
        if (choice == 'A') {
            cout << "...Matrix A transposition..." << endl;
            Transposition(A, Arow, Acolumn, Amax);
            break;
        }
        else if (choice == 'B') {
            cout << "...Matrix B transposition..." << endl;
            Transposition(B, Brow, Bcolumn, Bmax);
            break;
        }
        else
            cout << "Please enter the correct option" << endl;
    }
    cout << "...Matrix A plus matrix B..." << endl;
    if (Arow != Brow || Acolumn != Bcolumn)
        cout << "Different matrix types, unable to calculate" << endl;
    else
        addition(A, B, Arow, Acolumn);
    cout << "...Matrix A minus matrix B..." << endl;
    if (Arow != Brow || Acolumn != Bcolumn)
        cout << "Different matrix types, unable to calculate" << endl;
    else
        subtraction(A, B, Arow, Acolumn);
}

六、实验结果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值