Analyse a beam (Part 3)

//------------------------------------
// 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);

if(z<=zd) {
mx = z * (L - zd)/L;
}
else {
if(z>zd) {
mx = zd * (L - z)/L;
}
}

//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 RESULTS
//-----------------
cout << endl;
cout << "SPAN OF BEAM: " << L << " metres" << endl;
cout << "LOAD No\tTYPE\tWEIGHT\tSTART\tCOVER" <<endl;

for(int j=1;j<=nl; j++) {
if(tl[j] == 1) {
ldType = "PL";
}
else if (tl[j] == 2) {
ldType = "UDL";
}
else if (tl[j] == 3) {
ldType = "TLL";
}
else if (tl[j] == 4) {
ldType = "TLR";
}

cout << j << "\t";
cout << ldType;
cout << "\t" << setprecision(2) << showpoint << fixed << W[j] << "\t" << a[j] << "\t" <<c[j] << endl;
}

// Display Final reactions
cout << endl;
cout << "RA = " << setprecision(2) << showpoint << fixed << ra << "kN ";
cout << "RB = " << setprecision(2) << showpoint << fixed << rb <<"kN" << endl << endl;

cout << "Beam Size: Width = " << setprecision(0) << noshowpoint << b << " mm ";
cout << "Depth = " << setprecision(0) << noshowpoint << d << " mm" << endl;
cout << "Mod of Elasticity = " << setprecision(0) << ec << " N/sq.mm" << endl;
cout << "Moment of Inertia = " << setprecision(4) << showpoint << fixed << i2/(pow(10,12)) << " mm^4" << endl << endl;


// 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.

Thanks in anticpation of any help/suggestions.
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:
1
2
3
4
5
6
        static const char *vals[] = {"PL", "UDL", "TLL", "TLR"};
        if (tl[j] < 1 || tl[j] > 4) {
            ldType = "unknown";
        } else {
            ldType = vals[tl[j] - 1];
        }

Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/210417/
@dhayden
Thank you for your comments, particularly the last bit of code. I wouldn't have thought of doing that.

As you can see I have used switch earlier in the program. I used if..else here just to use it for practice.

I am now ticking off this thread and will post to the first thread on the subject. But any further comments will be appreciated there.
Topic archived. No new replies allowed.