返回矩阵函数中的矩阵总和
Sum of matrices in a returning matrix function
下面是一个可编译的程序,大部分是Craig Estey帮我解决了另一个问题。我只是使用它,以便人们可以测试程序,但我面临的问题是对两个矩阵求和的函数。准确地说,函数
Matrix* sum(Matrix* mat1, Matrix* mat2){
int row, col, k, l;
Matrix *mat3;
mat3 = allocateMemory(mat1->row, mat1->col);
if((mat1->row == mat2->row)&&(mat1->col == mat2->col)){ //verify if the two matrices have the same order
for (row = 0; row < mat1->row; row++){
for (col = 0; col < mat1->col; col++){
k = row * mat1->col + col;//create index for matrix 1 element
l = row * mat2->col + col;//create index for matrix 2 element
mat3->a[k] = mat1->a[k] + mat2->a[l]; //sum elements and store in mat3
}
}
return mat3;
}
}
可编译的 C 程序
#include <stdio.h>
#include <stdlib.h>
typedef struct matrix {
int row;
int col;
int *a;
} Matrix;
Matrix *allocateMemory(int row, int col)
{
Matrix *mat = malloc(sizeof(*mat));
if (mat == NULL) {
perror("malloc");
exit(1);
}
mat->row = row;
mat->col = col;
mat->a = calloc(row * col, sizeof(*mat->a));
if (mat->a == NULL) {
perror("calloc");
exit(1);
}
return mat;
}
void printMatrix(Matrix *mat)
{
int row, col, off;
for (row = 0; row < mat->row; row++) {
for (col = 0; col < mat->col; col++) {
off = (row * mat->col) + col;
printf("%d ", mat->a[off]);
}
printf("\n");
}
}
void matrix_fill(Matrix *mat)
{
int row, col, off;
int val = 1;
for (row = 0; row < mat->row; row++) {
for (col = 0; col < mat->col; col++) {
off = (row * mat->col) + col;
mat->a[off] = val++;
}
}
}
Matrix* sum(Matrix* mat1, Matrix* mat2){
int row, col, k, l;
Matrix *mat3;
mat3 = allocateMemory(mat1->row, mat1->col);
if((mat1->row == mat2->row)&&(mat1->col == mat2->col)){
for (row = 0; row < mat1->row; row++){
for (col = 0; col < mat1->col; col++){
k = row * mat1->col + col;
l = row * mat2->col + col;
mat3->a[k] = mat1->a[k] + mat2->a[l];
}
}
return mat3;
}
}
int main(void){
Matrix *m1 = allocateMemory(3,3);
Matrix *m2 = allocateMemory(3,3);
Matrix *m3 = allocateMemory(3,3);
matrix_fill(m1);
matrix_fill(m2);
printf("\nMatrix 1\n");
printMatrix(m1);
printf("\nMatrix 2\n");
printMatrix(m2);
Matrix* sum(Matrix* m1, Matrix* m2);
printf("\nSum of matrices m1 and m2\n");
printMatrix(m3);
return 0;
}
The sum function is supposed to return the result of the sum of two given matrices, but it is not working, just seems to return NULL value. But I couldn't understand why it is not working.
该函数具有未定义的行为和内存泄漏,因为在 if 语句中的条件
的情况下,它return什么也做不了
if((mat1->row == mat2->row)&&(mat1->col == mat2->col)){
将评估为 false。
函数可以通过以下方式声明和定义
Matrix * sum( const Matrix *mat1, const Matrix *mat2 )
{
Matrix *mat3 = NULL;
if ( ( mat1->row == mat2->row ) && ( mat1->col == mat2->col ) )
{
mat3 = allocateMemory( mat1->row, mat1->col );
if ( mat3 != NULL )
{
for ( int row = 0; row < mat1->row; row++ )
{
for ( int col = 0; col < mat1->col; col++ )
{
int k = row * mat1->col + col;
mat3->a[k] = mat1->a[k] + mat2->a[k];
}
}
}
}
return mat3;
}
在main函数中分配
Matrix *m3 = allocateMemory(3,3);
是多余的,如果您像这样调用该函数(也就是说,如果您按要求调用该函数),也会导致内存泄漏
m3 = sum( m1, m2 );
并且这条记录在main
Matrix* sum(Matrix* m1, Matrix* m2);
是一个函数声明。这不是函数调用。
你应该像上面这样写
Matrix *m3 = NULL;
//...
m3 = sum( m1, m2 );
下面是一个可编译的程序,大部分是Craig Estey帮我解决了另一个问题。我只是使用它,以便人们可以测试程序,但我面临的问题是对两个矩阵求和的函数。准确地说,函数
Matrix* sum(Matrix* mat1, Matrix* mat2){
int row, col, k, l;
Matrix *mat3;
mat3 = allocateMemory(mat1->row, mat1->col);
if((mat1->row == mat2->row)&&(mat1->col == mat2->col)){ //verify if the two matrices have the same order
for (row = 0; row < mat1->row; row++){
for (col = 0; col < mat1->col; col++){
k = row * mat1->col + col;//create index for matrix 1 element
l = row * mat2->col + col;//create index for matrix 2 element
mat3->a[k] = mat1->a[k] + mat2->a[l]; //sum elements and store in mat3
}
}
return mat3;
}
}
可编译的 C 程序
#include <stdio.h>
#include <stdlib.h>
typedef struct matrix {
int row;
int col;
int *a;
} Matrix;
Matrix *allocateMemory(int row, int col)
{
Matrix *mat = malloc(sizeof(*mat));
if (mat == NULL) {
perror("malloc");
exit(1);
}
mat->row = row;
mat->col = col;
mat->a = calloc(row * col, sizeof(*mat->a));
if (mat->a == NULL) {
perror("calloc");
exit(1);
}
return mat;
}
void printMatrix(Matrix *mat)
{
int row, col, off;
for (row = 0; row < mat->row; row++) {
for (col = 0; col < mat->col; col++) {
off = (row * mat->col) + col;
printf("%d ", mat->a[off]);
}
printf("\n");
}
}
void matrix_fill(Matrix *mat)
{
int row, col, off;
int val = 1;
for (row = 0; row < mat->row; row++) {
for (col = 0; col < mat->col; col++) {
off = (row * mat->col) + col;
mat->a[off] = val++;
}
}
}
Matrix* sum(Matrix* mat1, Matrix* mat2){
int row, col, k, l;
Matrix *mat3;
mat3 = allocateMemory(mat1->row, mat1->col);
if((mat1->row == mat2->row)&&(mat1->col == mat2->col)){
for (row = 0; row < mat1->row; row++){
for (col = 0; col < mat1->col; col++){
k = row * mat1->col + col;
l = row * mat2->col + col;
mat3->a[k] = mat1->a[k] + mat2->a[l];
}
}
return mat3;
}
}
int main(void){
Matrix *m1 = allocateMemory(3,3);
Matrix *m2 = allocateMemory(3,3);
Matrix *m3 = allocateMemory(3,3);
matrix_fill(m1);
matrix_fill(m2);
printf("\nMatrix 1\n");
printMatrix(m1);
printf("\nMatrix 2\n");
printMatrix(m2);
Matrix* sum(Matrix* m1, Matrix* m2);
printf("\nSum of matrices m1 and m2\n");
printMatrix(m3);
return 0;
}
The sum function is supposed to return the result of the sum of two given matrices, but it is not working, just seems to return NULL value. But I couldn't understand why it is not working.
该函数具有未定义的行为和内存泄漏,因为在 if 语句中的条件
的情况下,它return什么也做不了if((mat1->row == mat2->row)&&(mat1->col == mat2->col)){
将评估为 false。
函数可以通过以下方式声明和定义
Matrix * sum( const Matrix *mat1, const Matrix *mat2 )
{
Matrix *mat3 = NULL;
if ( ( mat1->row == mat2->row ) && ( mat1->col == mat2->col ) )
{
mat3 = allocateMemory( mat1->row, mat1->col );
if ( mat3 != NULL )
{
for ( int row = 0; row < mat1->row; row++ )
{
for ( int col = 0; col < mat1->col; col++ )
{
int k = row * mat1->col + col;
mat3->a[k] = mat1->a[k] + mat2->a[k];
}
}
}
}
return mat3;
}
在main函数中分配
Matrix *m3 = allocateMemory(3,3);
是多余的,如果您像这样调用该函数(也就是说,如果您按要求调用该函数),也会导致内存泄漏
m3 = sum( m1, m2 );
并且这条记录在main
Matrix* sum(Matrix* m1, Matrix* m2);
是一个函数声明。这不是函数调用。
你应该像上面这样写
Matrix *m3 = NULL;
//...
m3 = sum( m1, m2 );