I have a 2d array I want to transpose and return from a function. Below is my code and I get the following error
#include <iostream>
#include <bits/stdc++.h>
#include <chrono>
#include <fstream>
using namespace std;
#define R 550 // ROWS
#define C 550 // COLS
void generate_matrix(double (*mat)[C], int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
mat[i][j] = rand() % 100;
}
}
}
void print_matrix(double (*mat)[C], int n) {
printf("Printing Matrix:\n ");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << mat[i][j] <<" ";
}
}
}
void initialize_matrix(double (*mat)[C], int n) {
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
mat[i][j] = 0.0;
}
}
}
double** transpose_matrix(double (*mat)[C]) { // problem is here
double transpose[R][C];
for (int i = 0; i < R; ++i) {
for (int j = 0; j < R; ++j) {
transpose[j][i] = mat[i][j];
}
}
return transpose; // problem is here
}
int main() {
double A[R][C], B[R][C], result[R][C];
generate_matrix(A, C);
generate_matrix(B, C);
double** transposed_B = transpose_matrix(B);
print_matrix(transposed_B);
}
Error Message:
mat_mul_transpose.cpp: In function ‘double** transpose_matrix(double (*)[550])’:
mat_mul_transpose.cpp:46:12: error: cannot convert ‘double (*)[550]’ to ‘double**’ in return
return transpose;
How to fix this error?