数据结构C语言稀疏矩阵相乘
时间: 2023-11-26 21:46:32 浏览: 59
稀疏矩阵相乘是指两个稀疏矩阵相乘的操作。稀疏矩阵是指矩阵中大部分元素为0的矩阵。相比于普通矩阵,稀疏矩阵在存储和计算上都有很大的优势。下面是C语言实现稀疏矩阵相乘的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int row;
int col;
int value;
} Triple;
typedef struct {
Triple data[MAX_SIZE + 1];
int row_num;
int col_num;
int element_num;
} TSMatrix;
void CreateMatrix(TSMatrix *matrix) {
printf("请输入矩阵的行数和列数:");
scanf("%d %d", &matrix->row_num, &matrix->col_num);
printf("请输入矩阵的非零元素个数:");
scanf("%d", &matrix->element_num);
printf("请输入矩阵的三元组:\n");
for (int i = 1; i <= matrix->element_num; i++) {
scanf("%d %d %d", &matrix->data[i].row, &matrix->data[i].col, &matrix->data[i].value);
}
}
void PrintMatrix(TSMatrix matrix) {
printf("矩阵的三元组表示如下:\n");
for (int i = 1; i <= matrix.element_num; i++) {
printf("%d %d %d\n", matrix.data[i].row, matrix.data[i].col, matrix.data[i].value);
}
}
void MatrixMultiply(TSMatrix matrix1, TSMatrix matrix2, TSMatrix *result) {
if (matrix1.col_num != matrix2.row_num) {
printf("矩阵无法相乘!\n");
return;
}
int *temp = (int *)malloc((matrix2.col_num + 1) * sizeof(int));
int k = 1;
result->row_num = matrix1.row_num;
result->col_num = matrix2.col
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)