用C++设计矩阵类Matrix,矩阵mxn,实现指定m,n参数的函数SetVal(要分配数组空间),实现按位置(row,col)赋值函数Val,实现求转置函数 Transpose()进行行列对换
时间: 2023-05-12 18:03:53 浏览: 43
好的,我可以回答这个问题。以下是Matrix类的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int m;
int n;
double **data;
} Matrix;
Matrix *create_matrix(int m, int n) {
Matrix *mat = (Matrix *)malloc(sizeof(Matrix));
mat->m = m;
mat->n = n;
mat->data = (double **)malloc(sizeof(double *) * m);
for (int i = 0; i < m; i++) {
mat->data[i] = (double *)malloc(sizeof(double) * n);
}
return mat;
}
void set_val(Matrix *mat, int m, int n) {
mat->m = m;
mat->n = n;
mat->data = (double **)malloc(sizeof(double *) * m);
for (int i = 0; i < m; i++) {
mat->data[i] = (double *)malloc(sizeof(double) * n);
}
}
void val(Matrix *mat, int row, int col, double value) {
mat->data[row][col] = value;
}
Matrix *transpose(Matrix *mat) {
Matrix *trans = create_matrix(mat->n, mat->m);
for (int i = 0; i < mat->m; i++) {
for (int j = 0; j < mat->n; j++) {
trans->data[j][i] = mat->data[i][j];
}
}
return trans;
}
```
你可以使用create_matrix函数创建一个mxn的矩阵,使用set_val函数重新设置矩阵的大小,使用val函数按位置赋值,使用transpose函数求转置矩阵。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)