gpt4 book ai didi

c - 稀疏矩阵函数

转载 作者:行者123 更新时间:2023-11-30 21:09:51 25 4
gpt4 key购买 nike

这是我的作业。我可以返回 void,但我不知道如何使用 int 以及如何检查何时出现问题并返回 -1;

ADT SMatrix 是:

  1. 标题

    整数的稀疏矩阵(具有维度行 x 列)。

功能:

int SMTX_read (SMatrixType A)

::= read in a matrix from stdin and stores the matrix in A.
::= returns SMTX_ERROR if something went wrong
--------/* For this project, the input format will be
Line 1: two integers, rows & cols, dimension of the matrix Line 2 to rows+1: contains cols number of integers
Should take the input and convert to your proper ADT format */


int SMTX_print(SMatrixType A)

::= print the sparse matrix A in the following format
/* Line 1: print “Rows = ??, Cols = ??, # of non-zero entries = ??” Line 2 ~ ??: print “< Ri, Ci, entry-value>,” one 3-tuple per line */



int SMTX_add (SMatrixType A, B, C)

::= C <= A + B
::= returns SMTX_ERROR if something went wrong

int SMTX_subtract (SMatrixType A, B, C)

::= C <= A - B
::= returns SMTX_ERROR if something went wrong

int SMTX_transpose (SMatrixType A, B) ::= B <= AT

::= returns SMTX_ERROR if something went wrong

int SMTX_multiply (SMatrixType A, B, C)



::= C <= A x B
::= returns SMTX_ERROR if something went wrong /* please use the quick

algorithm given in lecture */

数据输入:

define SMTX_ERROR -1
define MAX_SMTX_SIZE 100
typedef struct SMatrix {
. ..
} SMatrixType

最佳答案

错误处理旨在通过返回值执行。例如,当期望根本没有错误时,Void 可能是打印函数。读功能和其他操作应检查输入: e. G。矩阵具有相应的维度,否则可能会出现其他问题并且结果将不符合预期。

int SMTX_read (SMatrixType A)
{
int err = 0;
/* reading input */
if( outcome != expected ) { err = SMTX_ERROR; }
/* .. */
return err;
}

或者另一个例子:

int division( int dividend, int divisor, int * quotient_ptr )
{
int err = -1;
if( 0 != divisor ) { err = 0; *quotient_ptr = dividend / divisor; }
return err;
}

关于c - 稀疏矩阵函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33118849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com