c语言利用指针数组实现矩阵运算,学习二维动态数组指针做矩阵运算的方法

本文介绍了使用C语言通过指针数组实现二维动态矩阵的分配、释放、赋值、加减乘转置等操作。提供完整的程序代码,包括矩阵初始化、输出、乘以常数及矩阵的加、减、乘、转置等功能。
摘要由CSDN通过智能技术生成

本文分享了利用二维动态数组指针做矩阵运算的实现代码。

1. 头文件

// juzhen 2.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "stdlib.h"

#include "windows.h"

#define OK 0

#define NG -1

typedef struct mat

{

int nRow; /* 行数 */

int nCol; /* 列数 */

int* pData; /* 指向矩??体的指? */

}MAT;

2. 程序代码

#include "stdafx.h"

#include "Matrix_cal.h"

/* Entity and initial matrix of the application matrix function */

int MATAlloc(MAT *pMat, int nRow, int nCol)

{

pMat->pData = (int *) malloc (nRow * nCol * sizeof(int) );

if(NULL == pMat->pData)

{

printf("Memary is error!\n");

return NG;

}

for(int i=0; i

{

for(int j=0; j

{

*(pMat->pData + i*nCol + j)=0;

}

}

pMat->nRow = nRow;

pMat->nCol = nCol;

return OK;

}

/* Release the memory space and reset the matrix data function */

void MATFree(MAT* pMat)

{

free(pMat->pData);

pMat->pData = NULL;

pMat->nRow = 0;

pMat->nCol = 0;

}

/* Import of matrix function */

int MATAssign (MAT* pMat1, const MAT* pMat2)

{

MATAlloc(pMat1, pMat2->nRow, pMat2->nCol);

for(int i=0; i < pMat1->nRow; ++i)

{

for(int j=0; j < pMat1->nCol; ++j)

{

*(pMat1->pData + i * pMat1->nCol + j) = *(pMat2->pData + i * pMat1->nCol + j);

}

}

return OK;

}

/* Matrix sum function */

int MATAdd(const MAT* pMat1, const MAT* pMat2, MAT* pMat3)

{

MATAlloc(pMat3, pMat1->nRow, pMat1->nCol);

if((pMat1->nRow == pMat2->nRow) && (pMat1->nCol == pMat2->nCol))

{

for(int i=0; inRow; ++i)

{

for(int j=0; jnCol; ++j)

{

*(pMat3->pData +

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值