I am trying to produce this code but have and error in the double
functions starting at double b[n];
. The error I am getting is saying that "the expression must have a constant value, the variable 'n' can not be used as a constant. Any help you can give would be much appreciated.
//Get inputs from user
double V = 0; // shear load applied
int n;
double H_total = 0;
double A_total = 0;
double a = 0;
double I = 0;
double t = 0;
double e = 0;
double y_bar = 0;
cout << "Input the shear load applied in [N]: " << endl;
cin >> V;
cout << "Input number of sections: " << endl;
cin >> n;
double b[n];
double h[n];
double A[n];
double y[n];
double Q[n];
double Tau[n];
for (int i = 1; i <= n; i++) { // Calculates variables to find shear stress
cout << "Width of section " << i << " in [mm]: " << endl;
cin >> b[i];
cout << "Height of section " << i << " in [mm]: " << endl;
cin >> h[i];
H_total += h[i];
A[i] = b[i] * h[i];
A_total += A[i];
y[i] = H_total - 0.5 * h[i];
a += A[i] * y[i];
y_bar = a / A_total;
}
cout << "Applied shear force, V = " << V / 1000 << " kN" << endl;
cout << "Y coordinate of the centroid for given cross section, Y_Bar = " << y_bar << " mm" << endl;
for (int i = 1; i <= n; i++) { // Finds moment of inertia
double d = (y[i] - y_bar);
I += (b[i] * pow(h[i], 3.0) / 12.0) + (A[i] * pow(d, 2.0));
}
cout << "Moment of Inertia, I = " << I << " mm^4" << endl;
for (int i = 1; i <= n; i++) { // Calculates first moment of inertia
Q[i] = A[i] * (y[i] - y_bar);
}
for (int i = 1; i <= n - 1; i++) {
if (b[i] <= b[i + 1]) {
t = b[i];
}
else {
t = b[i + 1];
}
Tau[i] = (abs(V * Q[i]) / (I * t));
}
for (int i = 1; i <= n - 1; i++) {
if (i <= 2) {
e += Tau[i];
}
else {
e -= Tau[i];
}
cout << "Shear stress between sections " << i << " and " << i + 1 << " = " << e << " MPa" <<
endl;
}
}