Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matrix 使用malloc进行矩阵乘法,无需用户输入_Matrix_Malloc_Multiplication - Fatal编程技术网

Matrix 使用malloc进行矩阵乘法,无需用户输入

Matrix 使用malloc进行矩阵乘法,无需用户输入,matrix,malloc,multiplication,Matrix,Malloc,Multiplication,我试图使用Malloc函数来动态分配内存,但我也希望为操作指定数据条目,而不是接受用户输入。 我在这里找到了这段代码,它工作得很好,但我正在处理一个大型数据集,并且不接受用户输入,因此我想继续使用MALLOC,但也要定义数据集 喜欢而不是跟随 //Input Matrix1 for (i = 0; i < r1; i++) for (j = 0; j < c1; j++) scanf_s("%d", &mat1[i][j]);

我试图使用Malloc函数来动态分配内存,但我也希望为操作指定数据条目,而不是接受用户输入。 我在这里找到了这段代码,它工作得很好,但我正在处理一个大型数据集,并且不接受用户输入,因此我想继续使用MALLOC,但也要定义数据集

喜欢而不是跟随

//Input Matrix1
    for (i = 0; i < r1; i++)
        for (j = 0; j < c1; j++)
           scanf_s("%d", &mat1[i][j]); 

请指定输入数据的格式。它是csv文件吗

b
的大小固定时(即编译时已知),您只能以
intb[4]={1,2,3,4}
的格式指定数据。但如果所有矩阵在编译时都是已知的,为什么还要麻烦进行动态分配呢

此外,我还稍微整理了一下您的代码:

#include <stdio.h>                                                                                                                                            
#include <stdlib.h>                                                                                                                                           
#include <string.h>                                                                                                                                           

#define index(x, y, r) (x+r*y)                                                                                                                                

struct matrix {
        int cols;
        int rows;
        double * data;
};

void print_mat(struct matrix * mat) {
        int i, j;
        for (i = 0; i < mat->rows; i++) {
                for (j = 0; j < mat->cols; j++) {
                        printf("%f \t", mat->data[index(i, j, mat->rows)]);
                }
                printf("\n");
        }
}

int mat_alloc(struct matrix *mat) {
        mat->data = (double*)malloc(mat->rows*mat->cols*sizeof(double));
}

int read_mat(struct matrix *mat) {
        int i, j;
        for (i = 0; i < mat->rows; i++) {
                for (j = 0; j < mat->cols; j++) {
                        scanf("%lf", &(mat->data[index(i, j, mat->rows)]));
                }
        }
}

int multiply(struct matrix * a, struct matrix * b, struct matrix * res) {
        if(a->cols != b->rows){
                printf("Matrix dimensions do not match!\n");
                return 0;
        }
        res->rows = a->rows;
        res->cols = b->cols;
        mat_alloc(res);
        memset(res->data, 0, res->cols*res->rows*sizeof(double));
        int i, j, k;
        for (i = 0; i < res->rows; i++) {
                for (j = 0; j < res->cols; j++) {
                        for (k = 0; k < a->cols; k++) {
                                res->data[index(i, j, res->rows)] += a->data[index(i, k, a->rows)] * b->data[index(k, j, b->rows)];
                        }
                }
        }
}

