用三元组存储下面矩阵并实现矩阵转置,要求:输出转置前后矩阵的三元组顺序表。1 0 0 0 8 0 0 0 0 4 3 0 0 0 0

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1000

typedef struct 
{
    int row, col;
    int e;
} Triple;

typedef struct
{
    Triple data[MAXSIZE + 1];
    int m, n, len;
}TSMatrix;

void TransposeTSMatrix(TSMatrix A, TSMatrix B)
{
    int i, j, k;
    B.m = A.n;
    B.n = A.m;
    B.len = A.len;
    printf("转置运算后:\n");
    if(B.len > 0)
    {
        j = 1;
        for(k = 1; k <= A.n; k++)
        {
            for(i = 1; i <= A.len; i++)
            {
                if(A.data[i].col == k)
                {
                    B.data[j].row = A.data[i].col;
                    B.data[j].col = A.data[i].row;
                    B.data[j].e = A.data[i].e;
                    printf("%d  %d  %d  \n", B.data[j].row, B.data[j].col, B.data[j].e);
                    j++;
                }
            }
        }
    }

}

int main()
{
    int a[3][5] = { {1, 0, 0, 0, 8},{0, 0, 0, 0, 4},{3, 0, 0, 0, 0} };
    int i, j, k;
    TSMatrix tsm;
    TSMatrix TSM;
    tsm.m = 3;
    tsm.n = 5;
    tsm.len = 0;
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 5; j++)
        {
            if (a[i][j] != 0)
            {
                Triple t;
                t.row = i+1;
                t.col = j+1;
                t.e = a[i][j];
                tsm.data[tsm.len+1] = t;
                tsm.len++;
            }
        }
    }
    for (i = 1; i <= tsm.len; i++)
    {
        printf("%d  %d  %d  \n", tsm.data[i].row, tsm.data[i].col, tsm.data[i].e);
    }
    TransposeTSMatrix(tsm, TSM);
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值