Write a main function and the following functions to compute the stress and stra
ID: 3629683 • Letter: W
Question
Write a main function and the following functions to compute the stress and strain in a steel rod of diameter D(inches) and length L (inches) subject to the compression loads P of 10,000 to 1,000,000 poundsin increments of 1000,000 pounds. The modulus of elasticity E for steel is 30 X 106.
- A function to commpute the stress from the formulas:
where A = D2 / 4.0
- A function to commpute the strain from the formulas:
- A function to output the stress and strain at different loads of P.
The function should call each other as shown in the structure chart
Explanation / Answer
So, I'm not sure what the increments were supposed to be, I'm assuming increments of 10000 but I declared it seperately on line 22 to make it easy to change.
I also wasn't sure how the program should take input for D and L, so I used cin.
Stress is called by print, as is strain, and print is called in main. If that structure isn't quite right it shouldn't be hard to pass a increment by reference and call them in main, but that really is poor code structure, Spagetti code, it's called.
To have a function "strain()" alter both "e" and "deltaL" I passed "deltaL" by reference to allow it to be changed in the function and returned strain. You might want to make stress and strain inline, to improve proformance, but it's a small enough program effeciency isn't really an issue.
#include<iostream> // for cout/cin
#include<iomanip> // for setprecision()
float stress(float weight, float diam)
{
float pi = 3.1415926535897932384626;
return ((weight*4.0)/(pi*diam*diam)); //f=p/pi*D*D/4.0
}
float strain(float stress, float len, float & deltaL)
{
float modOfElasSteel = 30000000;
deltaL = (stress*len/modOfElasSteel); // DeltaL = f*L1/E
return (stress/modOfElasSteel); // e = f/E
}
void print(float diam, float len)
{
int increment = 10000, lbound=10000, ubound=1000000;
float deltaL = 0;
std::cout << std::setprecision(2); // apply some structure to the table
std::cout << "lbs applied: stress: strain: deltaL:" << std::endl;
for (int weight = lbound; weight <= ubound; weight += increment)
{
std::cout << weight << " lbs | " << stress(weight, diam) << " psi |" << strain(stress(weight, diam), len, deltaL)
<< " | " << deltaL << " in" << std::endl;
}
}
int main()
{
float diam, len;
std::cout << "I can calculate the stess, strain, and change in len of a steel rod." << std::endl;
std::cout << "Diameter of the rod(in):";
std::cin >> diam;
std::cout << "Length of the rod(in):";
std::cin >> len;
print(diam, len);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.