int main() {                                                                                         
    struct matrix mat1, mat2, res;                                                                   

    printf("\nEnter the Order of the First matrix...\n");                                            
    scanf("%d %d", &mat1.rows, &mat1.cols);                                                          
    printf("\nEnter the Order of the Second matrix...\n");                                           
    scanf("%d %d", &mat2.rows, &mat2.cols);                                                          

    mat_alloc(&mat1);                                                                                
    mat_alloc(&mat2);                                                                                

    read_mat(&mat1);                                                                                 
    read_mat(&mat2);                                                                                 

    printf("Scanned matrices: \n");                                                                  
    print_mat(&mat1);                                                                                
    printf("\n");                                                                                    
    print_mat(&mat2);                                                                                
    printf("\n");                                                                                    

    multiply(&mat1, &mat2, &res);                                                                    

    printf("Calculated result: \n");                                                                 
    print_mat(&res);                                                                                 

    return 0;                                                                                        
}       
#包括
#包括
#包括
#定义索引(x,y,r)(x+r*y)
结构矩阵{
int cols;
int行;
双*数据;
};
无效打印矩阵(结构矩阵*矩阵){
int i,j;
对于(i=0;irows;i++){
对于(j=0;jcols;j++){
printf(“%f\t”,mat->data[索引(i,j,mat->rows)];
}
printf(“\n”);
}
}
int mat_alloc(结构矩阵*mat){
mat->data=(双精度*)malloc(mat->rows*mat->cols*sizeof(双精度));
}
int read_mat(结构矩阵*mat){
int i,j;
对于(i=0;irows;i++){
对于(j=0;jcols;j++){
scanf(“%lf”和(mat->data[索引(i,j,mat->rows)]);
}
}
}
整数乘法(结构矩阵*a,结构矩阵*b,结构矩阵*res){
如果(a->cols!=b->行){
printf(“矩阵尺寸不匹配!\n”);
返回0;
}
res->rows=a->rows;
res->cols=b->cols;
材料分配(res);
memset(res->data,0,res->cols*res->rows*sizeof(double));
int i,j,k;
对于(i=0;irows;i++){
对于(j=0;jcols;j++){
对于(k=0;kcols;k++){
res->data[索引(i,j,res->rows)]+=a->data[索引(i,k,a->rows)]*b->data[索引(k,j,b->rows)];
}
}
}
}
int main(){
结构矩阵mat1,mat2,res;
printf(“\n输入第一个矩阵的顺序…\n”);
scanf(“%d%d”、&mat1.rows、&mat1.cols);
printf(“\n输入第二个矩阵的顺序…\n”);
scanf(“%d%d”、&mat2.rows、&mat2.cols);
材料分配(和材料1);
材料分配(和材料2);
阅读材料和材料1;
阅读材料和材料2;
printf(“扫描矩阵:\n”);
打印材料(&mat1);
printf(“\n”);
打印材料(&mat2);
printf(“\n”);
乘法(&mat1,&mat2,&res);
printf(“计算结果:\n”);
打印材料和图片;
返回0;
}       
#include <stdio.h>                                                                                                                                            
#include <stdlib.h>                                                                                                                                           
#include <string.h>                                                                                                                                           

#define index(x, y, r) (x+r*y)                                                                                                                                

struct matrix {
        int cols;
        int rows;
        double * data;
};

void print_mat(struct matrix * mat) {
        int i, j;
        for (i = 0; i < mat->rows; i++) {
                for (j = 0; j < mat->cols; j++) {
                        printf("%f \t", mat->data[index(i, j, mat->rows)]);
                }
                printf("\n");
        }
}

int mat_alloc(struct matrix *mat) {
        mat->data = (double*)malloc(mat->rows*mat->cols*sizeof(double));
}

int read_mat(struct matrix *mat) {
        int i, j;
        for (i = 0; i < mat->rows; i++) {
                for (j = 0; j < mat->cols; j++) {
                        scanf("%lf", &(mat->data[index(i, j, mat->rows)]));
                }
        }
}

int multiply(struct matrix * a, struct matrix * b, struct matrix * res) {
        if(a->cols != b->rows){
                printf("Matrix dimensions do not match!\n");
                return 0;
        }
        res->rows = a->rows;
        res->cols = b->cols;
        mat_alloc(res);
        memset(res->data, 0, res->cols*res->rows*sizeof(double));
        int i, j, k;
        for (i = 0; i < res->rows; i++) {
                for (j = 0; j < res->cols; j++) {
                        for (k = 0; k < a->cols; k++) {
                                res->data[index(i, j, res->rows)] += a->data[index(i, k, a->rows)] * b->data[index(k, j, b->rows)];
                        }
                }
        }
}

int main() {                                                                                         
    struct matrix mat1, mat2, res;                                                                   

    printf("\nEnter the Order of the First matrix...\n");                                            
    scanf("%d %d", &mat1.rows, &mat1.cols);                                                          
    printf("\nEnter the Order of the Second matrix...\n");                                           
    scanf("%d %d", &mat2.rows, &mat2.cols);                                                          

    mat_alloc(&mat1);                                                                                
    mat_alloc(&mat2);                                                                                

    read_mat(&mat1);                                                                                 
    read_mat(&mat2);                                                                                 

    printf("Scanned matrices: \n");                                                                  
    print_mat(&mat1);                                                                                
    printf("\n");                                                                                    
    print_mat(&mat2);                                                                                
    printf("\n");                                                                                    

    multiply(&mat1, &mat2, &res);                                                                    

    printf("Calculated result: \n");                                                                 
    print_mat(&res);                                                                                 

    return 0;                                                                                        
}