相关知识
A的转置矩阵B满足B(i,j)=A(j,i)
编程要求
先把你已经通过的第一关的SM_SetAt代码复制过来,然后编写本关的代码
SparseMatrix* SM_Trans(SparseMatrix *A)。
在这个代码里,你需要自己创建一个稀疏矩阵A的转置矩阵,并返回。,
测试说明
平台会对你编写的代码进行测试:
测试输入:1 2 0.3;
预期输出:"correct!\n"
开始
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "SparseMatrix.h"
SparseMatrix* SM_Create(int rows, int cols, int maxnodes)
// 创建一个稀疏矩阵。
{
SparseMatrix* A=(SparseMatrix*)malloc(sizeof(SparseMatrix));
A->nodes=SL_Create(maxnodes); //给三元组表分配内存
A->Rows=rows;
A->Cols=cols;
A->maxnodes=maxnodes;
return A;
}
void SM_Free(SparseMatrix* A)
// 释放/删除 稀疏矩阵。
{
SL_Free(A->nodes);
free(A);
}
int SM_Length(SparseMatrix* A)
// 获取长度。
{
return SL_Length(A->nodes);
}
//设计这个小函数很好用,给读和写矩阵元素带来方便
int SM_Loc(SparseMatrix* A, int row, int col ){
if(row<0||row>=A->Rows||col<0||col>=A->Cols) {
return -2; // -2表示坐标非法
}
else {

最低0.47元/天 解锁文章
1887

被折叠的 条评论
为什么被折叠?



