[c++]二维矩阵存储及基本操作demo

C++实现二维矩阵存储及基本操作demo

/* 调试过程的低级错误:1.循环ij问题;2.指针未经初始化 */

#include
#include
#include
#include
#include
using namespace std;

class Matrix {
public:
	int row_num;
	int col_num;
	double **p = NULL;
};

Matrix* sampleMatrix = new Matrix;

void matrixInitialization(Matrix* matrix, int row, int col) {
	matrix->row_num = row;
	matrix->col_num = col;
	matrix->p = new double*[matrix->row_num];
	for (int i = 0; i < matrix->row_num; i++) {
		matrix->p[i] = new double[matrix->col_num];
	}
}

void ZeroMatrix(Matrix* matrix) {
	for (int i = 0; i < matrix->row_num; i++) {
		for (int j = 0; j < matrix->col_num; j++) {
			matrix->p[i][j] = 0;
		}
	}
}

void ValueMatrix(Matrix* matrix, int value) {
	for (int i = 0; i < matrix->row_num; i++) {
		for (int j = 0; j < matrix->col_num; j++) {
			matrix->p[i][j] = value;
		}
	}
}

void matrixDelete(Matrix* matrix) {
	for (int i = 0; i < matrix->row_num; i++) {
		delete[] matrix->p[i];
	}
	delete[] matrix->p;
}

int main(){
    int rows = 100;
    int cols = 184;
    matrixInitialization(sampleMatrix, rows, cols);

    ValueMatrix(sampleMatrix, 6);
    for(int i=0;irow_num;i++){
        for(int j=0;jcol_num;j++){
            cout<p[i][j]<<" ";
        }
        cout<

 

 

你可能感兴趣的:(c++)