Ridwan
Ridwan

Reputation: 37

Multidimensional arrays from MATLAB to C++

So I'm trying to do one of my project I did in MATLAB in C++ but I got stuck along the way.

Here's the portion of code here in MATLAB I want to convert to C++. It does work on MATLAB but not working in C++

RelRough = [0, 1E-6, 5E-6, 1E-5, 5E-5, 0.0001, 0.0002, 0.0004, 0.0006, 0.0008, 0.001]; 
ReT = [4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000,  100000,  200000,  300000,  400000,  500000];
for i = 1:length(ReT)
    for j = 1:length(RelRough)
       FCT_guess = 1;
       tolerance = 1;
       while tolerance > 1e-14
            FCT_cal = 1/(-2*log10((RelRough(j)/3.7) + (2.51/(ReT(i)*sqrt(FCT_guess)))))^2;
            tolerance = abs(FCT_cal-FCT_guess);
            FCT_guess = FCT_cal;
            FCT(i,j) = FCT_cal*1000;
       end
    end
end

Here's my C++ version and I kept getting error like "expression must have integral or unscoped enum type" for variable g

double RelRough[] = { 0, 1E-6, 5E-6, 1E-5, 5E-5, 0.0001, 0.0002, 0.0004, 0.0006, 0.0008, 0.001 };
const int lengthRelRough = sizeof(RelRough) / sizeof(RelRough[0]);

double ReT[] = { 4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000,  100000,  200000,  300000,  400000,  500000 };
const int lengthReT = sizeof(ReT) / sizeof(ReT[0]);

double fct[lengthReT][lengthRelRough] = { 0 };
    double fct_guess = 1;
double tolerance = 1;
double fct_cal = 0;
for (int ii = 0; ii < lengthReT; ++ii) {
    for (int jj = 0; jj < lengthRelRough; ++jj) {
        while (tolerance > 1e-14) {
            double h = (RelRough[jj] / 3.7), w = (2.51 / (ReT[ii] * sqrt(fct_guess)));
            double g = (-2*log10(h+w))^2;
            fct_cal = 1/g;
            tolerance = abs(fct_cal - fct_guess);
            fct_guess = fct_cal;
            fct[ii][jj] = fct_cal;
            std::cout << fct[ii][jj] << "\t";
        }
    }
}
    return 0;

}

Is there anyone that help to see what's wrong. Thanks in advance!

Upvotes: 1

Views: 67

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

Change this:

double g = (-2*log10(h+w))^2;

into:

double g = pow(-2*log10(h+w),2.0);

As @Eljay pointed out in his comment, the operator ^ performs a XOR in C++, and not an exponentiation. For more information:

Upvotes: 1

Related Questions