>
New Topic
>
Reply<
Esato Forum Index
>
General discussions >
Non mobile discussion
> C Program - needs debugging
Bookmark topic
Hi, wondering if anyone can help me out with this or gimme some pointers but basically this is what i have to do:
============================
Write a program that will calculate the thickness, t, a plank of wood needs to be if suspended between two walls a distance L apart and the deflection, d, should not exceed 50 mm when a person of mass m stands in the centre. Equations required are d = FL3/(48EI), F= mg and I =wt3/12. (Thus E, d and g are constants). Your program should ask the user to input the other variables w (width of plank), L and m into your program. (Young’s modulus E for wood is 10 GPa, g is 9.81 m s-2)
============================
and this is the code i wrote for it using C programming
============================
#include
#include
/* Declare variables */
float m;
float w;
float L;
double t;
float d = 0.05;
float E = 1e10;
float g = 9.81;
int x = 12;
int y = 48;
double a;
double b;
double c;
int main()
{
/*input data */
printf ("Please enter the mass of the man in kilograms(kg)");
scanf ("%d",&m);
printf ("Please enter the distance between the two supporting walls in metres(m)");
scanf ("%d",&L);
printf ("Please enter the width of the plank in metres(m)");
scanf ("%d",&w);
/* do calculations */
a = ((x)*(m*g)*(L*L*L));
b = (y*E*d*w);
c = (a/b);
t = pow (c,(1/3));
printf ("The thickness of the plank needs to be: %d",&t);
}
============================
But when i try to type in any values in the programe i get the same answer everytime: 4210720
This has been bugging me all day, i cant work out whats wrong
Any1 help me out?
Thanks
50
_________________

Wow 2400+ posts....
50Cent (+5, -0)
[ This Message was edited by: 50Cent on 2006-10-31 21:34 ]
--
Posted: 2006-10-31 22:23:45
Edit :
Quote
I'm a C developer as a profession, though not on PC's, but my initial reaction is to get you to change
printf ("The thickness of the plank needs to be: %d",&t);
to
printf ("The thickness of the plank needs to be: %d",t);
as the initial statement is trying to print the address of t rather than the value stored in it
--
Posted: 2006-10-31 22:31:30
Edit :
Quote
Quote:
On 2006-10-31 22:31:30, masseur wrote:
I'm a C developer as a profession, though not on PC's, but my initial reaction is to get you to change
printf ("The thickness of the plank needs to be: %d",&t);
to
printf ("The thickness of the plank needs to be: %d",t);
as the initial statement is trying to print the address of t rather than the value stored in it
ah ok that makes sense thanks, changed it... lemme try the program
hmmm....now it says the value is always zero
To break down the problem
The user has to input 3 peice of data:
- Distance (L)
- Mass (m)
- Width (w)
and then they need to tbe input into the following equation:
Thickness t = Cube Root [(12)*(m*g)*(L*L*L)]/ [48*E*d*w]
all the other letters and there corresponding values, i declared in the 1st section.
_________________

Wow 2400+ posts....
50Cent (+5, -0)
[ This Message was edited by: 50Cent on 2006-10-31 21:48 ]
--
Posted: 2006-10-31 22:46:30
Edit :
Quote
anyone?
--
Posted: 2006-11-01 13:19:13
Edit :
Quote
here you go. I'm at work so could give this a quick go.
what I changed...
since you defined m, w and L as float the scanf should use type %f, not %d which is for decimal integer. Therefore the values were not being accepted by the program correctly.
Also since the parameters to pow are of type double, the 1 / 3 must be represented as 1.0 / 3.0 otherwise this divide results in 0 and the result of the pow will always be 1.
finally, since your result t is a double precision you must use the type %g to output it, rather than %d, for the same reasons as above
here is the working program, though I cannot vouch that the actual formulas used as valid for what you are trying to calculate, but the program will give the right result using those formulas as specified
#include
#include
/* Declare variables */
float m;
float w;
float L;
double t;
float d = 0.05;
float E = 1e10;
float g = 9.81;
int x = 12;
int y = 48;
double a;
double b;
double c;
int main()
{
/*input data */
printf ("Please enter the mass of the man in kilograms(kg)");
scanf ("%f",&m);
printf ("Please enter the distance between the two supporting walls in metres(m)");
scanf ("%f",&L);
printf ("Please enter the width of the plank in metres(m)");
scanf ("%f",&w);
/* do calculations */
a = ((x)*(m*g)*(L*L*L));
b = (y*E*d*w);
c = (a/b);
t = pow (c,(1.0/3.0));
printf ("The thickness of the plank needs to be: %g",t);
}
--
Posted: 2006-11-01 14:08:35
Edit :
Quote
ahh ur a legend!!! thanks mate
--
Posted: 2006-11-02 21:37:27
Edit :
Quote
Just sent Masseur a PM, but if any1 else can answer this quick Q that'd be great
this is what ive got.....
-Its going to be part of a menu operated program.
-The coding alll goes fine untill the bit where i use if-statements
-I cant get it to read the value of x correctly. "x" is supposed to be two thirds of "y" which the user inputs
- it works fine, if i replace x with just the correct interger (that i worked out on a calculator), but not this way
any idea?
Many Thanks
50
=========================
#include
#include
/* Declare variables */
float P;
float a;
float b;
float t;
float k;
float R;
float y;
float x;
int main()
{
/*Input Data */
printf ("Please enter outside radius of vessel (mm): ");
scanf ("%f",&b);
printf ("Please enter inside redius of the vessel (mm): ");
scanf ("%f",&a);
printf ("Please enter the yield stress of the material: ");
scanf ("%f",&y);
/* Do Calculations */
t = (b/1000)-(a/1000);
k = (b/a);
R = (a+(t/2));
P = (y*((k*k)-1))/(2*(k*k));
/*Display Answers*/
printf ("The maximum pressure a cylindrical vessel can sustain without yielding is: %f (MNm-2)n",P);
/*Check if it s a safe working pressure*/
x = ((2/3)*y);
if (P
printf("Safe working pressure for vesseln");
else if (P > x)
printf("The working pressure of this vessel is unsafen");
}
======================
_________________

Wow 2400+ posts....
50Cent (+5, -0)
[ This Message was edited by: 50Cent on 2006-12-02 22:11 ]
--
Posted: 2006-12-02 23:11:03
Edit :
Quote
at a quick glance, and without a compiler handy, my initial thought is that 1/3 should be 1.0/3.0 in the same way it was in my response to your previous problem
--
Posted: 2006-12-02 23:16:19
Edit :
Quote
oh my god... yeah you nailed it!
Oh and one other thing.... when it calculates the value of P.... it gives the answer to 6 decimal places... anyway i can reduce this?
Thanks again for your help
--
Posted: 2006-12-02 23:21:36
Edit :
Quote
p will have more that 6 decimal places and since you are not displaying it in the program I assume you are looking at it in a debugger.
if you want to reduce the number of decimal places though it depends on your compiler.
there may be a round function which accepts 2 parameters
i.e. x = round(y,n) where n is the precision you want.
alternatively there may be a round function which simply rounds to an integer in which case you need to use the following syntax
x = round ( y * 1000.0 ) / 1000.0
this example would round to 3 decimal places and you simply change 1000.0 to any multiple of 10 to get the precision you want.
hope that helps
--
Posted: 2006-12-02 23:47:04
Edit :
Quote
New Topic
Reply