Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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
C++ 块矩阵转置_C++_Algorithm_Caching_Matrix - Fatal编程技术网

C++ 块矩阵转置

C++ 块矩阵转置,c++,algorithm,caching,matrix,C++,Algorithm,Caching,Matrix,我想通过将输入矩阵划分为块,然后进行转置来实现矩阵的转置。我参考了相应的帖子,编写了如下代码: #include<iostream> #include<stdlib.h> #define m 4 #include<sys/time.h> #include<time.h> #include<malloc.h> using namespace std; int **a, **b, **c; int count = 0; clock_t

我想通过将输入矩阵划分为块,然后进行转置来实现矩阵的转置。我参考了相应的帖子,编写了如下代码:

#include<iostream>
#include<stdlib.h>
#define m 4
#include<sys/time.h>
#include<time.h>
#include<malloc.h>

using namespace std;

int **a, **b, **c;
int count = 0;
clock_t t1, t2;    
int blocksize = 2;

int main(){
    a = (int **)malloc(m*sizeof(int *));
    for(int i = 0;i<m;i++){
            a[i] = (int *)malloc(m*sizeof(int));
    }
    b = (int **)malloc(m*sizeof(int *));
    for(int i = 0;i<m;i++){
            b[i] = (int *)malloc(m*sizeof(int));
    }
    for(int i=0; i<m; i++){
            for(int j =0; j<m; j++){
                    a[i][j]=(2*i)+(3*j);
            }
    }
    for(int i=0; i<m; i++){
            for(int j =0; j<m; j++){
                    cout << a[i][j] << "\t";
            }
            cout << "\n";
     }
    cout << "\n";
    t1 = clock();
    // MAIN BLOCK TRANSPOSE CODE
    for (int i = 0; i < m; i += blocksize) {
        for (int j = 0; j < m; j += blocksize) {
                    for (int k = i; k < i + blocksize; ++k) {
                            for (int l = j; l < j + blocksize; ++l) {
                                    b[k + l*m] = a[l + k*m];
                            }
                    }
            }
    }
    t2 = clock();
    for(int i=0; i<m; i++){
            for(int j =0; j<m; j++){
                    cout << b[i][j] << "\t";
            }
            cout << "\n";
     }
    free(a);
    free(b);
    cout << "\n";
    cout << (double)(t2-t1)/CLOCKS_PER_SEC << "\n";
return 0;
}  
预期输出阵列:

0       2       4       6
3       5       7       9  
6       8       10      12  
9       11      13      15  
获得的结果:

0       3       6       9
Segmentation fault

我认为你的矩阵应该在一个数组中编码,而不是在一个数组数组中编码(参见链接问题的编辑2)

您可能想试试这个:

int *a, *b, *c;

a = (int *)malloc(m*m*sizeof(int));
b = (int *)malloc(m*m*sizeof(int));
for(int i=0; i<m; i++){
        for(int j =0; j<m; j++){
                a[i*m+j]=(2*i)+(3*j);
        }
}
for(int i=0; i<m; i++){
        for(int j =0; j<m; j++){
                cout << a[i*m+j] << "\t";
        }
        cout << "\n";
 }
cout << "\n";
int*a、*b、*c;
a=(int*)malloc(m*m*sizeof(int));
b=(int*)malloc(m*m*sizeof(int));

对于(int i=0;i你所说的“代码未按预期工作”是什么意思?它在做什么?你期望什么?它显示原始矩阵的第一行并给出分段错误我认为你的矩阵应该在单个数组中编码,而不是在数组数组中。请参阅链接问题的
编辑2
。“未按预期工作”还不够好。请解释您的预期,以及程序未能达到这些预期的原因。编辑问题,而不是添加注释以提供缺少的细节。谢谢。工作出色。+1 Wesome man!谢谢。简单的修改使我的程序正常工作:)
int *a, *b, *c;

a = (int *)malloc(m*m*sizeof(int));
b = (int *)malloc(m*m*sizeof(int));
for(int i=0; i<m; i++){
        for(int j =0; j<m; j++){
                a[i*m+j]=(2*i)+(3*j);
        }
}
for(int i=0; i<m; i++){
        for(int j =0; j<m; j++){
                cout << a[i*m+j] << "\t";
        }
        cout << "\n";
 }
cout << "\n";