//------------------------------------
// CALCULATION OF MOMENTS ALONG BEAM
//------------------------------------
for (int k=1;k<=ns; ++k) {
z =(k-1)*L/(ns-1);
z1 = z - a[j];
//Calculations for appropriate load type
switch (tl[j]) {
case 1:
if (z <= a[j])
mk = rl * z;
else if (z >= a[j] + c[j])
mk = rr * (L - z);
break;
case 2:
if (z<=a[j]) {
mk = rl*z;
}
else if(z>=a[j] + c[j]) {
mk = rr*(L-z);
}
else {
wl = W[j] * z1/c[j];
mk = rl * (a[j] + z1) - (wl * z1/2);
}
break;
case 3:
if (z<=a[j]) {
mk = rl*z;
}
else if(z>=a[j] + c[j]) {
mk = rr*(L-z);
}
else {
wl = W[j] * pow((z1/c[j]),2);
mk = rl * (a[j] + z1) - (wl * z1/3);
}
break;
case 4:
z2 = c[j] - z1;
if (z<=a[j]) {
mk = rl*z;
}
else if(z>=a[j] + c[j]) {
mk = rr*(L-z);
}
else {
wl = W[j] * pow((z2/2),2);
mk = rr * (L - a[j] - c[j] + z2) - (wl * z2/3);
}
break;
//-----------------------------------------
} // End of switch scope for bending moments
//-----------------------------------------
// Cumulative bending moments
ms[k] = ms[k] + mk;
//--------------------------------------------
} // End of scope for calculate bending moments
//--------------------------------------------
//------------------------------------------
// SORT FOR MAXIMUM MOMENT AND ITS LOCATION
//------------------------------------------
for(int k = 1; k<=ns; ++k) {
if(ms[k] > mm) {
mm = ms[k];
sm = L * (k-1)/(ns-1);
}
} // End of scope for sorting max BM and its location
//-----------------------------------------------
// CALCULATE DEFLECTIONS AT SECTIONS ALONG BEAM
//-----------------------------------------------
//SET UP SIMPSON'S ORDINATES
s1[1] = 0;
s1[ns] = 0;
for (int k = 2;k<=ns;++k) {
if(k % 2 == 0) { // check for even number
s1[k] = 4;
}
else {
s1[k] = 2; // if not even number, it is odd number
}
}
//NUMERICAL INTEGRATION
for (int k=1; k <= ns; ++k) {
dk = 0;
zd = (k-1) * L/(ns-1);
for(int k1 = 1; k1 <= ns; ++k1) {
// UNIT LOAD MOMENTS
z = (k1 - 1) * L/(ns-1);
//SUMMING MOMENT PRODUCTS AND SIMPSONS' ORDINATES
dk = dk + ms[k1] * mx * s1[k1];
} // end of scope for loop k1
ds[k] = dk * L * pow(10,12)/(ns-1)/3/i2/ec;
} // end of scope for loop k
//SORT FOR MAXIMUM DEFLECTION AND ITS LOCATION
for(int k = 1; k <= ns; ++k) {
if (ds[k] > dm) {
dm = ds[k];
sd = L * (k - 1)/(ns-1);
}
} // end of scope for loop k for sort max deflection
//--------------------------------------
} // End of Scope of Number of Loads Loop
//--------------------------------------
// Display k and vs[k]
cout << endl << "Sec \tShear\tBM\tDefl" << endl;
//cout << fixed;
// DECIDE STEP for FOR LOOP
int step;
step = (ns-1)/10;
// LOOP for display of SF,BM & DEFL along beam
for (int k= 1; k<=ns; k += step ) {
cout << k << "\t" << setprecision(2) << showpoint << fixed << vs[k] << "\t" << ms[k] ;
cout << "\t" << ds[k] << endl;
}
cout << endl;
cout << "Maximum Bending Moment = " << mm << " kNm at position " << sm << " m from left support" << endl;
cout << "Maximum Deflection = " << dm << " mm";
cout << " at a position " << sd <<" m from left support" << endl;
cout << endl << "PRESS ANY KEY TO END THE PROGRAM" << endl;
return 0;
//----------------------------
} // End of Main Function Scope
//----------------------------
I've tested this out on several loading combinations that work Ok. If anyone wants to try some to check it out it would be appreciated. If anyone has any suggestions about how it could be improved, that would also be appreciated.
Just a point, I'm aware that there is no input error checking. That's on my To-Do list.
To get the code tags, edit your post. Highlight the code and then click the <> button to the right of the edit window.
Congratulations on your first C++ program. I found a few things that might be improved:
Variable k1 is unused. I found this by enabling warnings on my compiler. It's a good idea to do this because the compiler can catch bugs as well. ds[k] = dk * L * pow(10, 12) / (ns - 1) / 3 / i2 / ec;
You can use the constant 1E12 in place of pow(10,12).
When displaying the results, it would be better to use a switch statement to set ldType. Switch statements should be used when you're doing something depending on the value of a single expression:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
switch (tl[j]) {
case 1:
ldType = "PL";
break;
case 2:
ldType = "UDL";
break;
case 3:
ldType = "TLL";
break;
case 4:
ldType = "TLR";
break;
}
An even better way is to create an array of the strings and just index into it: